@tcds-io/fastify-errors 0.1.0 → 0.1.2
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 +4 -4
- package/dist/map-error.d.ts +9 -2
- package/dist/map-error.js +16 -5
- package/package.json +6 -5
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# @tcds-io/fastify-errors
|
|
2
2
|
|
|
3
|
-
Fastify error handler: maps [`@tcds-io/
|
|
3
|
+
Fastify error handler: maps [`@tcds-io/domain`](../domain) to an HTTP status and a wire body, in one place. `mapError` is the framework-free core, exported for other hosts.
|
|
4
4
|
|
|
5
5
|
```ts
|
|
6
6
|
import { installErrorHandler, mapError } from '@tcds-io/fastify-errors';
|
|
@@ -11,8 +11,8 @@ installErrorHandler(app); // error handler + not-found handler, one wire shape
|
|
|
11
11
|
Every failure leaves as `{ error: { id?, code, message, details?, trace? } }`.
|
|
12
12
|
|
|
13
13
|
- 4xx: `code`, `message`, `details?`; no `id`, not logged.
|
|
14
|
-
- 5xx: `code`, `message
|
|
15
|
-
- Request schema failures (Fastify validation or a thrown `ZodError
|
|
14
|
+
- 5xx: `code`, `message` = the status's HTTP reason (`Internal Server Error`, `Bad Gateway`, `Service Unavailable`, ...; one text for all with `internalMessage`), `id`. A thrown `ExternalPartyError` is a 502: the failure is a partner's, not ours. The real message and details never leave the process; log the error with the id. A `classify` result with `exposeMessage: true` keeps its own `message` on a 501-599 (a 502 `Payment provider unavailable`, say); details stay stripped and the `id` stays. A 500 always says `internalMessage`.
|
|
15
|
+
- Request schema failures (Fastify validation or a thrown `ZodError`, zod 3 or 4, matched by shape) are a 400 `REQUEST_INVALID` with `details.issues: [{ path: 'a.0.b', code, message }]`.
|
|
16
16
|
- Fastify's own request errors (`FST_ERR_CTP_*`, `FST_ERR_BAD_URL`, …) come from `DEFAULT_FRAMEWORK_ERRORS`; override entries with `frameworkErrors`.
|
|
17
17
|
- `retry-after` is set for a rate limit that knows its delay. Any other framework 4xx `statusCode` keeps its status as `BAD_REQUEST`.
|
|
18
18
|
- Options: `statusTable` (first match wins), `classify` (runs first, for legacy classes), `errorId`, `unknownCode`, `validationCode`, `clientErrorCode`, `internalMessage`, `includeTrace` (dev only).
|
|
@@ -20,4 +20,4 @@ Every failure leaves as `{ error: { id?, code, message, details?, trace? } }`.
|
|
|
20
20
|
|
|
21
21
|
## Peers
|
|
22
22
|
|
|
23
|
-
`@tcds-io/
|
|
23
|
+
`@tcds-io/domain` and `fastify` are peer dependencies: install `@tcds-io/domain` yourself so the app has one copy (ADR-0070).
|
package/dist/map-error.d.ts
CHANGED
|
@@ -28,6 +28,12 @@ export type Classification = {
|
|
|
28
28
|
readonly message?: string;
|
|
29
29
|
readonly details?: Readonly<Record<string, unknown>>;
|
|
30
30
|
readonly headers?: Readonly<Record<string, string>>;
|
|
31
|
+
/**
|
|
32
|
+
* Send `message` on a 501-599 instead of `internalMessage`. Default false. For a failure we name on purpose, such
|
|
33
|
+
* as a 502 `Payment provider unavailable`. Ignored on a 500, which always says `internalMessage`, and without a
|
|
34
|
+
* `message`. Details stay stripped and the `id` is still minted.
|
|
35
|
+
*/
|
|
36
|
+
readonly exposeMessage?: boolean;
|
|
31
37
|
};
|
|
32
38
|
/** A Fastify error code (`FST_ERR_*`) and what it answers. */
|
|
33
39
|
export type FrameworkErrorTable = Readonly<Record<string, {
|
|
@@ -62,7 +68,7 @@ export type MapErrorOptions = {
|
|
|
62
68
|
readonly validationCode?: string;
|
|
63
69
|
/** Merged over `DEFAULT_FRAMEWORK_ERRORS`, keyed by Fastify error code. */
|
|
64
70
|
readonly frameworkErrors?: FrameworkErrorTable;
|
|
65
|
-
/** What
|
|
71
|
+
/** What every 5xx says instead of its real message. Default: the status's HTTP reason (`Internal Server Error`, `Bad Gateway`, ...). */
|
|
66
72
|
readonly internalMessage?: string;
|
|
67
73
|
/** Adds the stack as `trace`. For local development only. */
|
|
68
74
|
readonly includeTrace?: boolean;
|
|
@@ -72,7 +78,8 @@ export type MapErrorOptions = {
|
|
|
72
78
|
* Framework-free: hand the result to your framework's error hook.
|
|
73
79
|
*
|
|
74
80
|
* - 4xx: the message and details go out, there is no id, and `log` is false.
|
|
75
|
-
* - 5xx:
|
|
81
|
+
* - 5xx: the status's HTTP reason (or `internalMessage`), no details, and an `id`; `log` is true so the hook logs the real error once with it.
|
|
82
|
+
* A classification with `exposeMessage: true` keeps its own message on a 501-599; a 500 never does.
|
|
76
83
|
* - Order: `classify`, then the status table for an `AppError`, then a request schema failure (400 with
|
|
77
84
|
* `details.issues`), then a known Fastify error code, then any framework 4xx `statusCode`, then 500.
|
|
78
85
|
*/
|
package/dist/map-error.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { randomUUID } from 'node:crypto';
|
|
2
|
-
import {
|
|
2
|
+
import { STATUS_CODES } from 'node:http';
|
|
3
|
+
import { AppError, BusinessRuleError, ConflictError, ExternalPartyError, ForbiddenError, NotFoundError, NotImplementedError, RateLimitedError, UnauthenticatedError, UnavailableError, ValidationError, } from '@tcds-io/domain';
|
|
3
4
|
export const DEFAULT_STATUS_TABLE = [
|
|
4
5
|
[ValidationError, 400],
|
|
5
6
|
[UnauthenticatedError, 401],
|
|
@@ -41,10 +42,13 @@ const fastifyIssuesOf = (error) => {
|
|
|
41
42
|
message: stringOr(fieldOf(entry, 'message'), ''),
|
|
42
43
|
}));
|
|
43
44
|
};
|
|
44
|
-
/**
|
|
45
|
+
/**
|
|
46
|
+
* A `ZodError` thrown in a handler, matched by shape so zod stays out of the dependencies.
|
|
47
|
+
* No `instanceof Error`: zod 4's `ZodError` is not an `Error` instance.
|
|
48
|
+
*/
|
|
45
49
|
const zodIssuesOf = (error) => {
|
|
46
50
|
const issues = fieldOf(error, 'issues');
|
|
47
|
-
if (
|
|
51
|
+
if (fieldOf(error, 'name') !== 'ZodError' || !Array.isArray(issues))
|
|
48
52
|
return null;
|
|
49
53
|
return issues.map((issue) => {
|
|
50
54
|
const path = fieldOf(issue, 'path');
|
|
@@ -86,7 +90,8 @@ const classifyBuiltIn = (error, options) => {
|
|
|
86
90
|
* Framework-free: hand the result to your framework's error hook.
|
|
87
91
|
*
|
|
88
92
|
* - 4xx: the message and details go out, there is no id, and `log` is false.
|
|
89
|
-
* - 5xx:
|
|
93
|
+
* - 5xx: the status's HTTP reason (or `internalMessage`), no details, and an `id`; `log` is true so the hook logs the real error once with it.
|
|
94
|
+
* A classification with `exposeMessage: true` keeps its own message on a 501-599; a 500 never does.
|
|
90
95
|
* - Order: `classify`, then the status table for an `AppError`, then a request schema failure (400 with
|
|
91
96
|
* `details.issues`), then a known Fastify error code, then any framework 4xx `statusCode`, then 500.
|
|
92
97
|
*/
|
|
@@ -96,10 +101,16 @@ export const mapError = (error, options = {}) => {
|
|
|
96
101
|
const server = status >= 500;
|
|
97
102
|
const trace = options.includeTrace === true ? traceOf(error) : undefined;
|
|
98
103
|
const details = server ? undefined : classification.details;
|
|
104
|
+
const internal = options.internalMessage ?? STATUS_CODES[status] ?? 'Internal Server Error';
|
|
105
|
+
const message = !server
|
|
106
|
+
? (classification.message ?? messageOf(error))
|
|
107
|
+
: status !== 500 && classification.exposeMessage === true && classification.message !== undefined
|
|
108
|
+
? classification.message
|
|
109
|
+
: internal;
|
|
99
110
|
const wire = {
|
|
100
111
|
...(server ? { id: (options.errorId ?? randomUUID)() } : {}),
|
|
101
112
|
code: classification.code,
|
|
102
|
-
message
|
|
113
|
+
message,
|
|
103
114
|
...(details !== undefined ? { details } : {}),
|
|
104
115
|
...(trace !== undefined ? { trace } : {}),
|
|
105
116
|
};
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tcds-io/fastify-errors",
|
|
3
|
-
"version": "0.1.
|
|
4
|
-
"description": "Fastify error handler: maps @tcds-io/errors to an HTTP status and a wire body in one place.",
|
|
3
|
+
"version": "0.1.2",
|
|
4
|
+
"description": "Fastify error handler: maps @tcds-io/domain errors to an HTTP status and a wire body in one place.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"sideEffects": false,
|
|
7
7
|
"license": "MIT",
|
|
@@ -28,17 +28,18 @@
|
|
|
28
28
|
"fastify": "^5.6.0",
|
|
29
29
|
"typescript": "^5.7.2",
|
|
30
30
|
"vitest": "^2.1.8",
|
|
31
|
-
"
|
|
31
|
+
"zod": "^4.1.0",
|
|
32
|
+
"@tcds-io/domain": "^0.1.0"
|
|
32
33
|
},
|
|
33
34
|
"engines": {
|
|
34
35
|
"node": ">=22"
|
|
35
36
|
},
|
|
36
37
|
"peerDependencies": {
|
|
37
|
-
"@tcds-io/
|
|
38
|
+
"@tcds-io/domain": "~0.1.0",
|
|
38
39
|
"fastify": ">=5"
|
|
39
40
|
},
|
|
40
41
|
"scripts": {
|
|
41
|
-
"build": "tsc -p tsconfig.build.json",
|
|
42
|
+
"build": "node -e \"require('node:fs').rmSync('dist',{recursive:true,force:true})\" && tsc -p tsconfig.build.json",
|
|
42
43
|
"check:types": "tsc --noEmit -p .",
|
|
43
44
|
"test": "vitest run"
|
|
44
45
|
}
|