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. |
74 lines
2.1 KiB
TypeScript
74 lines
2.1 KiB
TypeScript
import { fileURLToPath } from 'node:url'
|
|
import path from 'path'
|
|
const filename = fileURLToPath(import.meta.url)
|
|
const dirname = path.dirname(filename)
|
|
import type { SanitizedConfig } from 'payload'
|
|
|
|
import { APIError } from 'payload'
|
|
|
|
import { buildConfigWithDefaults } from '../buildConfigWithDefaults.js'
|
|
import { AfterOperationCollection } from './collections/AfterOperation/index.js'
|
|
import ChainingHooks from './collections/ChainingHooks/index.js'
|
|
import ContextHooks from './collections/ContextHooks/index.js'
|
|
import { DataHooks } from './collections/Data/index.js'
|
|
import Hooks, { hooksSlug } from './collections/Hook/index.js'
|
|
import NestedAfterReadHooks from './collections/NestedAfterReadHooks/index.js'
|
|
import Relations from './collections/Relations/index.js'
|
|
import TransformHooks from './collections/Transform/index.js'
|
|
import Users, { seedHooksUsers } from './collections/Users/index.js'
|
|
import { DataHooksGlobal } from './globals/Data/index.js'
|
|
export const HooksConfig: Promise<SanitizedConfig> = buildConfigWithDefaults({
|
|
admin: {
|
|
importMap: {
|
|
baseDir: path.resolve(dirname),
|
|
},
|
|
},
|
|
collections: [
|
|
AfterOperationCollection,
|
|
ContextHooks,
|
|
TransformHooks,
|
|
Hooks,
|
|
NestedAfterReadHooks,
|
|
ChainingHooks,
|
|
Relations,
|
|
Users,
|
|
DataHooks,
|
|
],
|
|
globals: [DataHooksGlobal],
|
|
endpoints: [
|
|
{
|
|
path: '/throw-to-after-error',
|
|
method: 'get',
|
|
handler: () => {
|
|
throw new APIError("I'm a teapot", 418)
|
|
},
|
|
},
|
|
],
|
|
hooks: {
|
|
afterError: [() => console.log('Running afterError hook')],
|
|
},
|
|
onInit: async (payload) => {
|
|
await seedHooksUsers(payload)
|
|
await payload.create({
|
|
collection: hooksSlug,
|
|
data: {
|
|
check: true,
|
|
fieldBeforeValidate: false,
|
|
collectionBeforeValidate: false,
|
|
fieldBeforeChange: false,
|
|
collectionBeforeChange: false,
|
|
fieldAfterChange: false,
|
|
collectionAfterChange: false,
|
|
collectionBeforeRead: false,
|
|
fieldAfterRead: false,
|
|
collectionAfterRead: false,
|
|
},
|
|
})
|
|
},
|
|
typescript: {
|
|
outputFile: path.resolve(dirname, 'payload-types.ts'),
|
|
},
|
|
})
|
|
|
|
export default HooksConfig
|