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 4xx errors #12695

Merged
merged 9 commits into from
Jul 1, 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
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ export class AppController1 {
return this.appService.testException(id);
}

@Get("test-expected-exception/:id")
async testExpectedException(@Param("id") id: string) {
return this.appService.testExpectedException(id);
}

@Get('test-outgoing-fetch-external-allowed')
async testOutgoingFetchExternalAllowed() {
return this.appService.testOutgoingFetchExternalAllowed();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Injectable } from '@nestjs/common';
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
import * as Sentry from '@sentry/nestjs';
import { makeHttpRequest } from './utils';

Expand Down Expand Up @@ -52,6 +52,10 @@ export class AppService1 {
throw new Error(`This is an exception with id ${id}`);
}

testExpectedException(id: string) {
throw new HttpException(`This is an expected exception with id ${id}`, HttpStatus.FORBIDDEN);
}

async testOutgoingFetchExternalAllowed() {
const fetchResponse = await fetch('http://localhost:3040/external-allowed');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ test('Sends exception to Sentry', async ({ baseURL }) => {

const errorEvent = await errorEventPromise;

console.log("UNEXPECTED EXCEPTION");
console.log(errorEvent);

Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
console.log("UNEXPECTED EXCEPTION");
console.log(errorEvent);

expect(errorEvent.exception?.values).toHaveLength(1);
expect(errorEvent.exception?.values?.[0]?.value).toBe('This is an exception with id 123');

Expand All @@ -28,3 +31,19 @@ test('Sends exception to Sentry', async ({ baseURL }) => {
span_id: expect.any(String),
});
});

test('Does not send expected exception to Sentry', async ({ baseURL }) => {
const errorEventPromise = waitForError('nestjs', event => {
return !event.type && event.exception?.values?.[0]?.value === 'This is an unexpected exception with id 123';
});

const response = await fetch(`${baseURL}/test-expected-exception/123`);
expect(response.status).toBe(403);

const errorEvent = await errorEventPromise;

console.log("EXPECTED EXCEPTION");
console.log(errorEvent);

Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
console.log("EXPECTED EXCEPTION");
console.log(errorEvent);

I guess a leftover of debugging? :D

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes! Sorry the test was not done yet, I just pushed for debugging. Should have put it back into draft mode :)

expect(errorEvent.exception?.values).toHaveLength(1);
});
Loading