Like what we’re doing? Star us on GitHub!

Is it possible to catch errors with Sentry?

thgh
4 weeks ago
6
  • dribbens
    Payload Team
    4 weeks ago

    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.

  • thgh
    4 weeks ago

    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

  • dribbens
    Payload Team
    4 weeks ago

    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 đź’Ż

  • thgh
    4 weeks ago

    Received my first errors đź‘Ś

Open the post
Continue the discussion in Discord
Can't find what you're looking for?
Get help straight from the Payload team with an Enterprise License.Learn More