Fondu

Errors

View as Markdown

The error page

An unknown URL, a throw new HTTPException(403) or an error in a handler: a page request gets app/routes/_error.tsx with the status, inside the root layout, with the shared props. API clients get plain text, or the HTTPException's own response. A page request is a navigation of Fondu's client or a request that accepts text/html, as a browser's document load does. Any other request (curl, a fetch() call) is an API client's.

// app/routes/_error.tsx
export default function ErrorPage({ status }: { status: number }) {
  return <h1>{status === 404 ? 'Page not found' : 'Something went wrong'}</h1>
}

The shared props are what the middleware shared before the error: the root _middleware.ts's, and those of the route's directories when the error came after them. If a middleware itself fails, the error page has only what was shared before it failed.

A method the route has no handler for (DELETE to a route with GET and POST, POST to a page with only a loader) is a 405 with an Allow header listing the route's methods (HEAD with GET; OPTIONS is not added). It gets the error page too, with status 405; a URL that matches no route stays a 404. A directory's cors() still answers a preflight, as its middleware runs first.

A response that Accept chooses (the error page or plain text, a redirect back or a 422) has Vary: Accept, added to the Vary it has, so a shared cache keeps the forms apart.

A request over the body limit (413) gets the error page too, with the shared props: the limit runs after the middleware of the route's directories. A CSRF refusal (403) is plain text, "Forbidden": Hono's CSRF protection answers it with its own response, before any middleware.

Without app/routes/_error.tsx, plain text for everyone. An error page that itself fails to render gets plain text too, with its status and the failure in the log (in the browser: "Something went wrong" and the failure in the console): a page is never left blank.

A page that throws as it renders in the browser gets the error page with status 500 in its place, inside its layouts (the error in the console); a layout that throws, the error page alone. The next page is shown as usual.

In development

A 500 is a report of the error instead: the message, and the stack with your files first; a navigation loads it as a document. Fix the code and save: the report reloads.

An error before any handler runs, in a route that throws as it is imported, is Vite's: its overlay shows it, as a whole page, and the next edit reloads the page.

Answers from outside the app

An answer that isn't a page, such as a proxy's 502 or a CDN's timeout page, comes from what stands in front of the app: Fondu answers navigations with pages, errors included. The browser shows it as it would without JavaScript: a link's loads the URL as a document, a form's replaces the page.

Your own handling

createApp() returns a Hono app: its notFound() and onError() replace Fondu's.

const app = createApp()

app.onError((error, c) => {
  report(error)
  return c.text('Something went wrong', 500)
})

export default app

Last updated