Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
8 changes: 4 additions & 4 deletions lib/loaders/express.loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,14 @@ export class ExpressLoader extends AbstractLoader {
}

app.use((err: any, req: any, _res: any, next: Function) => {
if (err instanceof HttpException) {
throw err;
}

if (isRouteExcluded(req, options.exclude)) {
const method = httpAdapter.getRequestMethod(req);
const url = httpAdapter.getRequestUrl(req);
return next(new NotFoundException(`Cannot ${method} ${url}`));
}

if (err instanceof HttpException) {
throw err;
} else if (err?.message?.includes('ENOENT')) {
throw new NotFoundException(err.message);
} else if (err?.code === 'ENOENT') {
Expand Down
30 changes: 29 additions & 1 deletion tests/e2e/express-adapter.e2e-spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { INestApplication } from '@nestjs/common';
import { INestApplication, PayloadTooLargeException } from '@nestjs/common';
import { NestFactory } from '@nestjs/core';
import { Server } from 'net';
import request from 'supertest';
Expand Down Expand Up @@ -156,4 +156,32 @@ describe('Express adapter', () => {
await app.close();
});
});

describe('when error happens in the previous middleware', () => {
beforeAll(async () => {
app = await NestFactory.create(AppModule.withDefaults(), {
logger: new NoopLogger()
});

app.use((_req, _res, next) => {
next(new PayloadTooLargeException());
});

app.setGlobalPrefix('api');

server = app.getHttpServer();
await app.init();
});

it('should return 413', async () => {
return request(server)
.get('/api')
.expect(413)
.expect(/Payload Too Large/);
});

afterAll(async () => {
await app.close();
});
});
});