Skip to content

How to prevent handleFastifyError.handleFastifyError errors in Sentry? #17119

Closed

Description

@punkpeye

Problem Statement

In my route handles, I throw Response objects like:

throw Response.json(
    {
      error: {
        code,
        details,
        id: sentryErrorForeignId,
        message,
      },
    } satisfies ApiErrorResponse,
    {
      headers,
      status,
    },
  );

It works as expected with Fastify, but the problem is that Sentry is catching these as if they are errors.

I get lots of these issues, like:

handleFastifyError.handleFastifyError
Level: Error
[object Response]
GET /api/gateway/v1/models

with this stack trace:

… es/.pnpm/@sentry+node@9.40.0/node_modules/@sentry/node/src/integrations/tracing/fastify/index.ts in handleFastifyError.handleFastifyError at line 87:5
… es/.pnpm/@sentry+node@9.40.0/node_modules/@sentry/node/src/integrations/tracing/fastify/index.ts in <anonymous> at line 123:24
node:diagnostics_channel in Channel.publish at line 150:9
/srv/node_modules/.pnpm/fastify@5.4.0/node_modules/fastify/lib/wrapThenable.js at line 55:22

node:internal/process/task_queues in process.processTicksAndRejections at line 105:5

https://glama.sentry.io/issues/6760827050/?project=4507975514849280&query=is%3Aunresolved&referrer=issue-stream

I am already returning false from shouldHandleError:

setupFastifyErrorHandler(app, {
    shouldHandleError: () => {
      // We are handling errors ourselves.
      // We are only using `setupFastifyErrorHandler` to setup request isolation context.
      return false;
    },
  });

  app.setErrorHandler((error, request, reply) => {
    return replyWithErrorPage({ error, origin: 'setErrorHandler', reply });
  });

Solution Brainstorm

Come to think about it, this is likely a bug.

Or maybe the ask is that at the moment setupFastifyErrorHandler is overloaded with error handling logic and also logic for starting a context for every request. It would be nice to separate out those two.

Activity

  1. moved this to Waiting for: Product Owner in GitHub Issues with 👀 3on Jul 22, 2025
  2. punkpeye commented on Jul 22, 2025

    @punkpeye
    Author

    Looking at the source code, this should never report. So this is a bug.

    if (shouldHandleError(error, request, reply)) {
    captureException(error);
    }

  3. punkpeye commented on Jul 22, 2025

    @punkpeye
    Author

    Reading the source code of @sentry/node/src/integrations/tracing/fastify/index.ts is making me realize that I don't need to add setupFastifyErrorHandler as it is automatically added by the instrumentation. However, the default instrumentation does not allow to configure shouldHandleError, so it seems I should remove the default integration and add it manually as needed.

  4. punkpeye commented on Jul 22, 2025

    @punkpeye
    Author

    Maybe log a warning if user uses setupFastifyErrorHandler if there is an existing instrumentation?

  5. Lms24 commented on Jul 22, 2025

    @Lms24
    Member

    Maybe log a warning if user uses setupFastifyErrorHandler if there is an existing instrumentation?

    This should already be logged out if you have debug: true enabled:

    debug.warn(
    'Fastify error handler was already registered via diagnostics channel.',
    'You can safely remove `setupFastifyErrorHandler` call and set `shouldHandleError` on the integration options.',
    );

    However, the default instrumentation does not allow to configure shouldHandleError, so it seems I should remove the default integration and add it manually as needed.

    This is still true for the moment but we're in the process of merging the PRs to expose shouldHandleError on the fastifyIntegration as an option. Expect this to be released by ~tomorrow.

    Gonna try to reproduce the original issue, will report back

  6. moved this from Waiting for: Product Owner to No status in GitHub Issues with 👀 3on Jul 22, 2025
  7. kblcuk commented on Jul 29, 2025

    @kblcuk

    @Lms24 any update on this? At least release that happened 20 hours ago didn't seem to have anything related to this issue.

  8. moved this to Waiting for: Product Owner in GitHub Issues with 👀 3on Jul 29, 2025
  9. self-assigned this
    on Jul 29, 2025
  10. 6 remaining items

  11. moved this from Waiting for: Community to Waiting for: Product Owner in GitHub Issues with 👀 3on Jul 29, 2025
  12. Lms24 commented on Jul 29, 2025

    @Lms24
    Member

    No worries, I realize we should have announced the release in this thread. Usually, we have automations for stuff like this but they didn't work correctly in this issue.

  13. moved this from Waiting for: Product Owner to No status in GitHub Issues with 👀 3on Jul 29, 2025
  14. Lms24 commented on Jul 29, 2025

    @Lms24
    Member

    @punkpeye given 9.41.0 is this issue still relevant from your end?

    Maybe log a warning if user uses setupFastifyErrorHandler if there is an existing instrumentation?

    We do log warnings now if we encounter setupFastifyErrorHandler usage, see:

    debug.warn(
    'Fastify error handler was already registered via diagnostics channel.',
    'You can safely remove `setupFastifyErrorHandler` call and set `shouldHandleError` on the integration options.',
    );

    Let me know if we should keep the issue open, otherwise it'll be closed. Thanks!

  15. moved this to Waiting for: Community in GitHub Issues with 👀 3on Jul 29, 2025
  16. punkpeye commented on Jul 29, 2025

    @punkpeye
    Author

    Personally, I am unblocked. Thank you

  17. moved this from Waiting for: Community to Waiting for: Product Owner in GitHub Issues with 👀 3on Jul 29, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions