Error handling x middleware regression since 2.1.3
See original GitHub issue👋 In versions 2.1.3 and beyond, there is a regression in how errors interact with middleware…
In version 2.1.2 and prior, if an error occurred in a route and app.onError
was invoked, middleware would still be processed for the error response. This would allow middleware such as the built-in logger
and built-in cors
to still process the error response, logging the response or injecting CORS headers.
In version 2.1.3 and since, if an error occurs and app.onError
responds, middleware does not have a chance to process the response, so middleware such as logger
cannot log the response, and cors
cannot inject any CORS headers.
const { Hono } = require('hono');
const { logger } = require('hono/logger');
const { cors } = require('hono/cors');
const app = new Hono();
app.use('*', logger());
app.use('*', cors({ origin: '*' }));
app.get('/error', () => {
throw new Error('Test error');
});
app.onError((err, ctx) => {
return ctx.json({
error: true,
status: 500,
message: err.message,
});
});
const main = async () => {
const res = await app.request('http://localhost/error');
console.log([ ...res.headers.entries() ]);
};
main();
This is pretty breaking for my project, as I would like to have error responses logged, and I’d like them to contain CORS headers like any other response.
Issue Analytics
- State:
- Created a year ago
- Comments:6
Top GitHub Comments
Hi @MattIPv4 !
Sorry! Fixed it and release a new version, “v2.2.5”. Please try it and if you still have problems, tell us again!
Hey! It looks like it’s been fixed for synchronous routes 🎉, but the issue still remains for any route that is asynchronous 😢
2.1.2
2.2.4