-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
Merged
Merged
Changes from 7 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
95f6c3c
add SentryRpcFilter
chargome e9d52ea
add jsdoc
chargome c197522
add rollup entrypoint
chargome cdf559d
revert microservice handling
chargome e4fd6f1
rm npm export
chargome 4c1cb11
remove peer
chargome 55ddeb9
Merge branch 'develop' into cg-nest-rpc-exceptions
chargome 3b5ca3f
remove dev dep
chargome 3e230bd
Merge branch 'develop' into cg-nest-rpc-exceptions
chargome File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.