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

fix: pass unhandled error on to next express error handler #1572

Merged
Merged
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
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