handleError for SvelteKit 3: ids only for unknown errors, compact 404 log line; error page suggestions prefer the real parent over the section
All checks were successful
Deploy production / deploy (push) Successful in 1m10s
All checks were successful
Deploy production / deploy (push) Successful in 1m10s
This commit is contained in:
@@ -42,18 +42,20 @@ export const handle: Handle = async ({ event, resolve }) => {
|
||||
return response;
|
||||
};
|
||||
|
||||
// Unexpected errors (anything not thrown with error()) and unknown routes. 5xx get a short id
|
||||
// that the error page shows and the log line starts with ("[error ab12cd34]"), so a screenshot
|
||||
// or report can be matched to the stack trace. The client only gets a generic message: no
|
||||
// stack, no internals. Query strings are left out of the log (they can carry tokens).
|
||||
export const handleError: HandleServerError = ({ error, event, status, message }) => {
|
||||
const route = event.route.id ?? '(no route)';
|
||||
const where = `${event.request.method} ${event.url.pathname} route=${route}`;
|
||||
if (status === 404) {
|
||||
console.warn(`[404] ${where}`);
|
||||
return { message: 'Not Found' };
|
||||
// Runs for every error (SvelteKit 3 passes a `kind`). Unknown errors (bugs, failing services)
|
||||
// get a short id that the error page shows and the log line starts with ("[error ab12cd34]"),
|
||||
// so a screenshot or report can be matched to the stack trace; the client only gets the
|
||||
// generic "Internal Error", never the stack. 404s get one compact line (Caddy keeps no access
|
||||
// log), other error() calls are expected and stay quiet. Query strings are never logged.
|
||||
export const handleError: HandleServerError = (input) => {
|
||||
const { event } = input;
|
||||
const where = `${event.request.method} ${event.url.pathname} route=${event.route.id ?? '(none)'}`;
|
||||
if (input.kind === 'unknown') {
|
||||
const id = crypto.randomUUID().slice(0, 8);
|
||||
console.error(`[error ${id}] 500 ${where}`, input.error);
|
||||
return { id };
|
||||
}
|
||||
const id = crypto.randomUUID().slice(0, 8);
|
||||
console.error(`[error ${id}] ${status} ${where}`, error);
|
||||
return { message: status >= 500 ? 'Internal Error' : message, id };
|
||||
if (input.error.status === 404) console.warn(`[404] ${where}`);
|
||||
else if (input.error.status >= 500)
|
||||
console.error(`[${input.error.status}] ${where} ${input.error.message}`);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user