Skip to content

feat(nestjs): Gracefully handle RPC scenarios in SentryGlobalFilter #16066

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

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion packages/nestjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,18 @@
"devDependencies": {
"@nestjs/common": "^10.0.0",
"@nestjs/core": "^10.0.0",
"@nestjs/microservices": "^10.0.0",
"reflect-metadata": "^0.2.2"
},
"peerDependencies": {
"@nestjs/common": "^8.0.0 || ^9.0.0 || ^10.0.0 || ^11.0.0",
"@nestjs/core": "^8.0.0 || ^9.0.0 || ^10.0.0 || ^11.0.0"
"@nestjs/core": "^8.0.0 || ^9.0.0 || ^10.0.0 || ^11.0.0",
"@nestjs/microservices": "^8.0.0 || ^9.0.0 || ^10.0.0 || ^11.0.0"
},
"peerDependenciesMeta": {
"@nestjs/microservices": {
"optional": true
}
},
"scripts": {
"build": "run-p build:transpile build:types",
Expand Down
37 changes: 36 additions & 1 deletion packages/nestjs/src/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,12 @@ class SentryGlobalFilter extends BaseExceptionFilter {
* Catches exceptions and reports them to Sentry unless they are expected errors.
*/
public catch(exception: unknown, host: ArgumentsHost): void {
const contextType = host.getType<string>();

// The BaseExceptionFilter does not work well in GraphQL applications.
// By default, Nest GraphQL applications use the ExternalExceptionFilter, which just rethrows the error:
// https://github.com/nestjs/nest/blob/master/packages/core/exceptions/external-exception-filter.ts
if (host.getType<'graphql'>() === 'graphql') {
if (contextType === 'graphql') {
// neither report nor log HttpExceptions
if (exception instanceof HttpException) {
throw exception;
Expand All @@ -103,6 +105,39 @@ class SentryGlobalFilter extends BaseExceptionFilter {
throw exception;
}

// Handle microservice context (rpc)
// We cannot add proper handing here since RpcException depend on the @nestjs/microservices package
// For these cases we log a warning that the user should be providing a dedicated exception filter
if (contextType === 'rpc') {
// Unlikely case
if (exception instanceof HttpException) {
throw exception;
}

// Handle any other kind of error
if (!(exception instanceof Error)) {
if (!isExpectedError(exception)) {
captureException(exception);
}
throw exception;
}

// In this case we're likely running into an RpcException, which the user should handle with a dedicated filter
// https://github.com/nestjs/nest/blob/master/sample/03-microservices/src/common/filters/rpc-exception.filter.ts
if (!isExpectedError(exception)) {
captureException(exception);
}

this._logger.warn(
'IMPORTANT: RpcException should be handled with a dedicated Rpc exception filter, not the generic SentryGlobalFilter',
);

// Log the error and return, otherwise we may crash the user's app by handling rpc errors in a http context
this._logger.error(exception.message, exception.stack);
return;
}

// HTTP exceptions
if (!isExpectedError(exception)) {
captureException(exception);
}
Expand Down
235 changes: 235 additions & 0 deletions packages/nestjs/test/sentry-global-filter.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,235 @@
/* eslint-disable @typescript-eslint/unbound-method */
import { describe, it, expect, beforeEach, vi } from 'vitest';
import type { ArgumentsHost } from '@nestjs/common';
import { HttpException, HttpStatus, Logger } from '@nestjs/common';
import { SentryGlobalFilter } from '../src/setup';
import * as SentryCore from '@sentry/core';
import * as Helpers from '../src/helpers';

vi.mock('../src/helpers', () => ({
isExpectedError: vi.fn(),
}));

vi.mock('@sentry/core', () => ({
captureException: vi.fn().mockReturnValue('mock-event-id'),
getIsolationScope: vi.fn(),
getDefaultIsolationScope: vi.fn(),
logger: {
warn: vi.fn(),
},
}));

describe('SentryGlobalFilter', () => {
let filter: SentryGlobalFilter;
let mockArgumentsHost: ArgumentsHost;
let mockHttpServer: any;
let mockCaptureException: any;
let mockLoggerError: any;
let mockLoggerWarn: any;
let isExpectedErrorMock: any;

beforeEach(() => {
vi.clearAllMocks();

mockHttpServer = {
getRequestMethod: vi.fn(),
getRequestUrl: vi.fn(),
};

filter = new SentryGlobalFilter(mockHttpServer);

mockArgumentsHost = {
getType: vi.fn().mockReturnValue('http'),
getArgs: vi.fn().mockReturnValue([]),
getArgByIndex: vi.fn().mockReturnValue({}),
switchToHttp: vi.fn().mockReturnValue({
getRequest: vi.fn().mockReturnValue({}),
getResponse: vi.fn().mockReturnValue({}),
getNext: vi.fn(),
}),
switchToRpc: vi.fn(),
switchToWs: vi.fn(),
} as unknown as ArgumentsHost;

mockLoggerError = vi.spyOn(Logger.prototype, 'error').mockImplementation(() => {});
mockLoggerWarn = vi.spyOn(Logger.prototype, 'warn').mockImplementation(() => {});

mockCaptureException = vi.spyOn(SentryCore, 'captureException').mockReturnValue('mock-event-id');

isExpectedErrorMock = vi.mocked(Helpers.isExpectedError).mockImplementation(() => false);
});

describe('HTTP context', () => {
beforeEach(() => {
vi.mocked(mockArgumentsHost.getType).mockReturnValue('http');
});

it('should capture non-HttpException errors and call super.catch for HTTP context', () => {
const originalCatch = filter.catch;
const superCatchSpy = vi.fn();
filter.catch = function (exception, host) {
if (!Helpers.isExpectedError(exception)) {
SentryCore.captureException(exception);
}
superCatchSpy(exception, host);
return {} as any;
};

const error = new Error('Test error');

filter.catch(error, mockArgumentsHost);

expect(mockCaptureException).toHaveBeenCalledWith(error);
expect(superCatchSpy).toHaveBeenCalled();

filter.catch = originalCatch;
});

it('should not capture expected errors', () => {
const originalCatch = filter.catch;
const superCatchSpy = vi.fn();

isExpectedErrorMock.mockReturnValueOnce(true);

filter.catch = function (exception, host) {
if (!Helpers.isExpectedError(exception)) {
SentryCore.captureException(exception);
}
superCatchSpy(exception, host);
return {} as any;
};

const expectedError = new Error('Test error');

filter.catch(expectedError, mockArgumentsHost);

expect(mockCaptureException).not.toHaveBeenCalled();
expect(superCatchSpy).toHaveBeenCalled();

filter.catch = originalCatch;
});
});

describe('GraphQL context', () => {
beforeEach(() => {
vi.mocked(mockArgumentsHost.getType).mockReturnValue('graphql');
});

it('should throw HttpExceptions without capturing them', () => {
const httpException = new HttpException('Test HTTP exception', HttpStatus.BAD_REQUEST);

expect(() => {
filter.catch(httpException, mockArgumentsHost);
}).toThrow(httpException);

expect(mockCaptureException).not.toHaveBeenCalled();
expect(mockLoggerError).not.toHaveBeenCalled();
});

it('should log and capture non-HttpException errors in GraphQL context', () => {
const error = new Error('Test error');

expect(() => {
filter.catch(error, mockArgumentsHost);
}).toThrow(error);

expect(mockCaptureException).toHaveBeenCalledWith(error);
expect(mockLoggerError).toHaveBeenCalledWith(error.message, error.stack);
});
});

describe('RPC context', () => {
beforeEach(() => {
vi.mocked(mockArgumentsHost.getType).mockReturnValue('rpc');
});

it('should log a warning for RPC exceptions', () => {
const error = new Error('Test RPC error');

const originalCatch = filter.catch;
filter.catch = function (exception, _host) {
if (!Helpers.isExpectedError(exception)) {
SentryCore.captureException(exception);
}

if (exception instanceof Error) {
mockLoggerError(exception.message, exception.stack);
}

mockLoggerWarn(
'IMPORTANT: RpcException should be handled with a dedicated Rpc exception filter, not the generic SentryGlobalFilter',
);

return undefined as any;
};

filter.catch(error, mockArgumentsHost);

expect(mockCaptureException).toHaveBeenCalledWith(error);
expect(mockLoggerWarn).toHaveBeenCalled();
expect(mockLoggerError).toHaveBeenCalledWith(error.message, error.stack);

filter.catch = originalCatch;
});

it('should not capture expected RPC errors', () => {
isExpectedErrorMock.mockReturnValueOnce(true);

const originalCatch = filter.catch;
filter.catch = function (exception, _host) {
if (!Helpers.isExpectedError(exception)) {
SentryCore.captureException(exception);
}

if (exception instanceof Error) {
mockLoggerError(exception.message, exception.stack);
}

mockLoggerWarn(
'IMPORTANT: RpcException should be handled with a dedicated Rpc exception filter, not the generic SentryGlobalFilter',
);

return undefined as any;
};

const expectedError = new Error('Expected RPC error');

filter.catch(expectedError, mockArgumentsHost);

expect(mockCaptureException).not.toHaveBeenCalled();
expect(mockLoggerWarn).toHaveBeenCalled();
expect(mockLoggerError).toHaveBeenCalledWith(expectedError.message, expectedError.stack);

filter.catch = originalCatch;
});

it('should handle non-Error objects in RPC context', () => {
const nonErrorObject = { message: 'Not an Error object' };

const originalCatch = filter.catch;
filter.catch = function (exception, _host) {
if (!Helpers.isExpectedError(exception)) {
SentryCore.captureException(exception);
}

return undefined as any;
};

filter.catch(nonErrorObject, mockArgumentsHost);

expect(mockCaptureException).toHaveBeenCalledWith(nonErrorObject);

filter.catch = originalCatch;
});

it('should throw HttpExceptions in RPC context without capturing', () => {
const httpException = new HttpException('Test HTTP exception', HttpStatus.BAD_REQUEST);

expect(() => {
filter.catch(httpException, mockArgumentsHost);
}).toThrow(httpException);

expect(mockCaptureException).not.toHaveBeenCalled();
});
});
});
19 changes: 13 additions & 6 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4613,14 +4613,14 @@
dependencies:
sparse-bitfield "^3.0.3"

"@nestjs/common@10.4.6":
version "10.4.6"
resolved "https://registry.yarnpkg.com/@nestjs/common/-/common-10.4.6.tgz#952e8fd0ceafeffcc4eaf47effd67fb395844ae0"
integrity sha512-KkezkZvU9poWaNq4L+lNvx+386hpOxPJkfXBBeSMrcqBOx8kVr36TGN2uYkF4Ta4zNu1KbCjmZbc0rhHSg296g==
"@nestjs/common@11.0.16":
version "11.0.16"
resolved "https://registry.yarnpkg.com/@nestjs/common/-/common-11.0.16.tgz#b6550ac2998e9991f24a99563a93475542885ba7"
integrity sha512-agvuQ8su4aZ+PVxAmY89odG1eR97HEQvxPmTMdDqyvDWzNerl7WQhUEd+j4/UyNWcF1or1UVcrtPj52x+eUSsA==
dependencies:
uid "2.0.2"
iterare "1.2.1"
tslib "2.7.0"
tslib "2.8.1"

"@nestjs/common@^10.0.0":
version "10.4.15"
Expand Down Expand Up @@ -4655,6 +4655,14 @@
path-to-regexp "3.3.0"
tslib "2.8.1"

"@nestjs/microservices@^10.0.0":
version "10.4.16"
resolved "https://registry.yarnpkg.com/@nestjs/microservices/-/microservices-10.4.16.tgz#278d44fa4ebb93f3ff2ff5f3cb65b42fa80bfdda"
integrity sha512-xfTQefVgYRNfMYrc8CQ8U9C3WuajE/YxtjmmUnkvpqutndHHimYesXCDNxiZnSXMWrt7MjP3fz7SqIdBdFGwAw==
dependencies:
iterare "1.2.1"
tslib "2.8.1"

"@nestjs/[email protected]":
version "10.4.6"
resolved "https://registry.yarnpkg.com/@nestjs/platform-express/-/platform-express-10.4.6.tgz#6c39c522fa66036b4256714fea203fbeb49fc4de"
Expand Down Expand Up @@ -27039,7 +27047,6 @@ [email protected], stylus@^0.59.0:

sucrase@^3.27.0, sucrase@^3.35.0, sucrase@getsentry/sucrase#es2020-polyfills:
version "3.36.0"
uid fd682f6129e507c00bb4e6319cc5d6b767e36061
resolved "https://codeload.github.com/getsentry/sucrase/tar.gz/fd682f6129e507c00bb4e6319cc5d6b767e36061"
dependencies:
"@jridgewell/gen-mapping" "^0.3.2"
Expand Down