Errors
Thrown errors
Anything thrown in middleware, a handler or a template becomes a 500.
- Dev mode or
debug: true: an HTML error page with the message, the file and line (template lines map to the.etsor.mdsource), and the stack trimmed topb_pages-relative paths. - Otherwise: a plain "Something went wrong" page.
The full error always goes to the app's log.
ctx.httpError(status, message)
Throw ctx.httpError() to answer with a specific status:
// pb_pages/products/[id].ts
module.exports = {
load(ctx) {
let product
try {
product = ctx.app.findFirstRecordByData('products', 'slug', ctx.params.id)
} catch (e) {
throw ctx.httpError(404, 'No such product') // findFirstRecordByData throws when nothing matches
}
ctx.data.product = product
},
}
- In a page, it sends that status with a plain page showing
message. - In an endpoint, it sends JSON (unless the request's
Acceptputstext/htmlfirst):{ "status": 404, "message": "No such product" }.
A custom error page
Create pb_pages/+error.ets. It renders for every ctx.httpError and for every 500 outside dev mode, inside the root layout, with ctx.error = { status, message }:
<!-- pb_pages/+error.ets -->
<% slot('title', () => { %>Error <%= ctx.error.status %><% }) %>
<h1><%= ctx.error.status %></h1>
<p><%= ctx.error.message %></p>
+error and +404 live only at the top of pb_pages. Either may be .ets or .tsx.
A custom 404 page
Create pb_pages/+404.ets. It renders inside the root layout, with status 404, for a GET or HEAD that nothing answers: no page, no file in pb_public and no PocketBase route. It never renders for /api/ or /_/. The root +middleware.ts runs first, and page code can still change the status.
<!-- pb_pages/+404.ets -->
<% slot('title', () => { %>Not found<% }) %>
<h1>Not found</h1>
<p>Nothing lives at <%= ctx.req.url.pathname %>.</p>
Questions and comments
Comments are a GitHub Discussions thread; sign in with GitHub to post.