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
Show file tree
Hide file tree
Changes from 1 commit
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
17 changes: 11 additions & 6 deletions packages/node/src/integrations/tracing/nest/nest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 { ExpectedException, MinimalNestJsApp, NestJsErrorFilter } from './types';
import type { MinimalNestJsApp, NestJsErrorFilter } from './types';

const INTEGRATION_NAME = 'Nest';

Expand Down Expand Up @@ -87,11 +87,16 @@ export function setupNestErrorHandler(app: MinimalNestJsApp, baseFilter: NestJsE
const originalCatch = Reflect.get(target, prop, receiver);

return (exception: unknown, host: unknown) => {
const status_code = (exception as ExpectedException).status;
const error_property = (exception as ExpectedException).error;

// don't report expected errors
if (status_code !== undefined || error_property !== undefined) {
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;

/*
Don't report expected NestJS control flow errors
- `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.

return originalCatch.apply(target, [exception, host]);
}

Expand Down
10 changes: 0 additions & 10 deletions packages/node/src/integrations/tracing/nest/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,6 @@ export interface MinimalNestJsApp {
}) => void;
}

/**
* Interface for an expected exception in nest.
*
* Expected exceptions in nest are HttpExceptions (have a status property) or RpcExceptions (have an error property).
*/
export interface ExpectedException {
status?: number;
error?: string | object;
}

/**
* A minimal interface for an Observable.
*/
Expand Down
Loading