Skip to content
This repository has been archived by the owner on Apr 6, 2023. It is now read-only.

fix(nuxt): allow responding with custom headers from error.vue #8469

Merged
merged 4 commits into from Oct 26, 2022

Conversation

danielroe
Copy link
Member

πŸ”— Linked issue

follow-up to #7340, resolves nuxt/nuxt#14895

❓ Type of change

  • πŸ“– Documentation (updates to the documentation or readme)
  • 🐞 Bug fix (a non-breaking change that fixes an issue)
  • πŸ‘Œ Enhancement (improving an existing functionality like performance)
  • ✨ New feature (a non-breaking change that adds functionality)
  • ⚠️ Breaking change (fix or feature that would cause existing functionality to change)

πŸ“š Description

This allows responding to an error within error.vue with more than just HTML and the original error code, such as modifying the code or redirecting to a different page.

πŸ“ Checklist

  • I have linked an issue or discussion.
  • I have updated the documentation accordingly.

@danielroe danielroe added bug Something isn't working 🍰 p2-nice-to-have Priority 2: nothing is broken but it's worth addressing labels Oct 25, 2022
@danielroe danielroe requested a review from pi0 October 25, 2022 14:17
@danielroe danielroe self-assigned this Oct 25, 2022
@codesandbox
Copy link

codesandbox bot commented Oct 25, 2022

CodeSandbox logoCodeSandbox logoΒ  Open in CodeSandbox Web Editor | VS Code | VS Code Insiders

@netlify
Copy link

netlify bot commented Oct 25, 2022

βœ… Deploy Preview for nuxt3-docs canceled.

Name Link
πŸ”¨ Latest commit fd9ea43
πŸ” Latest deploy log https://app.netlify.com/sites/nuxt3-docs/deploys/63580a35d11c460008e1e81b

packages/nuxt/src/core/runtime/nitro/error.ts Show resolved Hide resolved
packages/nuxt/src/core/runtime/nitro/error.ts Show resolved Hide resolved
@@ -63,9 +65,13 @@ export default <NitroErrorHandler> async function errorhandler (error: H3Error,
// TODO: Support `message` in template
(errorObject as any).description = errorObject.message
}
html = template(errorObject)
event.res.setHeader('Content-Type', 'text/html;charset=UTF-8')
return event.res.end(template(errorObject))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we please return string? H3 will add content-type too and also handles sending response string. Reducing req/res dependency

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes. though note I just moved this up - this wasn't a change in this pr

Copy link
Member Author

@danielroe danielroe Oct 25, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just tried - are you sure an error handler supports returning value directly? it seems to hang.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can check. But seems strange because it is a nitro API route right?

}

event.res.setHeader('Content-Type', 'text/html;charset=UTF-8')
event.res.end(html)
event.respondWith(new H3Response(await res.text(), {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

respond with API is not stable yet and might change. Let's use raw methods for now. I will have an idea for a way to simplify and standardized response format for nitro...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sounds good!

@pi0 pi0 merged commit 12808f1 into main Oct 26, 2022
@pi0 pi0 deleted the fix/error-headers branch October 26, 2022 08:25
@pi0 pi0 mentioned this pull request Nov 3, 2022
@thijsw
Copy link

thijsw commented Nov 9, 2022

I'd like to use this feature but I'm not sure how to use it? Does anybody might want to share an example of how I can redirect a user on the error page?

Copy link
Member Author

@thijsw Would you create a discussion rather than commenting on a merged PR?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
3.x bug Something isn't working 🍰 p2-nice-to-have Priority 2: nothing is broken but it's worth addressing
Projects
None yet
Development

Successfully merging this pull request may close these issues.

allow setting headers or returning a redirect from error.vue
3 participants