Simplify your stack and build anything. Or everything.
Build tomorrow’s web with a modern solution you truly own.
Code-based nature means you can build on top of it to power anything.
It’s time to take back your content infrastructure.

Is it possible to catch errors with Sentry?

default discord avatar
thgh.2 years ago
6
  • discord user avatar
    dribbens
    2 years 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.2 years 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
    2 years 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.2 years ago

    Received my first errors 👌

  • default discord avatar
    tombell95last year
    @969226489549713438

    is this still required with plugin-sentry (

    https://github.com/payloadcms/plugin-sentry

    ) ?

  • discord user avatar
    dribbens
    last year

    Right, you should be able use the plugin. I haven't tested this plugin on Payload 2.0 yet. It may need alias changes to support the bundler pattern.


    Please open an issue if you have any trouble using it.

Star on GitHub

Star

Chat on Discord

Discord

online

Can't find what you're looking for?

Get dedicated engineering support directly from the Payload team.