Skip to content

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

@punkpeye

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/@[email protected]/node_modules/@sentry/node/src/integrations/tracing/fastify/index.ts in handleFastifyError.handleFastifyError at line 87:5
… es/.pnpm/@[email protected]/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/[email protected]/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.

Metadata

Metadata

Assignees

Labels

Projects

Status

Waiting for: Product Owner

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions