You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
feat!: improve afterError hook to accept array of functions, change to object args (#8389)
Changes the `afterError` hook structure, adds tests / more docs.
Ensures that the `req.responseHeaders` property is respected in the
error handler.
**Breaking**
`afterError` now accepts an array of functions instead of a single
function:
```diff
- afterError: () => {...}
+ afterError: [() => {...}]
```
The args are changed to accept an object with the following properties:
| Argument | Description |
| ------------------- |
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|
| **`error`** | The error that occurred. |
| **`context`** | Custom context passed between Hooks. [More
details](./context). |
| **`graphqlResult`** | The GraphQL result object, available if the hook
is executed within a GraphQL context. |
| **`req`** | The
[Request](https://developer.mozilla.org/en-US/docs/Web/API/Request)
object containing the currently authenticated `user` |
| **`collection`** | The [Collection](../configuration/collections) in
which this Hook is running against. This will be `undefined` if the hook
is executed from a non-collection endpoint or GraphQL. |
| **`result`** | The formatted error result object, available if the
hook is executed from a REST context. |
@@ -289,6 +290,30 @@ The following arguments are provided to the `afterOperation` hook:
289
290
|**`operation`**| The name of the operation that this hook is running within. |
290
291
|**`result`**| The result of the operation, before modifications. |
291
292
293
+
### afterError
294
+
295
+
The `afterError` Hook is triggered when an error occurs in the Payload application. This can be useful for logging errors to a third-party service, sending an email to the development team, logging the error to Sentry or DataDog, etc. The output can be used to transform the result object / status code.
|**`context`**| Custom context passed between Hooks. [More details](./context). |
312
+
|**`graphqlResult`**| The GraphQL result object, available if the hook is executed within a GraphQL context. |
313
+
|**`req`**| The `PayloadRequest` object that extends [Web Request](https://developer.mozilla.org/en-US/docs/Web/API/Request). Contains currently authenticated `user` and the Local API instance `payload`. |
314
+
|**`collection`**| The [Collection](../configuration/collections) in which this Hook is running against. |
315
+
|**`result`**| The formatted error result object, available if the hook is executed from a REST context. |
316
+
292
317
### beforeLogin
293
318
294
319
For [Auth-enabled Collections](../authentication/overview), this hook runs during `login` operations where a user with the provided credentials exist, but before a token is generated and added to the response. You can optionally modify the user that is returned, or throw an error in order to deny the login operation.
Copy file name to clipboardExpand all lines: docs/hooks/overview.mdx
+12-9Lines changed: 12 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -43,7 +43,7 @@ export default buildConfig({
43
43
// ...
44
44
// highlight-start
45
45
hooks: {
46
-
afterError:() => {...}
46
+
afterError:[() => {...}]
47
47
},
48
48
// highlight-end
49
49
})
@@ -57,28 +57,31 @@ The following options are available:
57
57
58
58
### afterError
59
59
60
-
The `afterError` Hook is triggered when an error occurs in the Payload application. This can be useful for logging errors to a third-party service, sending an email to the development team, logging the error to Sentry or DataDog, etc.
60
+
The `afterError` Hook is triggered when an error occurs in the Payload application. This can be useful for logging errors to a third-party service, sending an email to the development team, logging the error to Sentry or DataDog, etc. The output can be used to transform the result object / status code.
61
61
62
62
```ts
63
63
import { buildConfig } from'payload'
64
64
65
65
exportdefaultbuildConfig({
66
66
// ...
67
67
hooks: {
68
-
afterError: async ({ error }) => {
68
+
afterError: [async ({ error }) => {
69
69
// Do something
70
-
}
70
+
}]
71
71
},
72
72
})
73
73
```
74
74
75
75
The following arguments are provided to the `afterError` Hook:
|**`context`**| Custom context passed between Hooks. [More details](./context). |
81
+
|**`graphqlResult`**| The GraphQL result object, available if the hook is executed within a GraphQL context. |
82
+
|**`req`**| The `PayloadRequest` object that extends [Web Request](https://developer.mozilla.org/en-US/docs/Web/API/Request). Contains currently authenticated `user` and the Local API instance `payload`. |
83
+
|**`collection`**| The [Collection](../configuration/collections) in which this Hook is running against. This will be `undefined` if the hook is executed from a non-collection endpoint or GraphQL. |
84
+
|**`result`**| The formatted error result object, available if the hook is executed from a REST context. |
82
85
## Async vs. Synchronous
83
86
84
87
All Hooks can be written as either synchronous or asynchronous functions. Choosing the right type depends on your use case, but switching between the two is as simple as adding or removing the `async` keyword.
0 commit comments