From 4b4ef2ae2516f568048dd0e0c807a4b111bce30a Mon Sep 17 00:00:00 2001 From: Lukas Stracke Date: Fri, 12 Sep 2025 14:45:21 +0200 Subject: [PATCH 1/3] ref(node): Adjust mechanism of `express`, `hapi` and `fastify` error handlers --- .../test-applications/node-express/tests/errors.test.ts | 7 ++++++- .../test-applications/node-fastify-5/tests/errors.test.ts | 7 ++++++- .../test-applications/node-hapi/tests/errors.test.ts | 7 ++++++- packages/node/src/integrations/tracing/express.ts | 2 +- packages/node/src/integrations/tracing/fastify/index.ts | 2 +- packages/node/src/integrations/tracing/hapi/index.ts | 2 +- 6 files changed, 21 insertions(+), 6 deletions(-) diff --git a/dev-packages/e2e-tests/test-applications/node-express/tests/errors.test.ts b/dev-packages/e2e-tests/test-applications/node-express/tests/errors.test.ts index 00f9f413906f..3a3c821a927d 100644 --- a/dev-packages/e2e-tests/test-applications/node-express/tests/errors.test.ts +++ b/dev-packages/e2e-tests/test-applications/node-express/tests/errors.test.ts @@ -11,7 +11,12 @@ test('Sends correct error event', async ({ baseURL }) => { const errorEvent = await errorEventPromise; expect(errorEvent.exception?.values).toHaveLength(1); - expect(errorEvent.exception?.values?.[0]?.value).toBe('This is an exception with id 123'); + const exception = errorEvent.exception?.values?.[0]; + expect(exception?.value).toBe('This is an exception with id 123'); + expect(exception?.mechanism).toEqual({ + type: 'auto.middleware.express', + handled: false, + }); expect(errorEvent.request).toMatchObject({ method: 'GET', diff --git a/dev-packages/e2e-tests/test-applications/node-fastify-5/tests/errors.test.ts b/dev-packages/e2e-tests/test-applications/node-fastify-5/tests/errors.test.ts index cf1428b7a34a..a25f990748ea 100644 --- a/dev-packages/e2e-tests/test-applications/node-fastify-5/tests/errors.test.ts +++ b/dev-packages/e2e-tests/test-applications/node-fastify-5/tests/errors.test.ts @@ -11,7 +11,12 @@ test('Sends correct error event', async ({ baseURL }) => { const errorEvent = await errorEventPromise; expect(errorEvent.exception?.values).toHaveLength(1); - expect(errorEvent.exception?.values?.[0]?.value).toBe('This is an exception with id 123'); + const exception = errorEvent.exception?.values?.[0]; + expect(exception?.value).toBe('This is an exception with id 123'); + expect(exception?.mechanism).toEqual({ + type: 'auto.function.fastify', + handled: false, + }); expect(errorEvent.request).toEqual({ method: 'GET', diff --git a/dev-packages/e2e-tests/test-applications/node-hapi/tests/errors.test.ts b/dev-packages/e2e-tests/test-applications/node-hapi/tests/errors.test.ts index 4f2fba52aa95..39edc8bcde0e 100644 --- a/dev-packages/e2e-tests/test-applications/node-hapi/tests/errors.test.ts +++ b/dev-packages/e2e-tests/test-applications/node-hapi/tests/errors.test.ts @@ -22,7 +22,12 @@ test('Sends thrown error to Sentry', async ({ baseURL }) => { }); expect(errorEvent.exception?.values).toHaveLength(1); - expect(errorEvent.exception?.values?.[0]?.value).toBe('This is an error'); + const exception = errorEvent.exception?.values?.[0]; + expect(exception?.value).toBe('This is an error'); + expect(exception?.mechanism).toEqual({ + type: 'auto.function.hapi', + handled: false, + }); expect(errorEvent.request).toEqual({ method: 'GET', diff --git a/packages/node/src/integrations/tracing/express.ts b/packages/node/src/integrations/tracing/express.ts index fed0a65cfd0b..e645cc24d31e 100644 --- a/packages/node/src/integrations/tracing/express.ts +++ b/packages/node/src/integrations/tracing/express.ts @@ -130,7 +130,7 @@ export function expressErrorHandler(options?: ExpressHandlerOptions): ExpressErr const shouldHandleError = options?.shouldHandleError || defaultShouldHandleError; if (shouldHandleError(error)) { - const eventId = captureException(error, { mechanism: { type: 'middleware', handled: false } }); + const eventId = captureException(error, { mechanism: { type: 'auto.middleware.express', handled: false } }); (res as { sentry?: string }).sentry = eventId; } diff --git a/packages/node/src/integrations/tracing/fastify/index.ts b/packages/node/src/integrations/tracing/fastify/index.ts index 65d783eb8be7..084c50a6957e 100644 --- a/packages/node/src/integrations/tracing/fastify/index.ts +++ b/packages/node/src/integrations/tracing/fastify/index.ts @@ -132,7 +132,7 @@ function handleFastifyError( } if (shouldHandleError(error, request, reply)) { - captureException(error, { mechanism: { handled: false, type: 'fastify' } }); + captureException(error, { mechanism: { handled: false, type: 'auto.function.fastify' } }); } } diff --git a/packages/node/src/integrations/tracing/hapi/index.ts b/packages/node/src/integrations/tracing/hapi/index.ts index eaa8d9c56ad9..6e5ea4dcc8a2 100644 --- a/packages/node/src/integrations/tracing/hapi/index.ts +++ b/packages/node/src/integrations/tracing/hapi/index.ts @@ -54,7 +54,7 @@ function isErrorEvent(event: unknown): event is RequestEvent { function sendErrorToSentry(errorData: object): void { captureException(errorData, { mechanism: { - type: 'hapi', + type: 'auto.function.hapi', handled: false, data: { function: 'hapiErrorPlugin', From 82dd716770ef38cbfd1aa427d5d14b59d1ff5060 Mon Sep 17 00:00:00 2001 From: Lukas Stracke Date: Fri, 12 Sep 2025 16:50:00 +0200 Subject: [PATCH 2/3] fix tests --- .../suites/express/handle-error-scope-data-loss/test.ts | 6 +++--- .../suites/express/handle-error-tracesSampleRate-0/test.ts | 2 +- .../express/handle-error-tracesSampleRate-unset/test.ts | 2 +- .../suites/express/handle-error/test.ts | 2 +- packages/node/src/integrations/tracing/hapi/index.ts | 3 --- 5 files changed, 6 insertions(+), 9 deletions(-) diff --git a/dev-packages/node-integration-tests/suites/express/handle-error-scope-data-loss/test.ts b/dev-packages/node-integration-tests/suites/express/handle-error-scope-data-loss/test.ts index 61291f86320d..cb5d20760c00 100644 --- a/dev-packages/node-integration-tests/suites/express/handle-error-scope-data-loss/test.ts +++ b/dev-packages/node-integration-tests/suites/express/handle-error-scope-data-loss/test.ts @@ -22,7 +22,7 @@ test('withScope scope is NOT applied to thrown error caught by global handler', values: [ { mechanism: { - type: 'middleware', + type: 'auto.middleware.express', handled: false, }, type: 'Error', @@ -61,7 +61,7 @@ test('http requestisolation scope is applied to thrown error caught by global ha values: [ { mechanism: { - type: 'middleware', + type: 'auto.middleware.express', handled: false, }, type: 'Error', @@ -109,7 +109,7 @@ test('withIsolationScope scope is NOT applied to thrown error caught by global h values: [ { mechanism: { - type: 'middleware', + type: 'auto.middleware.express', handled: false, }, type: 'Error', diff --git a/dev-packages/node-integration-tests/suites/express/handle-error-tracesSampleRate-0/test.ts b/dev-packages/node-integration-tests/suites/express/handle-error-tracesSampleRate-0/test.ts index 1f434ebc0971..b8cc2cf53d34 100644 --- a/dev-packages/node-integration-tests/suites/express/handle-error-tracesSampleRate-0/test.ts +++ b/dev-packages/node-integration-tests/suites/express/handle-error-tracesSampleRate-0/test.ts @@ -13,7 +13,7 @@ test('should capture and send Express controller error with txn name if tracesSa values: [ { mechanism: { - type: 'middleware', + type: 'auto.middleware.express', handled: false, }, type: 'Error', diff --git a/dev-packages/node-integration-tests/suites/express/handle-error-tracesSampleRate-unset/test.ts b/dev-packages/node-integration-tests/suites/express/handle-error-tracesSampleRate-unset/test.ts index 4f0dcaae1411..a5f9a1332866 100644 --- a/dev-packages/node-integration-tests/suites/express/handle-error-tracesSampleRate-unset/test.ts +++ b/dev-packages/node-integration-tests/suites/express/handle-error-tracesSampleRate-unset/test.ts @@ -14,7 +14,7 @@ test('should capture and send Express controller error if tracesSampleRate is no values: [ { mechanism: { - type: 'middleware', + type: 'auto.middleware.express', handled: false, }, type: 'Error', diff --git a/dev-packages/node-integration-tests/suites/express/handle-error/test.ts b/dev-packages/node-integration-tests/suites/express/handle-error/test.ts index 0db624160959..4e702360242f 100644 --- a/dev-packages/node-integration-tests/suites/express/handle-error/test.ts +++ b/dev-packages/node-integration-tests/suites/express/handle-error/test.ts @@ -18,7 +18,7 @@ test('should capture and send Express controller error with txn name if tracesSa values: [ { mechanism: { - type: 'middleware', + type: 'auto.middleware.express', handled: false, }, type: 'Error', diff --git a/packages/node/src/integrations/tracing/hapi/index.ts b/packages/node/src/integrations/tracing/hapi/index.ts index 6e5ea4dcc8a2..a8a64c0b0714 100644 --- a/packages/node/src/integrations/tracing/hapi/index.ts +++ b/packages/node/src/integrations/tracing/hapi/index.ts @@ -56,9 +56,6 @@ function sendErrorToSentry(errorData: object): void { mechanism: { type: 'auto.function.hapi', handled: false, - data: { - function: 'hapiErrorPlugin', - }, }, }); } From 1d616a95afe63a83c57a4172f49eb1ed709f6984 Mon Sep 17 00:00:00 2001 From: Lukas Stracke Date: Mon, 15 Sep 2025 11:35:18 +0200 Subject: [PATCH 3/3] changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2e273e367111..7cbb0281ac31 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,6 +21,7 @@ - ref(core): Adjust MCP server error event `mechanism` ([#17622](https://github.com/getsentry/sentry-javascript/pull/17622)) - ref(core): Simplify `linkedErrors` mechanism logic ([#17600](https://github.com/getsentry/sentry-javascript/pull/17600)) - ref(nextjs): Set more specific event `mechanism`s ([#17543](https://github.com/getsentry/sentry-javascript/pull/17543)) + - ref(node): Adjust mechanism of express, hapi and fastify error handlers ([#17623](https://github.com/getsentry/sentry-javascript/pull/17623)) - ref(node-core): Add `mechanism` to cron instrumentations ([#17544](https://github.com/getsentry/sentry-javascript/pull/17544)) - ref(node-core): Add more specific `mechanism.type` to worker thread errors from `childProcessIntegration` ([#17578](https://github.com/getsentry/sentry-javascript/pull/17578)) - ref(node): Add mechanism to errors captured via connect and koa integrations ([#17579](https://github.com/getsentry/sentry-javascript/pull/17579))