Skip to content

Commit e0f5414

Browse files
committed
doc: add exceptions
1 parent 7c9c76a commit e0f5414

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed

docs/.vitepress/config.ts

+4
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,10 @@ export default defineConfig({
9797
{
9898
text: 'Advanced Usage',
9999
items: [
100+
{
101+
text: 'Exceptions',
102+
link: '/guide/advanced_usage/exceptions'
103+
},
100104
{
101105
text: 'PrecompileUrl',
102106
link: '/guide/advanced_usage/pre-compile-on-demand'
+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
2+
# Exceptions
3+
4+
|Code |Description | Origin |
5+
| -------------------------- | ----------------------------------------------- | ------- |
6+
| E_MISSING_PACKAGE | Missing package | |
7+
| E_CANNOT_CREATE_ATTACHMENT | Unable to create Attachment Object | |
8+
| E_ISNOT_BUFFER | Is not a Buffer | |
9+
| E_ISNOT_BASE64 | Is not a Base64 | |
10+
| ENOENT | Unable to read file | |
11+
| E_CANNOT_WRITE_FILE | Unable to write file to the destination | Drive |
12+
| E_CANNOT_READ_FILE | Unable to read file | Drive |
13+
| E_CANNOT_DELETE_FILE | Unable to delete file | Drive |
14+
| E_CANNOT_SET_VISIBILITY | Unable to set file visibility | Drive |
15+
| E_CANNOT_GENERATE_URL | Unable to generate URL for a file | Drive |
16+
| E_UNALLOWED_CHARACTERS | The file key has unallowed set of characters | Drive |
17+
| E_INVALID_KEY | Key post normalization leads to an empty string | Drive |
18+
19+
[Adonis documentation exception](https://docs.adonisjs.com/guides/basics/exception-handling)
20+
21+
## Handling exceptions
22+
23+
If you want to handle a specific exception differently, you can do that inside the `handle` method. Make sure to use the `ctx.response.send` method to send a response, since the return value from the `handle` method is discarded.
24+
25+
::: code-group
26+
27+
```typescript [API]
28+
import { errors } from '@jrmc/adonis-attachment'
29+
30+
export default class HttpExceptionHandler extends ExceptionHandler {
31+
async handle(error: unknown, ctx: HttpContext) {
32+
if (error instanceof errors.E_CANNOT_WRITE_FILE) {
33+
const err = error as errors.E_CANNOT_WRITE_FILE
34+
ctx.response.status(422).send(err.messages)
35+
return
36+
}
37+
38+
return super.handle(error, ctx)
39+
}
40+
}
41+
```
42+
43+
```typescript [web]
44+
import { errors } from '@jrmc/adonis-attachment'
45+
46+
export default class HttpExceptionHandler extends ExceptionHandler {
47+
async handle(error: unknown, ctx: HttpContext) {
48+
if (error instanceof errors.E_CANNOT_WRITE_FILE) {
49+
ctx.session.flash('notification', {
50+
type: 'error',
51+
message: err.message,
52+
})
53+
54+
return ctx.response.redirect('back')
55+
}
56+
57+
return super.handle(error, ctx)
58+
}
59+
}
60+
61+
```
62+
63+
:::

0 commit comments

Comments
 (0)