Is it possible to catch errors with Sentry?

default discord avatar
thgh
7 months ago
6
  • discord user avatar
    dribbens
    Payload Team
    7 months 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.

  • default discord avatar
    thgh
    7 months 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

  • discord user avatar
    dribbens
    Payload Team
    7 months 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 💯

  • default discord avatar
    thgh
    7 months ago

    Received my first errors 👌

Open the post
Continue the discussion in Discord
Like what we're doing?
Star us on GitHub!

Star

Connect with the Payload Community on Discord

Discord

online

Can't find what you're looking for?

Get help straight from the Payload team with an Enterprise License.