From fd10a2b1d39f5079f732546d978fea9f50521612 Mon Sep 17 00:00:00 2001 From: Francesco Novy Date: Fri, 3 May 2024 09:57:57 +0200 Subject: [PATCH] fix(nestjs): Ensure Nest.js interceptor works with non-http context (#11880) Fixes https://github.com/getsentry/sentry-javascript/issues/11877 --- packages/node/src/integrations/tracing/nest.ts | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/packages/node/src/integrations/tracing/nest.ts b/packages/node/src/integrations/tracing/nest.ts index 220b60d68a59..a831a6771179 100644 --- a/packages/node/src/integrations/tracing/nest.ts +++ b/packages/node/src/integrations/tracing/nest.ts @@ -5,6 +5,8 @@ import type { IntegrationFn } from '@sentry/types'; import { logger } from '@sentry/utils'; interface MinimalNestJsExecutionContext { + getType: () => string; + switchToHttp: () => { // minimal request object // according to official types, all properties are required but @@ -57,10 +59,13 @@ export function setupNestErrorHandler(app: MinimalNestJsApp, baseFilter: NestJsE return next.handle(); } - const req = context.switchToHttp().getRequest(); - if (req.route) { - getIsolationScope().setTransactionName(`${req.method?.toUpperCase() || 'GET'} ${req.route.path}`); + if (context.getType() === 'http') { + const req = context.switchToHttp().getRequest(); + if (req.route) { + getIsolationScope().setTransactionName(`${req.method?.toUpperCase() || 'GET'} ${req.route.path}`); + } } + return next.handle(); }, });