Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NestJS error handler doesn't support non-HTTP contexts #11877

Closed
3 tasks done
sgarner opened this issue May 2, 2024 · 1 comment · Fixed by #11880
Closed
3 tasks done

NestJS error handler doesn't support non-HTTP contexts #11877

sgarner opened this issue May 2, 2024 · 1 comment · Fixed by #11880

Comments

@sgarner
Copy link

sgarner commented May 2, 2024

Is there an existing issue for this?

How do you use Sentry?

Sentry Saas (sentry.io)

Which SDK are you using?

@sentry/node

SDK Version

8.0.0-beta.6

Framework Version

No response

Link to Sentry event

No response

SDK Setup

No response

Steps to Reproduce

In this code for the setupNestErrorHandler, an interceptor is created. It wants to get an HTTP request from the execution context and when the request has a route property it uses that to set the transaction name:

export function setupNestErrorHandler(app: MinimalNestJsApp, baseFilter: NestJsErrorFilter): void {
app.useGlobalInterceptors({
intercept(context, next) {
if (getIsolationScope() === getDefaultIsolationScope()) {
logger.warn('Isolation scope is still the default isolation scope, skipping setting transactionName.');
return next.handle();
}
const req = context.switchToHttp().getRequest();
if (req.route) {
getIsolationScope().setTransactionName(`${req.method?.toUpperCase() || 'GET'} ${req.route.path}`);
}
return next.handle();
},
});

However, this assumes the NestJS app is only operating as an HTTP server. NestJS execution contexts come in different varieties when the app is being used for microservices or as a GraphQL server. These contexts may not have a request, and this is not respected by the handler.

Expected Result

The interceptor should check if it is in an HTTP execution context before inspecting the request or route.

if (context.getType() === 'http') {
  const req = context.switchToHttp().getRequest();
  // etc
}

Actual Result

In non-HTTP contexts, an error occurs in the interceptor:

TypeError: Cannot read properties of undefined (reading 'route')

See #5578 (comment)

@mydea
Copy link
Member

mydea commented May 3, 2024

Thank you for reporting this in such detail! I opened a PR to fix this here: #11880

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

2 participants