Errors
Problem Details
Return errors as RFC 9457 application/problem+json with @senrijs/senri/problem
import { } from "@senrijs/senri";
import { , , } from "@senrijs/senri/problem";
const = ();
const = ();
.(.);
.(.);
.("/transfer", () => {
throw new (403, {
: "https://example.com/probs/out-of-credit",
: "You do not have enough credit.",
: "Your current balance is 30, but that costs 50.",
: { : 30 },
});
});
.("/legacy", (, ) => (, { : 410, : "Use /v2 instead." })); // return without throwing{
"type": "https://example.com/probs/out-of-credit",
"title": "You do not have enough credit.",
"status": 403,
"detail": "Your current balance is 30, but that costs 50.",
"balance": 30
}The format changes only when you opt in. The default error format (text) stays as it is.
Rules
- Without
type, it isabout:blank, andtitleis the status name ("Not Found"and so on) - For
HttpErrors that are notProblemErrors (405 from the router, 401 frombasicAuth()and so on), amessagethat is only the status ("405 Method Not Allowed") gets nodetail; any other message becomesdetail. Headers such asAllow,WWW-AuthenticateandRetry-Afterare kept - Errors that are not
HttpErrors become a 500 with onlytitle, without showing their content - The details of validation errors (
errors) are not included by default, because they show how the schemas are built. Include them withproblemDetails({ validationErrors: true }) - In apps that do not use
problemDetails(), aProblemErroris a plainHttpErrorand is sent as text: itsdetail(ortitle) - Like any
app.onError, it also replaces the development error and 404 pages. You can set it only in production