fix(plugin-sentry): capture non APIError errors to sentry (#9595)
Previously, non Payload errors (that aren't `APIError`, for example from `throw new Error()`) weren't captured to Sentry
This commit is contained in:
@@ -53,9 +53,8 @@ export const sentryPlugin =
|
||||
afterError: [
|
||||
...(config.hooks?.afterError ?? []),
|
||||
async (args) => {
|
||||
if ('status' in args.error) {
|
||||
const apiError = args.error as APIError
|
||||
if (apiError.status >= 500 || captureErrors.includes(apiError.status)) {
|
||||
const status = (args.error as APIError).status ?? 500
|
||||
if (status >= 500 || captureErrors.includes(status)) {
|
||||
let context: Partial<ScopeContext> = {
|
||||
extra: {
|
||||
errorCollectionSlug: args.collection?.slug,
|
||||
@@ -86,7 +85,6 @@ export const sentryPlugin =
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
],
|
||||
},
|
||||
|
||||
@@ -61,7 +61,29 @@ describe('@payloadcms/plugin-sentry - unit', () => {
|
||||
|
||||
const hook = config.hooks?.afterError?.[0] as AfterErrorHook
|
||||
|
||||
const error = new APIError('ApiError', 500)
|
||||
const apiError = new Error('ApiError')
|
||||
|
||||
const afterApiErrorHookArgs: AfterErrorHookArgs = {
|
||||
req: {} as PayloadRequest,
|
||||
context: {},
|
||||
error: apiError,
|
||||
collection: { slug: 'mock-slug' } as any,
|
||||
}
|
||||
|
||||
const captureExceptionSpy = jest.spyOn(mockSentry, 'captureException')
|
||||
|
||||
await hook(afterApiErrorHookArgs)
|
||||
|
||||
expect(captureExceptionSpy).toHaveBeenCalledTimes(1)
|
||||
expect(captureExceptionSpy).toHaveBeenCalledWith(apiError, {
|
||||
extra: {
|
||||
errorCollectionSlug: 'mock-slug',
|
||||
hintTimestamp,
|
||||
},
|
||||
})
|
||||
expect(captureExceptionSpy).toHaveReturnedWith(mockExceptionID)
|
||||
|
||||
const error = new Error('Error')
|
||||
|
||||
const afterErrorHookArgs: AfterErrorHookArgs = {
|
||||
req: {} as PayloadRequest,
|
||||
@@ -70,11 +92,9 @@ describe('@payloadcms/plugin-sentry - unit', () => {
|
||||
collection: { slug: 'mock-slug' } as any,
|
||||
}
|
||||
|
||||
const captureExceptionSpy = jest.spyOn(mockSentry, 'captureException')
|
||||
|
||||
await hook(afterErrorHookArgs)
|
||||
|
||||
expect(captureExceptionSpy).toHaveBeenCalledTimes(1)
|
||||
expect(captureExceptionSpy).toHaveBeenCalledTimes(2)
|
||||
expect(captureExceptionSpy).toHaveBeenCalledWith(error, {
|
||||
extra: {
|
||||
errorCollectionSlug: 'mock-slug',
|
||||
|
||||
Reference in New Issue
Block a user