エラー処理
Problem Details
@senrijs/senri/problem で、エラーを RFC 9457 の application/problem+json で返す
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." })); // 投げずに返す{
"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
}選んだときだけ形が変わります。既定のエラーの形(テキスト)はそのままです。
決まり
typeを省くとabout:blankになり、titleは status の名前("Not Found"など)になりますProblemErrorでないHttpError(ルーターの 405、basicAuth()の 401 など)は、messageが status だけ("405 Method Not Allowed")ならdetailを付けず、それ以外ならdetailにします。Allow・WWW-Authenticate・Retry-Afterなどのヘッダーは残りますHttpErrorでないエラーは、中身を出さずにtitleだけの 500 にします- バリデーションのエラーの中身(
errors)は、スキーマの作りが外から見えるので、既定では出しません。problemDetails({ validationErrors: true })で出します problemDetails()を使わないアプリでは、ProblemErrorはふつうのHttpErrorで、detail(なければtitle)をテキストで返します- ほかの
app.onErrorと同じく、開発モードのエラーページと 404 ページも置き換わります。本番だけで決めることもできます