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. |
36 lines
798 B
TypeScript
36 lines
798 B
TypeScript
import type { Config } from 'payload'
|
|
|
|
import type { PluginOptions } from './types.js'
|
|
|
|
import { captureException } from './captureException.js'
|
|
import { startSentry } from './startSentry.js'
|
|
|
|
export const sentryPlugin =
|
|
(pluginOptions: PluginOptions) =>
|
|
(incomingConfig: Config): Config => {
|
|
const config = { ...incomingConfig }
|
|
|
|
if (pluginOptions.enabled === false || !pluginOptions.dsn) {
|
|
return config
|
|
}
|
|
|
|
config.hooks = {
|
|
...(incomingConfig.hooks || {}),
|
|
|
|
afterError: [
|
|
({ error }) => {
|
|
captureException(error)
|
|
},
|
|
],
|
|
}
|
|
|
|
config.onInit = async (payload) => {
|
|
if (incomingConfig.onInit) {
|
|
await incomingConfig.onInit(payload)
|
|
}
|
|
startSentry(pluginOptions, payload)
|
|
}
|
|
|
|
return config
|
|
}
|