Skip to content

Commit

Permalink
fix: pass unhandled error on to next express error handler
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
luddwichr committed Feb 5, 2025
1 parent 678d76c commit 1aa5d8d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/loaders/express.loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ export class ExpressLoader extends AbstractLoader {
throw new NotFoundException(err.message);
} else if (err?.code === 'ENOENT') {
throw new NotFoundException(`ENOENT: ${err.message}`);
} else {
next(err);
}
});
});
Expand Down
18 changes: 18 additions & 0 deletions tests/e2e/express-adapter.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,24 @@ describe('Express adapter', () => {
let server: Server;
let app: INestApplication;

describe('when middleware throws generic error', () => {
beforeAll(async () => {
app = await NestFactory.create(AppModule.withDefaults(), {
logger: new NoopLogger()
});
app.use((_req, _res, next) => next(new Error('Something went wrong')));

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

describe('GET /index.html', () => {
it('should return Iternal Server Error', async () => {
return request(server).get('/index.html').expect(500);
});
});
});

describe('when "fallthrough" option is set to "true"', () => {
beforeAll(async () => {
app = await NestFactory.create(AppModule.withFallthrough(), {
Expand Down

0 comments on commit 1aa5d8d

Please sign in to comment.