nitrojs / nitrojs/nitro

v3: Nitro doesnt send correct response when throwing errors

Open
#4,183 4 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug v3
Dominant language
TypeScript
Stars
11.2k
Forks
899
Avg merge
2d 24m
Merged PRs (30d)
40

Description

Reproduction

https://github.com/MickL/nitro-ofetch-bug

Describe the bug

When throwing an error, the frontend does never receive a response, e.g.:

import {defineHandler, HTTPError} from "nitro";

export default defineHandler((event) => {
    throw new HTTPError({
        status: 401,
        message: 'hello world',
    });
});

In separate frontend app:

import { ofetch } from "ofetch";

await ofetch(`http://localhost:3000/hello`, {
      onRequest(context) {
        console.log('onRequest');
      },
      onRequestError(context) {
        console.log(context.error);
        console.log('onRequestError');
      },
      onResponse(context) {
        // This never gets called <------
        console.log('onResponse: ' + context.response.status);
      },
      onResponseError(context) {
        // This never gets called <------
        console.log('onResponseError: ' + context.response.status);
      },
});

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the linked reproduction at https://github.com/MickL/nitro-ofetch-bug and trace Nitro's handling of the HTTPError thrown by defineHandler. Verify the frontend receives the server response and that the relevant ofetch response callback is invoked with the 401 status and error message shown in the issue.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
api, backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.