-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
feat(nestjs): Filter RPC exceptions #13227
Changes from 6 commits
0faa467
cebcd44
1db607f
da797a7
b1ac229
979d7e7
619e8fe
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,7 +13,7 @@ import type { IntegrationFn, Span } from '@sentry/types'; | |
import { logger } from '@sentry/utils'; | ||
import { generateInstrumentOnce } from '../../../otel/instrument'; | ||
import { SentryNestInstrumentation } from './sentry-nest-instrumentation'; | ||
import type { MinimalNestJsApp, NestJsErrorFilter } from './types'; | ||
import type { ExpectedException, MinimalNestJsApp, NestJsErrorFilter } from './types'; | ||
|
||
const INTEGRATION_NAME = 'Nest'; | ||
|
||
|
@@ -87,10 +87,11 @@ export function setupNestErrorHandler(app: MinimalNestJsApp, baseFilter: NestJsE | |
const originalCatch = Reflect.get(target, prop, receiver); | ||
|
||
return (exception: unknown, host: unknown) => { | ||
const status_code = (exception as { status?: number }).status; | ||
const status_code = (exception as ExpectedException).status; | ||
const error_property = (exception as ExpectedException).error; | ||
|
||
// don't report expected errors | ||
if (status_code !== undefined) { | ||
if (status_code !== undefined || error_property !== undefined) { | ||
return originalCatch.apply(target, [exception, host]); | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. h: Let's go for something like the following here: const exceptionIsObject = typeof exception === 'object' && exception !== null;
const exceptionStatusCode = exceptionIsObject && 'status' in exception ? exception.status : null;
const exceptionErrorProperty = exceptionIsObject && 'error' in exception ? exception.error : null;
// Only report expected errors
// - ExpectedNestJS control flow errors that with status codes like NestJS' `HttpException` errors will have a `status` property.
// - NestJS' expected `RpcExceptions` will have an `error` property
if (exceptionStatusCode === null && exceptionErrorProperty === null) {
captureException(exception, {
mechanism: {
handled: false,
},
});
}
return originalCatch.apply(target, [exception, host]); There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why? It's not immediately obvious to me how that improves the current implementation There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The current implementation will throw if the error is nullish, which can always happen. We should avoid doing any type casts ( Also, we should add documentation. Somebody who comes across the code is basically in the dark what it means to look on the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok sure, thanks for clarifying. Will update |
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This breaks NestJS projects which don't use
@nestjs/microservices
.I've tried creating an issue, but couldn't submit it because GH kept showing "Something went wrong" popup.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for writing in! I am currently working on a fix for the nest SDK and will remove the dependency again as part of that PR. ref