nitro icon indicating copy to clipboard operation
nitro copied to clipboard

Errors thrown in the 'request' hook are silently ignored

Open slavafomin opened this issue 3 weeks ago โ€ข 1 comments

Hello!

When registering a handler for the 'request' runtime hook in a Nitro plugin, any errors thrown (synchronously or asynchronously) within the handler are caught internally and silently ignored. The error is neither logged (in development or production) nor does it propagate to fail the request or trigger the 'error' hook. As a result, the request continues processing as normal, which can lead to unexpected behavior and hard-to-debug issues.

This makes the 'request' hook unreliable for scenarios where an error should abort the request early (e.g., validation failures, authentication checks, or critical setup).

Steps to Reproduce

  1. Create a Nitro plugin (e.g., plugins/error.ts):
export default defineNitroPlugin(nitroApp => {
  nitroApp.hooks.hook('request', event => {
    throw new Error('OOPS!'); // This error is silently swallowed
  });
});
  1. Start the Nitro dev server (npx nitropack dev).
  2. Make any request to the server.

Expected Behavior

The error should:

  • Be logged to the console (definitely)
  • Cause the request to fail with a 500 response (not clear)

Alternatively, clear documentation warning that throwing errors in 'request' (and possibly other hooks) has no effect.

Actual Behavior

  • No error is logged.
  • No 500 response is sent.
  • The request proceeds normally and succeeds (or follows the normal route handling).

This issue can hide critical bugs in production if developers assume throwing in a 'request' hook will fail the request. Suggestion: Either change to propagate errors (with proper handling) or explicitly document the silent swallow behavior for specific hooks like 'request'.

slavafomin avatar Dec 15 '25 18:12 slavafomin

๐Ÿ”— Similar Issues

Related Issues

  • https://github.com/nitrojs/nitro/issues/3811
  • https://github.com/nitrojs/nitro/issues/3872
  • https://github.com/nitrojs/nitro/issues/3631
  • https://github.com/nitrojs/nitro/issues/3499

๐Ÿ‘ค Suggested Assignees


Enable issue planning

To enable issue planning, add the following to your .coderabbit.yaml:

issue_enrichment:
  planning:
    enabled: true

You can then request a plan by commenting @coderabbitai plan on any issue.

๐Ÿงช Issue enrichment is currently in early access.

To disable automatic issue enrichment, add the following to your .coderabbit.yaml:

issue_enrichment:
  auto_enrich:
    enabled: false

coderabbitai[bot] avatar Dec 15 '25 18:12 coderabbitai[bot]

Be logged to the console (definitely)

I think this is an improvement for the DX ๐Ÿ‘

For now, you can catch potential errors with this:

nitroApp.hooks.hook('error', error => {
  console.error('Caught error:', error);
})

kricsleo avatar Dec 17 '25 15:12 kricsleo