fix: handle thrown errors in config-level afterError hook (#5147)
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import type { SanitizedConfig } from '../../packages/payload/src/config/types'
|
||||
|
||||
import { APIError } from '../../packages/payload/errors'
|
||||
import { buildConfigWithDefaults } from '../buildConfigWithDefaults'
|
||||
import AfterOperation from './collections/AfterOperation'
|
||||
import ChainingHooks from './collections/ChainingHooks'
|
||||
@@ -24,6 +25,18 @@ export const HooksConfig: Promise<SanitizedConfig> = buildConfigWithDefaults({
|
||||
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({
|
||||
|
||||
@@ -462,4 +462,13 @@ describe('Hooks', () => {
|
||||
expect(doc.field_globalAndField).toStrictEqual(globalAndFieldString + globalAndFieldString)
|
||||
})
|
||||
})
|
||||
|
||||
describe('config level after error hook', () => {
|
||||
it('should handle error', async () => {
|
||||
const response = await fetch(`${apiUrl}/throw-to-after-error`)
|
||||
const body = await response.json()
|
||||
expect(response.status).toEqual(418)
|
||||
expect(body).toEqual({ errors: [{ message: "I'm a teapot" }] })
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user