Skip to content

Context Not Propagated for Unhandled Exceptions in withScope #14839

Closed
@AB-291

Description

@AB-291

Is there an existing issue for this?

How do you use Sentry?

Sentry Saas (sentry.io)

Which SDK are you using?

@sentry/nestjs

SDK Version

8.47.0

Framework Version

Nestjs 10.4.2

Link to Sentry event

No response

Reproduction Example/SDK Setup

import { Catch, ArgumentsHost, HttpException } from '@nestjs/common';
import { BaseExceptionFilter } from '@nestjs/core';
import { SentryExceptionCaptured } from '@sentry/nestjs';
import * as Sentry from '@sentry/nestjs';

@Catch()
export class SentryFilter extends BaseExceptionFilter {
  @SentryExceptionCaptured()
  catch(exception: unknown, host: ArgumentsHost) {
    const ctx = host.switchToHttp();
    const request = ctx.getRequest();

    Sentry.withScope((scope) => {
      // Add request details
      if (request) {
        scope.setContext('Request', {
          url: request.url,
          method: request.method,
          headers: request.headers,
          body: request.body,
          query: request.query,
          params: request.params,
        });
        scope.setUser(request.user);
      }

      if (exception instanceof HttpException) {
        const status = exception.getStatus();
        const exceptionResponse = exception.getResponse();

        scope.setLevel(status >= 500 ? 'error' : 'warning');
        scope.setContext('Error Details', {
          message: exception.message,
          status,
          response: exceptionResponse,
          stack: exception.stack,
        });

        Sentry.captureException(exception);
      } else {
        scope.setLevel('error');
        scope.setContext('Error Details', {
          message: (exception as any).message || 'Unhandled Exception',
          stack: (exception as any).stack || 'No stack trace available',
        });

        Sentry.captureException(exception);
      }
    });

    // Continue default NestJS handling
    super.catch(exception, host);
  }
}

Steps to Reproduce

  1. For handled exceptions (e.g., when throwing HttpException), the request context (URL, method, headers, etc.) and user details are captured and displayed correctly in Sentry.
  2. For unhandled exceptions (e.g., unexpected errors or rejected promises), the context does not propagate even though I explicitly set it in Sentry.withScope.

Expected Result

The request and user context attached in Sentry.withScope should propagate correctly for unhandled exceptions and appear in the Sentry logs.

Actual Result

I have debugged the issue and confirmed the following:

  • The context (e.g., request details) is successfully attached to the scope in the Sentry.withScope block.
  • However, when Sentry.captureException is called, the context does not appear in the logs on Sentry’s dashboard.

What I have tried:

  • I added middleware to attach request details globally to Sentry.configureScope, but the issue persists for unhandled exceptions.
  • I verified that my Sentry configuration is correct and that @sentry/node and @sentry/nestjs are using the latest versions.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Package: nestjsIssues related to the Sentry Nestjs SDK

    Type

    Projects

    Status

    Waiting for: Product Owner

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions