Skip to content

Commit

Permalink
fix: dont send enoent for routes under the excluded path
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilmysliwiec committed Jan 20, 2025
1 parent 96b7d2f commit 2cbaaaa
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
13 changes: 10 additions & 3 deletions lib/loaders/express.loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,19 @@ export class ExpressLoader extends AbstractLoader {
app.get(options.renderPath, renderFn);
}

// eslint-disable-next-line @typescript-eslint/no-unused-vars
app.use((err: any, _req: any, _res: any, _next: Function) => {
app.use((err: any, req: any, _res: any, next: Function) => {
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') || err?.code === 'ENOENT') {
} else if (err?.message?.includes('ENOENT')) {
throw new NotFoundException(err.message);
} else if (err?.code === 'ENOENT') {
throw new NotFoundException(`ENOENT: ${err.message}`);
}
});
});
Expand Down
13 changes: 12 additions & 1 deletion tests/e2e/express-adapter.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,18 @@ describe('Express adapter', () => {
return request(server)
.get('/404')
.expect(404)
.expect(/Not Found/);
.expect(/Not Found/)
.expect(/ENOENT/);
});
});

describe('when trying to hit a non-existing route under the excluded path', () => {
it('should return 404', async () => {
return request(server)
.get('/api/404')
.expect(404)
.expect(/Not Found/)
.expect(/Cannot GET \/api\/404/);
});
});

Expand Down

0 comments on commit 2cbaaaa

Please sign in to comment.