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

Cannot track status codes #754

Closed
imslepov opened this issue May 17, 2024 · 0 comments · Fixed by #756
Closed

Cannot track status codes #754

imslepov opened this issue May 17, 2024 · 0 comments · Fixed by #756

Comments

@imslepov
Copy link
Contributor

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 returns res.statusCode equal to 200
  • onAfterResponse 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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant