Errors details
The rules, limits and messages behind the Errors guide. The signatures are under createError and the headings after it.
What the visitor sees
| Error | Status | Message and data in production |
|---|---|---|
thrown with createError | its status | as you wrote them |
any other, with a status or statusCode of 400 to 599 | that status | the status text, such as "Conflict", and no data |
| any other | 500 | "Internal Server Error" and no data |
| a URL that nothing matches | 404 | |
| a path whose server routes do not take the method | 405, with an allow header |
- The same holds for the HTML and for the payload. In development the real message and the stack are shown.
- At a handler's URL and at a URL that nothing matches, a request that prefers
text/htmlgets the HTML error page. Any other gets JSON. - An error after the first part of a streamed page was sent cannot change the status.
createErrorwith astatusoutside 400 to 599 makes a 500.- Nuxt's
statusCodeandstatusMessageare not options (HAMUR_W3017).hamur migrate nuxtrewrites them.
Errors that skip a boundary
A route boundary catches what the route's load, middleware, meta.params, meta.validate, search schema and render throw. The error page answers for these instead:
- a
createError({ fatal: true }) - an error of a layout or of the app root
- a malformed path
A redirect, false and abortNavigation() are not errors, and no boundary catches them. A 404 takes the covering not-found component first, then a route boundary. page:error runs before a boundary renders, and in the browser app:error runs too.
notFoundMode
app.notFoundMode says what answers a URL that no page matches, with status 404.
| Value | Answer |
|---|---|
"root" (default) | app/pages/_not-found.<ext>, inside the app root and the default layout; without that file, the error page |
"fuzzy" | the deepest parent page or pathless layout whose path is a prefix of the URL, with the not-found component of its children in its Outlet; with no such page or component, as "root" |
The prerendered 404.html is the root not-found component when the app has one.
Errors in the browser
| Case | What happens |
|---|---|
middleware throws, a load answers an error, a layout is unknown | the error page at the new URL |
| the code of a page cannot be loaded, as after a deploy | a full page load |
an error while rendering that no ErrorBoundary catches | the error page at the URL being rendered |
| an error outside a render, such as in Vue's event handlers | the page stays, except for a createError({ fatal: true }) |
showError(error) | the error page at the current URL; the history does not change |
showError(error) on the server, or while a navigation renders | throws the error, so the error page answers for that URL |
| any successful navigation | clears the error |
The hooks page:error, app:error and app:error:cleared are in the list of app hooks. An app:error hook cannot change what the page shows. A hook that throws is logged, and the others still run.
ErrorBoundary
The props in each library are under ErrorBoundary. On the server, a part that throws renders its fallback into the HTML, and the page answers 200. The browser starts with that fallback, and reset does nothing on the server.
In React, a suspended child can fail after the first part of the page was sent. The browser then starts with the server's fallback. An ErrorBoundary inside that failed Suspense is rendered again in the browser.
Error and render handlers
| Rule | error | render |
|---|---|---|
| runs for | every error the server answers, after the error hooks | every page, the error page included |
| does not run for | captureError, a stream, waitUntil, the endpoints under /_hamur/ | a handler's route or a public file |
| returns | a Response, or undefined to keep the answer | a Response |
| hands over with | next(error?), so next(createError({ status: 404 })) turns your own error into a 404 | render(), once per request |
- The app's handler runs first, then each layer's by priority, then each module's.
nextandrenderreach the next one, and Hamur's own answer after the last. - A handler that throws or returns something else is logged once, and the next answer is used:
the error handler of server/index.ts returned string; return a Response or undefined. - A value that is not a function fails when the server starts:
error of server/index.ts must be a function from defineErrorHandler (got object). - An error the page throws rejects
render(). A handler that catches it and returns aResponseanswers with that, and the error is still reported. - A streamed page stays streamed unless the
renderhandler reads its body.
The types are under defineErrorHandler and defineRenderHandler.