Skip to content

Commit

Permalink
Add local filter test
Browse files Browse the repository at this point in the history
  • Loading branch information
nicohrubec committed Aug 8, 2024
1 parent 1bcaf37 commit 22eadbe
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import { Controller, Get, Param, ParseIntPipe, UseGuards, UseInterceptors } from '@nestjs/common';
import { Controller, Get, Param, ParseIntPipe, UseFilters, UseGuards, UseInterceptors } from '@nestjs/common';
import { flush } from '@sentry/nestjs';
import { AppService } from './app.service';
import { ExampleExceptionGlobalFilter } from './example-global-filter.exception';
import { ExampleExceptionLocalFilter } from './example-local-filter.exception';
import { ExampleLocalFilter } from './example-local.filter';
import { ExampleGuard } from './example.guard';
import { ExampleInterceptor } from './example.interceptor';
import { ExampleExceptionGlobalFilter } from "./example-global-filter.exception";

@Controller()
@UseFilters(ExampleLocalFilter)
export class AppController {
constructor(private readonly appService: AppService) {}

Expand Down Expand Up @@ -80,4 +83,9 @@ export class AppController {
async exampleExceptionGlobalFilter() {
throw new ExampleExceptionGlobalFilter();
}

@Get('example-exception-local-filter')
async exampleExceptionLocalFilter() {
throw new ExampleExceptionLocalFilter();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { ScheduleModule } from '@nestjs/schedule';
import { SentryModule } from '@sentry/nestjs/setup';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { ExampleGlobalFilter } from './example-global.filter';
import { ExampleMiddleware } from './example.middleware';
import {ExampleGlobalFilter} from "./example-global.filter";

@Module({
imports: [SentryModule.forRoot(), ScheduleModule.forRoot()],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ArgumentsHost, BadRequestException, Catch, ExceptionFilter } from '@nestjs/common';
import { Request, Response } from 'express';
import { ExampleExceptionGlobalFilter } from "./example-global-filter.exception";
import { ExampleExceptionGlobalFilter } from './example-global-filter.exception';

@Catch(ExampleExceptionGlobalFilter)
export class ExampleGlobalFilter implements ExceptionFilter {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export class ExampleExceptionLocalFilter extends Error {
constructor() {
super('Original local example exception!');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { ArgumentsHost, BadRequestException, Catch, ExceptionFilter } from '@nestjs/common';
import { Request, Response } from 'express';
import { ExampleExceptionLocalFilter } from './example-local-filter.exception';

@Catch(ExampleExceptionLocalFilter)
export class ExampleLocalFilter implements ExceptionFilter {
catch(exception: BadRequestException, host: ArgumentsHost): void {
const ctx = host.switchToHttp();
const response = ctx.getResponse<Response>();
const request = ctx.getRequest<Request>();
const status = exception.getStatus();
console.log('Example local exception filter!');

response.status(status).json({
statusCode: 400,
timestamp: new Date().toISOString(),
path: request.url,
message: 'Example exception was handled by local filter!',
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,27 @@ test('Does not send RpcExceptions to Sentry', async ({ baseURL }) => {
expect(errorEventOccurred).toBe(false);
});

test('Global exception filter registered in root module is applied', async ({ baseURL }) => {
test('Global exception filter registered in main module is applied', async ({ baseURL }) => {
const response = await fetch(`${baseURL}/example-exception-global-filter`);
const responseBody = await response.json();

console.log(responseBody);

// this should fail but doesn't because the bad request exception filter is not being properly applied
// this should fail but doesn't because the exception filter is not being properly applied
expect(response.status).toBe(500);
expect(responseBody).toEqual({
message: 'Internal server error',
statusCode: 500,
});
});

test('Local exception filter registered in main module is applied', async ({ baseURL }) => {
const response = await fetch(`${baseURL}/example-exception-global-filter`);
const responseBody = await response.json();

console.log(responseBody);

// this should fail but doesn't because the exception filter is not being properly applied
expect(response.status).toBe(500);
expect(responseBody).toEqual({
message: 'Internal server error',
Expand Down

0 comments on commit 22eadbe

Please sign in to comment.