Skip to content

Commit 1aa5d8d

Browse files
committed
fix: pass unhandled error on to next express error handler
Commit 0ae6f54 introduced an error handling middleware. However, it does not delegate to the next error handler if it cannot handle the error adequately. This causes the request to hang until a timeout kills the connection. By passing the error on to next, other error handlers (like the default error handler) handle it.
1 parent 678d76c commit 1aa5d8d

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

lib/loaders/express.loader.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ export class ExpressLoader extends AbstractLoader {
8383
throw new NotFoundException(err.message);
8484
} else if (err?.code === 'ENOENT') {
8585
throw new NotFoundException(`ENOENT: ${err.message}`);
86+
} else {
87+
next(err);
8688
}
8789
});
8890
});

tests/e2e/express-adapter.e2e-spec.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,24 @@ describe('Express adapter', () => {
99
let server: Server;
1010
let app: INestApplication;
1111

12+
describe('when middleware throws generic error', () => {
13+
beforeAll(async () => {
14+
app = await NestFactory.create(AppModule.withDefaults(), {
15+
logger: new NoopLogger()
16+
});
17+
app.use((_req, _res, next) => next(new Error('Something went wrong')));
18+
19+
server = app.getHttpServer();
20+
await app.init();
21+
});
22+
23+
describe('GET /index.html', () => {
24+
it('should return Iternal Server Error', async () => {
25+
return request(server).get('/index.html').expect(500);
26+
});
27+
});
28+
});
29+
1230
describe('when "fallthrough" option is set to "true"', () => {
1331
beforeAll(async () => {
1432
app = await NestFactory.create(AppModule.withFallthrough(), {

0 commit comments

Comments
 (0)