v3: Nitro doesnt send correct response when throwing errors
Open
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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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