fix: handle thrown errors in config-level afterError hook (#5147)

This commit is contained in:
Elliot DeNolf
2024-02-21 16:44:16 -05:00
committed by GitHub
parent 70e57fef18
commit 32ed95e1ee
3 changed files with 23 additions and 1 deletions

View File

@@ -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({

View File

@@ -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" }] })
})
})
})