Cannot track status codes
imslepov opened this issue · comments
Environment
- Node Version: v20.12.2
- H3 Version: 1.11.1
Reproduction
See https://stackblitz.com/edit/github-d9z5tl?file=app.ts
Describe the bug
Forwarded from nuxt/nuxt#27248
When I return an error in eventHandler
's using createError
, global hooks onBeforeResponse
and onAfterResponse
behave unexpectedly:
onBeforeResponse
always returnsres.statusCode
equal to 200onAfterResponse
does not work in case of an error in the handler
This does not track the actual status codes that users may encounter. For example, in my app I cannot collect statistics in Prometheus metrics.
export const app = createApp({
onBeforeResponse: (event) => {
// will always be 200
console.log('[beforeResponse]', event.node.res.statusCode);
},
onAfterResponse: (event) => {
// not called
console.log('[afterResponse]', event.node.res.statusCode);
},
});
const router = createRouter();
app.use(router);
router.get(
'/',
eventHandler((event) => {
return createError({ statusCode: 404 });
})
);
Additional context
No response
Logs
No response