hono-problem-details 0.5.0 → 0.6.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 +54 -0
- package/dist/index.d.cts +8 -8
- package/dist/index.d.ts +8 -8
- package/package.json +6 -5
package/README.md
CHANGED
|
@@ -50,6 +50,12 @@ can all agree on.
|
|
|
50
50
|
npm install hono-problem-details
|
|
51
51
|
```
|
|
52
52
|
|
|
53
|
+
### Requirements
|
|
54
|
+
|
|
55
|
+
- Hono `>= 4.12.14` (peer dependency)
|
|
56
|
+
- TypeScript `>= 5.0` — the published `.d.ts` files are CI-tested against TS 5.0, 5.4, 5.7, and 5.9. Older TS versions may work but are not verified.
|
|
57
|
+
- Node.js `>= 22` (Node 20 reached end-of-life in April 2026; v0.6.0 raised the floor)
|
|
58
|
+
|
|
53
59
|
## Quick Start
|
|
54
60
|
|
|
55
61
|
```ts
|
|
@@ -160,6 +166,49 @@ throw problemDetails({
|
|
|
160
166
|
Rate-limit metadata goes in `extensions` — clients read it straight from the body instead
|
|
161
167
|
of juggling `Retry-After` headers.
|
|
162
168
|
|
|
169
|
+
## Unhandled Errors — 500
|
|
170
|
+
|
|
171
|
+
Anything thrown that isn't a `ProblemDetailsError` or `HTTPException` (and isn't matched by
|
|
172
|
+
`mapError`) becomes a generic 500. `detail` is **always** the constant string
|
|
173
|
+
`"An unexpected error occurred"` — never the raw `error.message` or stack — so UIs that
|
|
174
|
+
render `detail` verbatim cannot leak server internals.
|
|
175
|
+
|
|
176
|
+
```ts
|
|
177
|
+
app.get("/boom", () => {
|
|
178
|
+
throw new Error("DB connection lost: ECONNREFUSED");
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
// HTTP/1.1 500 Internal Server Error
|
|
182
|
+
// Content-Type: application/problem+json
|
|
183
|
+
// {
|
|
184
|
+
// "type": "about:blank",
|
|
185
|
+
// "status": 500,
|
|
186
|
+
// "title": "Internal Server Error",
|
|
187
|
+
// "detail": "An unexpected error occurred"
|
|
188
|
+
// }
|
|
189
|
+
```
|
|
190
|
+
|
|
191
|
+
In development, set `includeStack: true` to surface the stack trace as a top-level `stack`
|
|
192
|
+
extension member. `detail` stays constant either way — read the stack from `body.stack`:
|
|
193
|
+
|
|
194
|
+
```ts
|
|
195
|
+
problemDetailsHandler({
|
|
196
|
+
includeStack: process.env.NODE_ENV !== "production",
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
// HTTP/1.1 500 Internal Server Error
|
|
200
|
+
// {
|
|
201
|
+
// "type": "about:blank",
|
|
202
|
+
// "status": 500,
|
|
203
|
+
// "title": "Internal Server Error",
|
|
204
|
+
// "detail": "An unexpected error occurred",
|
|
205
|
+
// "stack": "Error: DB connection lost: ECONNREFUSED\n at ..."
|
|
206
|
+
// }
|
|
207
|
+
```
|
|
208
|
+
|
|
209
|
+
Keep `includeStack` off in production — stack traces should not leave the server even via
|
|
210
|
+
opt-in extension fields.
|
|
211
|
+
|
|
163
212
|
## Extension Members
|
|
164
213
|
|
|
165
214
|
Extension members are flattened to top level per RFC 9457:
|
|
@@ -296,6 +345,11 @@ app.post("/users", sValidator("json", schema, standardSchemaProblemHook()), (c)
|
|
|
296
345
|
|
|
297
346
|
## OpenAPI Integration
|
|
298
347
|
|
|
348
|
+
> **Peer dependencies**: `./openapi` requires `@hono/zod-openapi@^1.0.0`, which in turn
|
|
349
|
+
> requires `zod@^4.0.0`. The base `./zod` integration (validator hook) works with both
|
|
350
|
+
> `zod@^3.25.0` and `zod@^4.0.0` — the version constraint above only applies when you import
|
|
351
|
+
> from `hono-problem-details/openapi`.
|
|
352
|
+
|
|
299
353
|
Use with `@hono/zod-openapi` to document Problem Details error responses in your OpenAPI spec:
|
|
300
354
|
|
|
301
355
|
```ts
|
package/dist/index.d.cts
CHANGED
|
@@ -61,11 +61,6 @@ interface ProblemDetailsHandlerOptions {
|
|
|
61
61
|
localize?: (pd: ProblemDetails, c: Context) => Partial<ProblemDetails> | undefined;
|
|
62
62
|
}
|
|
63
63
|
|
|
64
|
-
/** Look up the standard HTTP reason phrase for a status code (e.g. 404 → `"Not Found"`). */
|
|
65
|
-
declare function statusToPhrase(status: number): string | undefined;
|
|
66
|
-
/** Look up a URL-safe slug for a status code (e.g. 404 → `"not-found"`). Used by the `typePrefix` handler option. */
|
|
67
|
-
declare function statusToSlug(status: number): string | undefined;
|
|
68
|
-
|
|
69
64
|
/**
|
|
70
65
|
* Error class for RFC 9457 Problem Details.
|
|
71
66
|
* Thrown by {@link problemDetails} and caught by the handler to produce
|
|
@@ -97,9 +92,6 @@ declare class ProblemDetailsError extends Error {
|
|
|
97
92
|
*/
|
|
98
93
|
declare function problemDetails<T extends Record<string, unknown>>(input: ProblemDetailsInput<T>): ProblemDetailsError;
|
|
99
94
|
|
|
100
|
-
/** RFC 9457 media type: `application/problem+json; charset=utf-8`. */
|
|
101
|
-
declare const PROBLEM_JSON_CONTENT_TYPE = "application/problem+json; charset=utf-8";
|
|
102
|
-
|
|
103
95
|
/**
|
|
104
96
|
* Create an `app.onError` handler that returns RFC 9457 Problem Details responses.
|
|
105
97
|
* Handles {@link ProblemDetailsError}, Hono `HTTPException`, and unhandled exceptions.
|
|
@@ -151,4 +143,12 @@ interface ProblemTypeRegistry<K extends string> {
|
|
|
151
143
|
*/
|
|
152
144
|
declare function createProblemTypeRegistry<K extends string>(definitions: Record<K, ProblemTypeDefinition>): ProblemTypeRegistry<K>;
|
|
153
145
|
|
|
146
|
+
/** Look up the standard HTTP reason phrase for a status code (e.g. 404 → `"Not Found"`). */
|
|
147
|
+
declare function statusToPhrase(status: number): string | undefined;
|
|
148
|
+
/** Look up a URL-safe slug for a status code (e.g. 404 → `"not-found"`). Used by the `typePrefix` handler option. */
|
|
149
|
+
declare function statusToSlug(status: number): string | undefined;
|
|
150
|
+
|
|
151
|
+
/** RFC 9457 media type: `application/problem+json; charset=utf-8`. */
|
|
152
|
+
declare const PROBLEM_JSON_CONTENT_TYPE = "application/problem+json; charset=utf-8";
|
|
153
|
+
|
|
154
154
|
export { PROBLEM_JSON_CONTENT_TYPE, type ProblemDetails, ProblemDetailsError, type ProblemDetailsHandlerOptions, type ProblemDetailsInput, createProblemTypeRegistry, problemDetails, problemDetailsHandler, statusToPhrase, statusToSlug };
|
package/dist/index.d.ts
CHANGED
|
@@ -61,11 +61,6 @@ interface ProblemDetailsHandlerOptions {
|
|
|
61
61
|
localize?: (pd: ProblemDetails, c: Context) => Partial<ProblemDetails> | undefined;
|
|
62
62
|
}
|
|
63
63
|
|
|
64
|
-
/** Look up the standard HTTP reason phrase for a status code (e.g. 404 → `"Not Found"`). */
|
|
65
|
-
declare function statusToPhrase(status: number): string | undefined;
|
|
66
|
-
/** Look up a URL-safe slug for a status code (e.g. 404 → `"not-found"`). Used by the `typePrefix` handler option. */
|
|
67
|
-
declare function statusToSlug(status: number): string | undefined;
|
|
68
|
-
|
|
69
64
|
/**
|
|
70
65
|
* Error class for RFC 9457 Problem Details.
|
|
71
66
|
* Thrown by {@link problemDetails} and caught by the handler to produce
|
|
@@ -97,9 +92,6 @@ declare class ProblemDetailsError extends Error {
|
|
|
97
92
|
*/
|
|
98
93
|
declare function problemDetails<T extends Record<string, unknown>>(input: ProblemDetailsInput<T>): ProblemDetailsError;
|
|
99
94
|
|
|
100
|
-
/** RFC 9457 media type: `application/problem+json; charset=utf-8`. */
|
|
101
|
-
declare const PROBLEM_JSON_CONTENT_TYPE = "application/problem+json; charset=utf-8";
|
|
102
|
-
|
|
103
95
|
/**
|
|
104
96
|
* Create an `app.onError` handler that returns RFC 9457 Problem Details responses.
|
|
105
97
|
* Handles {@link ProblemDetailsError}, Hono `HTTPException`, and unhandled exceptions.
|
|
@@ -151,4 +143,12 @@ interface ProblemTypeRegistry<K extends string> {
|
|
|
151
143
|
*/
|
|
152
144
|
declare function createProblemTypeRegistry<K extends string>(definitions: Record<K, ProblemTypeDefinition>): ProblemTypeRegistry<K>;
|
|
153
145
|
|
|
146
|
+
/** Look up the standard HTTP reason phrase for a status code (e.g. 404 → `"Not Found"`). */
|
|
147
|
+
declare function statusToPhrase(status: number): string | undefined;
|
|
148
|
+
/** Look up a URL-safe slug for a status code (e.g. 404 → `"not-found"`). Used by the `typePrefix` handler option. */
|
|
149
|
+
declare function statusToSlug(status: number): string | undefined;
|
|
150
|
+
|
|
151
|
+
/** RFC 9457 media type: `application/problem+json; charset=utf-8`. */
|
|
152
|
+
declare const PROBLEM_JSON_CONTENT_TYPE = "application/problem+json; charset=utf-8";
|
|
153
|
+
|
|
154
154
|
export { PROBLEM_JSON_CONTENT_TYPE, type ProblemDetails, ProblemDetailsError, type ProblemDetailsHandlerOptions, type ProblemDetailsInput, createProblemTypeRegistry, problemDetails, problemDetailsHandler, statusToPhrase, statusToSlug };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "hono-problem-details",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.6.0",
|
|
4
4
|
"description": "RFC 9457 Problem Details middleware for Hono",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.cjs",
|
|
@@ -133,9 +133,9 @@
|
|
|
133
133
|
}
|
|
134
134
|
},
|
|
135
135
|
"devDependencies": {
|
|
136
|
-
"@biomejs/biome": "^
|
|
136
|
+
"@biomejs/biome": "^2.4.14",
|
|
137
137
|
"@changesets/changelog-github": "0.6.0",
|
|
138
|
-
"@changesets/cli": "2.
|
|
138
|
+
"@changesets/cli": "2.31.0",
|
|
139
139
|
"@hono/standard-validator": "0.2.2",
|
|
140
140
|
"@hono/valibot-validator": "^0.6.1",
|
|
141
141
|
"@hono/zod-openapi": "1.3.0",
|
|
@@ -143,7 +143,7 @@
|
|
|
143
143
|
"@standard-schema/spec": "1.1.0",
|
|
144
144
|
"@vitest/coverage-v8": "^3.0.0",
|
|
145
145
|
"hono": "^4.12.14",
|
|
146
|
-
"lefthook": "2.1.
|
|
146
|
+
"lefthook": "2.1.6",
|
|
147
147
|
"tsup": "^8.0.0",
|
|
148
148
|
"typescript": "^5.7.0",
|
|
149
149
|
"valibot": "^1.3.1",
|
|
@@ -151,7 +151,7 @@
|
|
|
151
151
|
"zod": "^4.0.0"
|
|
152
152
|
},
|
|
153
153
|
"engines": {
|
|
154
|
-
"node": ">=
|
|
154
|
+
"node": ">=22"
|
|
155
155
|
},
|
|
156
156
|
"publishConfig": {
|
|
157
157
|
"access": "public",
|
|
@@ -166,6 +166,7 @@
|
|
|
166
166
|
"lint:fix": "biome check --write .",
|
|
167
167
|
"format": "biome format --write .",
|
|
168
168
|
"typecheck": "tsc --noEmit",
|
|
169
|
+
"test:compat": "tsc -p tsconfig.compat.json",
|
|
169
170
|
"release": "pnpm build && changeset publish",
|
|
170
171
|
"version-packages": "changeset version && pnpm lint:fix"
|
|
171
172
|
}
|