Primate Logo Primate
Guides Routes

404 fallback vs. +error.ts

Use a rest route for "not found" pages. Use +error.ts to handle errors thrown by matched routes. Only the nearest +error.ts runs; error handlers don't compose.

404 fallback ≠ error handler. The fallback only runs when nothing else matched. +error.ts runs when a matched route (or hook/layout) throws.

1) Not-found fallback (handles unmatched URLs)

Return a normal error response.

// routes/[[...path]].ts
import route from "primate/route";

export default route({
  get() {
    return new Response("Not found", { status: 404 });
  },
});

2) Error handler (handles thrown errors in matched routes)

+error.ts is triggered on any thrown errors within routes.

// routes/+error.ts
import route from "primate/route";
import response from "primate/response";

 // or return a rendered error view

export default route({
  get() {
    return response.redirect("/");
  },
});

3) When to use which

  • Use the fallback route to show a friendly 404.
  • Use +error.ts for exceptions/timeouts/validation errors in routes that did match.