hono-problem-details 0.2.1 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +19 -5
- package/dist/index.cjs +5 -1
- package/dist/index.d.cts +18 -3
- package/dist/index.d.ts +18 -3
- package/dist/index.js +5 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -125,6 +125,11 @@ throw problemDetails({
|
|
|
125
125
|
`instance` points at the specific occurrence — clients can use it as a key for retry logic
|
|
126
126
|
or deduplication.
|
|
127
127
|
|
|
128
|
+
> **Auto-fill shortcuts.** `title` is optional — when omitted, the standard HTTP reason phrase
|
|
129
|
+
> for `status` is used (`404` → `"Not Found"`). Similarly, `instance` can be populated from
|
|
130
|
+
> the request path automatically via `problemDetailsHandler({ autoInstance: true })`. Both
|
|
131
|
+
> shortcuts skip the boilerplate in the example above; explicit values always win.
|
|
132
|
+
|
|
128
133
|
### Conflict — 409
|
|
129
134
|
|
|
130
135
|
```ts
|
|
@@ -337,14 +342,16 @@ const errorWithExtensions = createProblemDetailsSchema(
|
|
|
337
342
|
|
|
338
343
|
## Localization
|
|
339
344
|
|
|
340
|
-
Use the `localize` callback to translate `title` and `detail` based on the request context
|
|
345
|
+
Use the `localize` callback to translate `title` and `detail` based on the request context.
|
|
346
|
+
Return a partial patch with just the fields you want to override — everything else falls
|
|
347
|
+
through unchanged. Returning nothing (or `undefined`) leaves the response untouched.
|
|
341
348
|
|
|
342
349
|
```ts
|
|
343
350
|
problemDetailsHandler({
|
|
344
351
|
localize: (pd, c) => {
|
|
345
352
|
const lang = c.req.header("Accept-Language");
|
|
346
353
|
if (lang?.startsWith("ja")) {
|
|
347
|
-
return {
|
|
354
|
+
return { title: translate("ja", pd.title) };
|
|
348
355
|
}
|
|
349
356
|
return pd;
|
|
350
357
|
},
|
|
@@ -373,11 +380,18 @@ problemDetailsHandler({
|
|
|
373
380
|
// Default type URI (default: "about:blank")
|
|
374
381
|
defaultType: "about:blank",
|
|
375
382
|
|
|
376
|
-
// Include stack trace in
|
|
383
|
+
// Include stack trace in `extensions.stack` on 500 responses (development only)
|
|
377
384
|
includeStack: process.env.NODE_ENV === "development",
|
|
378
385
|
|
|
379
|
-
//
|
|
380
|
-
|
|
386
|
+
// Populate `instance` from `c.req.path` when the thrown problem didn't specify one
|
|
387
|
+
autoInstance: true,
|
|
388
|
+
|
|
389
|
+
// Localize title/detail before sending the response.
|
|
390
|
+
// Return a partial patch — fields you omit fall through from the original.
|
|
391
|
+
localize: (pd, c) => {
|
|
392
|
+
const lang = c.req.header("Accept-Language") ?? "en";
|
|
393
|
+
return { title: `[${lang}] ${pd.title}` };
|
|
394
|
+
},
|
|
381
395
|
|
|
382
396
|
// Custom error mapping
|
|
383
397
|
mapError: (error) => {
|
package/dist/index.cjs
CHANGED
|
@@ -171,6 +171,9 @@ function buildType(status, options) {
|
|
|
171
171
|
}
|
|
172
172
|
function toResponse(input, c, options) {
|
|
173
173
|
let pd = normalizeProblemDetails(input);
|
|
174
|
+
if (options.autoInstance && pd.instance === void 0) {
|
|
175
|
+
pd = { ...pd, instance: c.req.path };
|
|
176
|
+
}
|
|
174
177
|
if (options.localize) {
|
|
175
178
|
try {
|
|
176
179
|
pd = { ...pd, ...options.localize(pd, c) };
|
|
@@ -208,7 +211,8 @@ function problemDetailsHandler(options = {}) {
|
|
|
208
211
|
status: 500,
|
|
209
212
|
type: buildType(500, options),
|
|
210
213
|
title: "Internal Server Error",
|
|
211
|
-
detail:
|
|
214
|
+
detail: "An unexpected error occurred",
|
|
215
|
+
extensions: options.includeStack ? { stack: error.stack } : void 0
|
|
212
216
|
},
|
|
213
217
|
c,
|
|
214
218
|
options
|
package/dist/index.d.cts
CHANGED
|
@@ -38,12 +38,27 @@ interface ProblemDetailsHandlerOptions {
|
|
|
38
38
|
typePrefix?: string;
|
|
39
39
|
/** Default type URI. Defaults to "about:blank" */
|
|
40
40
|
defaultType?: string;
|
|
41
|
-
/**
|
|
41
|
+
/**
|
|
42
|
+
* Include the stack trace on unhandled `Error` responses (500). The stack is
|
|
43
|
+
* emitted as a top-level `stack` extension member per RFC 9457 §3.1 flattening,
|
|
44
|
+
* not in `detail`, to prevent leakage into UIs that render `detail` verbatim.
|
|
45
|
+
* Development-only.
|
|
46
|
+
*/
|
|
42
47
|
includeStack?: boolean;
|
|
48
|
+
/**
|
|
49
|
+
* When `true`, populate `instance` from the request path (`c.req.path`) if
|
|
50
|
+
* the thrown problem didn't specify one. Explicit values always win.
|
|
51
|
+
* Default: `false` — opt-in to avoid silently changing response shape.
|
|
52
|
+
*/
|
|
53
|
+
autoInstance?: boolean;
|
|
43
54
|
/** Custom error to ProblemDetails mapping */
|
|
44
55
|
mapError?: (error: Error) => ProblemDetailsInput | undefined;
|
|
45
|
-
/**
|
|
46
|
-
|
|
56
|
+
/**
|
|
57
|
+
* Localize title/detail before sending the response.
|
|
58
|
+
* Returned fields are merged onto the original ProblemDetails, so callers may
|
|
59
|
+
* return a partial patch (e.g. `{ title: "..." }`) or omit the return entirely.
|
|
60
|
+
*/
|
|
61
|
+
localize?: (pd: ProblemDetails, c: Context) => Partial<ProblemDetails> | undefined;
|
|
47
62
|
}
|
|
48
63
|
|
|
49
64
|
/** Look up the standard HTTP reason phrase for a status code (e.g. 404 → `"Not Found"`). */
|
package/dist/index.d.ts
CHANGED
|
@@ -38,12 +38,27 @@ interface ProblemDetailsHandlerOptions {
|
|
|
38
38
|
typePrefix?: string;
|
|
39
39
|
/** Default type URI. Defaults to "about:blank" */
|
|
40
40
|
defaultType?: string;
|
|
41
|
-
/**
|
|
41
|
+
/**
|
|
42
|
+
* Include the stack trace on unhandled `Error` responses (500). The stack is
|
|
43
|
+
* emitted as a top-level `stack` extension member per RFC 9457 §3.1 flattening,
|
|
44
|
+
* not in `detail`, to prevent leakage into UIs that render `detail` verbatim.
|
|
45
|
+
* Development-only.
|
|
46
|
+
*/
|
|
42
47
|
includeStack?: boolean;
|
|
48
|
+
/**
|
|
49
|
+
* When `true`, populate `instance` from the request path (`c.req.path`) if
|
|
50
|
+
* the thrown problem didn't specify one. Explicit values always win.
|
|
51
|
+
* Default: `false` — opt-in to avoid silently changing response shape.
|
|
52
|
+
*/
|
|
53
|
+
autoInstance?: boolean;
|
|
43
54
|
/** Custom error to ProblemDetails mapping */
|
|
44
55
|
mapError?: (error: Error) => ProblemDetailsInput | undefined;
|
|
45
|
-
/**
|
|
46
|
-
|
|
56
|
+
/**
|
|
57
|
+
* Localize title/detail before sending the response.
|
|
58
|
+
* Returned fields are merged onto the original ProblemDetails, so callers may
|
|
59
|
+
* return a partial patch (e.g. `{ title: "..." }`) or omit the return entirely.
|
|
60
|
+
*/
|
|
61
|
+
localize?: (pd: ProblemDetails, c: Context) => Partial<ProblemDetails> | undefined;
|
|
47
62
|
}
|
|
48
63
|
|
|
49
64
|
/** Look up the standard HTTP reason phrase for a status code (e.g. 404 → `"Not Found"`). */
|
package/dist/index.js
CHANGED
|
@@ -44,6 +44,9 @@ function buildType(status, options) {
|
|
|
44
44
|
}
|
|
45
45
|
function toResponse(input, c, options) {
|
|
46
46
|
let pd = normalizeProblemDetails(input);
|
|
47
|
+
if (options.autoInstance && pd.instance === void 0) {
|
|
48
|
+
pd = { ...pd, instance: c.req.path };
|
|
49
|
+
}
|
|
47
50
|
if (options.localize) {
|
|
48
51
|
try {
|
|
49
52
|
pd = { ...pd, ...options.localize(pd, c) };
|
|
@@ -81,7 +84,8 @@ function problemDetailsHandler(options = {}) {
|
|
|
81
84
|
status: 500,
|
|
82
85
|
type: buildType(500, options),
|
|
83
86
|
title: "Internal Server Error",
|
|
84
|
-
detail:
|
|
87
|
+
detail: "An unexpected error occurred",
|
|
88
|
+
extensions: options.includeStack ? { stack: error.stack } : void 0
|
|
85
89
|
},
|
|
86
90
|
c,
|
|
87
91
|
options
|