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

feat(nestjs): Filter RPC exceptions #13227

Merged
merged 7 commits into from
Aug 5, 2024
Merged

Conversation

nicohrubec
Copy link
Contributor

@nicohrubec nicohrubec commented Aug 5, 2024

RpcExceptions are always explicitly thrown in nest. Therefore, they are expected for users and should not be sent to Sentry.

This PR filters these exceptions. In @sentry/nestjs we can simply use instanceof RpcExceptions to achieve this. In @sentry/node we do not have access to this class, so we need to check based on a property.

ref

@nicohrubec nicohrubec linked an issue Aug 5, 2024 that may be closed by this pull request
Copy link
Contributor

github-actions bot commented Aug 5, 2024

size-limit report 📦

Path Size
@sentry/browser 22.46 KB (0%)
@sentry/browser (incl. Tracing) 34.24 KB (0%)
@sentry/browser (incl. Tracing, Replay) 70.29 KB (0%)
@sentry/browser (incl. Tracing, Replay) - with treeshaking flags 63.63 KB (0%)
@sentry/browser (incl. Tracing, Replay with Canvas) 74.69 KB (0%)
@sentry/browser (incl. Tracing, Replay, Feedback) 87.29 KB (0%)
@sentry/browser (incl. Tracing, Replay, Feedback, metrics) 89.13 KB (0%)
@sentry/browser (incl. metrics) 26.76 KB (0%)
@sentry/browser (incl. Feedback) 39.39 KB (0%)
@sentry/browser (incl. sendFeedback) 27.08 KB (0%)
@sentry/browser (incl. FeedbackAsync) 31.72 KB (0%)
@sentry/react 25.22 KB (0%)
@sentry/react (incl. Tracing) 37.23 KB (0%)
@sentry/vue 26.6 KB (0%)
@sentry/vue (incl. Tracing) 36.08 KB (0%)
@sentry/svelte 22.59 KB (0%)
CDN Bundle 23.65 KB (0%)
CDN Bundle (incl. Tracing) 35.89 KB (0%)
CDN Bundle (incl. Tracing, Replay) 70.32 KB (0%)
CDN Bundle (incl. Tracing, Replay, Feedback) 75.59 KB (0%)
CDN Bundle - uncompressed 69.4 KB (0%)
CDN Bundle (incl. Tracing) - uncompressed 106.32 KB (0%)
CDN Bundle (incl. Tracing, Replay) - uncompressed 218.17 KB (0%)
CDN Bundle (incl. Tracing, Replay, Feedback) - uncompressed 231.08 KB (0%)
@sentry/nextjs (client) 37.08 KB (0%)
@sentry/sveltekit (client) 34.81 KB (0%)
@sentry/node 114.77 KB (0%)
@sentry/node - without tracing 89.33 KB (0%)
@sentry/aws-serverless 98.5 KB (0%)

@nicohrubec nicohrubec marked this pull request as ready for review August 5, 2024 11:14
@nicohrubec nicohrubec requested review from a team, mydea and lforst and removed request for a team August 5, 2024 11:16
Comment on lines 90 to 97
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]);
}

Copy link
Member

Choose a reason for hiding this comment

The 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]);

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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

Copy link
Member

@lforst lforst Aug 5, 2024

Choose a reason for hiding this comment

The 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 (as) as much as possible. The change I suggested does runtime checks not to throw when nullish values are passed.

Also, we should add documentation. Somebody who comes across the code is basically in the dark what it means to look on the status or error props.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok sure, thanks for clarifying. Will update

@nicohrubec nicohrubec requested a review from lforst August 5, 2024 14:00
- `HttpException` errors will have a `status` property
- `RpcException` errors will have an `error` property
*/
if (exceptionStatusCode !== null || exceptionErrorProperty !== null) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

l: I would flip the condition: We report the error if this and this condition is true. To me the early return is a weird pattern here but I'll leave it up to you.

@nicohrubec nicohrubec merged commit c1052ab into develop Aug 5, 2024
124 checks passed
@nicohrubec nicohrubec deleted the nh/filter-rpc-exceptions branch August 5, 2024 14:29
@@ -11,6 +11,7 @@ import { Catch } from '@nestjs/common';
import { Injectable } from '@nestjs/common';
import { Global, Module } from '@nestjs/common';
import { APP_FILTER, APP_INTERCEPTOR, BaseExceptionFilter } from '@nestjs/core';
import { RpcException } from '@nestjs/microservices';
Copy link

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.

Copy link
Contributor Author

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

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

Successfully merging this pull request may close these issues.

Filter all RpcExceptions in @sentry/nestjs
3 participants