Errors and not-found pages#
Things go wrong. A record does not exist, a visitor lacks permission, or a bug throws. Kiln gives you three tools: throw an HttpError to return a specific status, use _not-found.tsx and _error.tsx to show friendly pages, and read the development overlay when you are fixing a bug.
HTTP errors#
HttpError stops rendering and returns the status you choose:
import { HttpError } from "@kiln-js/core";
throw new HttpError(429, "Too many requests");Two shortcuts cover the common cases:
import { notFound, forbidden } from "@kiln-js/core";
if (!post) notFound(); // 404
if (!canEdit) forbidden(); // 403Both return never, so code after them does not run and TypeScript knows that.
Where the error appears depends on the request:
- A page request. The status is set and a minimal HTML page is returned, unless a
_not-found.tsxapplies to a 404. Kiln renders that page for a 404. - An API request under
/api/, or any request withaccept: application/json. The response is JSON:{ "error": "Too many requests" }.
If you do not pass a message, Kiln uses Not found for 404, Forbidden for 403, Unauthorized for 401, and Error for everything else.
Redirects#
redirect is not an error, but it leaves rendering early in the same way. Return it:
import { redirect } from "@kiln-js/core";
export async function loader({ request }: { request: Request }) {
if (!isSignedIn(request)) return redirect("/login");
return {};
}Use 303 (the default) after a form submission, and 302 or 301 for permanent or temporary moves. See Loaders and actions.
The not-found page#
A _not-found.tsx file renders when no route matches a URL, or when a loader throws notFound(). It is a normal page component with the layouts that apply to its folder:
// app/pages/_not-found.tsx
export default function NotFound() {
return (
<main>
<kiln:title>Page not found</kiln:title>
<h1>We can't find that page</h1>
<p>Check the address, or go back home.</p>
</main>
);
}A _not-found.tsx in a folder covers only URLs under that folder. The nearest one wins. Without any, the server sends the notFound string from createAppHandler, which defaults to <h1>404: Not found</h1>.
The not-found page is sent with status 404 and cache-control: no-store.
The error page#
A _error.tsx renders when a page throws an error that is not an HttpError:
// app/pages/_error.tsx
export default function ErrorPage({ status, error }: { status: number; error: { message: string; requestId: string } }) {
return (
<main>
<kiln:title>Something went wrong</kiln:title>
<h1>{status}</h1>
<p>{error.message}</p>
<p>Reference: {error.requestId}</p>
</main>
);
}The page receives:
status:500.error.message: the generic text Kiln uses for 500 errors, so internal details are not shown to visitors.error.requestId: a random id that is also written to the server log, so you can match a visitor's report to the logged stack trace.
The page is sent with status 500 and cache-control: no-store.
An error in a _error file itself returns a plain server error. Keep the error page simple.
Development#
During kiln dev, an error in a page is not handed to _error.tsx. Instead, the development overlay shows the failing file, a code frame around the line, and the full stack. That lets you see the problem where it happened. The overlay clears when the file is fixed.
A server-side change that causes an error reloads the error page once the file that caused it changes.
Production#
In production, the server logs the error with the request method, path, the request id, and the stack trace. It then renders the nearest _error.tsx. If there is none, the server returns a plain 500 response.
Errors in client navigation#
If a navigation fails, for example because the network is down, the client logs navigation failed, loading the page normally and does a full page load. A page that crashes after it has rendered is caught by an error boundary, and the layouts stay usable. See @kiln-js/client.
Errors in actions and API routes#
- An
HttpErrorin an action returns that status. - An unexpected error in an action is not rendered with
_error.tsx. It is logged and the visitor receives a plain server error. Catch errors you expect in the action and return a redirect or aResponsewith a message. - An unexpected error in an API route is logged and returns a server error. API routes do not render
_errorpages.
Summary#
| Situation | Result |
|---|---|
notFound() or no matching route | _not-found.tsx with 404 |
forbidden() or HttpError(403) | Status 403 with a short page, or JSON under /api/ |
| Unexpected exception in a page, production | _error.tsx with 500 and a request id |
| Unexpected exception in a page, development | Overlay with the stack |
| Unexpected exception in an API route | Server log and 500 |

