There is an undocummented
afterError
hook that you can use for the backend for this purpose.
In your config you can do this:
buildConfig({
// collections, etc.
hooks: { afterError (err, res) => {
// call your sentry code here
}
});
On the frontend I would probably set up the error boundary in a provider in
admin.components.providers
or your own
index.tsx
The afterError hook is called in an express middleware for REST, or in Payload's GraphQL code. I don't believe it gets called when using the local API.
It would be interesting to know which request triggered the error, otherwise Sentry won't be able to track much. How about a breaking change that would change the
errorHandler
arguments to a single object with:
afterError({
error: Error,
body: ErrorResponse,
request?: PayloadRequest
response?: ExpressResponse
}):
// Send this response if the error originated from HTTP request
| ErrorResponse
// Don't send a response, the handler take care of it
| null
This would cover both local API and HTTP contexts
I'd agree, that would be way more helpful.
If I can remember back to why I did this 2 years ago this way it had to do with supporting both GraphQL and REST API with the same hook.
I'm sure there is room for improvement on this and adding some documentation and tests around this would be 💯
Received my first errors 👌
Star
Discord
online
Get help straight from the Payload team with an Enterprise License.