@setu-ts/exceptions 0.0.0-stage → 0.10.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/AGENT_CARD.md +50 -0
- package/LICENSE +21 -0
- package/README.md +188 -2
- package/dist/errors/exceptions.d.ts +146 -0
- package/dist/errors/exceptions.d.ts.map +1 -0
- package/dist/errors/exceptions.js +189 -0
- package/dist/errors/exceptions.js.map +1 -0
- package/dist/errors/http-error.d.ts +111 -0
- package/dist/errors/http-error.d.ts.map +1 -0
- package/dist/errors/http-error.js +81 -0
- package/dist/errors/http-error.js.map +1 -0
- package/dist/formatters/error-formatter.d.ts +81 -0
- package/dist/formatters/error-formatter.d.ts.map +1 -0
- package/dist/formatters/error-formatter.js +58 -0
- package/dist/formatters/error-formatter.js.map +1 -0
- package/dist/formatters/problem-details.d.ts +118 -0
- package/dist/formatters/problem-details.d.ts.map +1 -0
- package/dist/formatters/problem-details.js +108 -0
- package/dist/formatters/problem-details.js.map +1 -0
- package/dist/formatters/rfc7807-formatter.d.ts +20 -0
- package/dist/formatters/rfc7807-formatter.d.ts.map +1 -0
- package/dist/formatters/rfc7807-formatter.js +27 -0
- package/dist/formatters/rfc7807-formatter.js.map +1 -0
- package/dist/formatters/rfc9457-formatter.d.ts +29 -0
- package/dist/formatters/rfc9457-formatter.d.ts.map +1 -0
- package/dist/formatters/rfc9457-formatter.js +43 -0
- package/dist/formatters/rfc9457-formatter.js.map +1 -0
- package/dist/index.d.ts +33 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +33 -0
- package/dist/index.js.map +1 -0
- package/dist/middleware/error-handler.d.ts +111 -0
- package/dist/middleware/error-handler.d.ts.map +1 -0
- package/dist/middleware/error-handler.js +221 -0
- package/dist/middleware/error-handler.js.map +1 -0
- package/dist/middleware/error-responder-impl.d.ts +57 -0
- package/dist/middleware/error-responder-impl.d.ts.map +1 -0
- package/dist/middleware/error-responder-impl.js +158 -0
- package/dist/middleware/error-responder-impl.js.map +1 -0
- package/package.json +53 -3
- package/src/errors/exceptions.ts +209 -0
- package/src/errors/http-error.ts +132 -0
- package/src/formatters/error-formatter.ts +127 -0
- package/src/formatters/problem-details.ts +177 -0
- package/src/formatters/rfc7807-formatter.ts +48 -0
- package/src/formatters/rfc9457-formatter.ts +69 -0
- package/src/index.ts +57 -0
- package/src/middleware/error-handler.ts +357 -0
- package/src/middleware/error-responder-impl.ts +192 -0
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Error format selector — resolves the configured error format to a concrete
|
|
3
|
+
* formatter function.
|
|
4
|
+
*
|
|
5
|
+
* Supported formats:
|
|
6
|
+
* - `'default'` — Framework-standard error shape (`statusCode`, `message`,
|
|
7
|
+
* optional `details`).
|
|
8
|
+
* - `'rfc9457'` — RFC 9457 Problem Details (see `rfc9457-formatter.ts`).
|
|
9
|
+
* - `'rfc7807'` — deprecated; RFC 7807 Problem Details (see
|
|
10
|
+
* `rfc7807-formatter.ts`). RFC 7807 was obsoleted by RFC 9457.
|
|
11
|
+
* - A custom function — used as-is.
|
|
12
|
+
*
|
|
13
|
+
* @module
|
|
14
|
+
*/
|
|
15
|
+
import type { IRequestContext } from '@setu-ts/common';
|
|
16
|
+
|
|
17
|
+
import { rfc9457Formatter } from './rfc9457-formatter.ts';
|
|
18
|
+
import { rfc7807Formatter } from './rfc7807-formatter.ts';
|
|
19
|
+
import { HttpError } from '../errors/http-error.ts';
|
|
20
|
+
|
|
21
|
+
// ---------------------------------------------------------------------------
|
|
22
|
+
// Types
|
|
23
|
+
// ---------------------------------------------------------------------------
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* A function that formats a thrown error into a serializable error body.
|
|
27
|
+
*
|
|
28
|
+
* @param error - The thrown error (an `HttpError` or a generic `Error`)
|
|
29
|
+
* @param ctx - Optional request context (used for `instance`, logging, etc.)
|
|
30
|
+
* @returns The formatted error body
|
|
31
|
+
* @since 0.1.0
|
|
32
|
+
*/
|
|
33
|
+
export type ErrorHandlerFormatter = (
|
|
34
|
+
error: Error,
|
|
35
|
+
ctx?: IRequestContext,
|
|
36
|
+
) => Record<string, unknown>;
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* The built-in error format identifiers for `@setu-ts/exceptions`.
|
|
40
|
+
*
|
|
41
|
+
* `'rfc7807'` is retained for backward compatibility and resolves to the
|
|
42
|
+
* deprecated {@linkcode rfc7807Formatter}; prefer `'rfc9457'`.
|
|
43
|
+
*
|
|
44
|
+
* @since 0.1.0
|
|
45
|
+
*/
|
|
46
|
+
export type ErrorFormat = 'default' | 'rfc9457' | 'rfc7807';
|
|
47
|
+
|
|
48
|
+
// ---------------------------------------------------------------------------
|
|
49
|
+
// Built-in default formatter
|
|
50
|
+
// ---------------------------------------------------------------------------
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* The framework-standard error body shape.
|
|
54
|
+
*
|
|
55
|
+
* @since 0.1.0
|
|
56
|
+
*/
|
|
57
|
+
export interface DefaultErrorBody {
|
|
58
|
+
/** The HTTP status code. */
|
|
59
|
+
readonly statusCode: number;
|
|
60
|
+
/** Human-readable error message. */
|
|
61
|
+
readonly message: string;
|
|
62
|
+
/** Optional structured details (present when the error carries any). */
|
|
63
|
+
readonly details?: Readonly<Record<string, unknown>>;
|
|
64
|
+
/** Optional stack trace (present only when `includeStackTrace` is on). */
|
|
65
|
+
readonly stack?: string;
|
|
66
|
+
/** Allow callers to attach further members. */
|
|
67
|
+
readonly [key: string]: unknown;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Framework-standard error formatter.
|
|
72
|
+
*
|
|
73
|
+
* Returns a body shaped as:
|
|
74
|
+
*
|
|
75
|
+
* ```json
|
|
76
|
+
* { "statusCode": 404, "message": "User not found", "details": { ... } }
|
|
77
|
+
* ```
|
|
78
|
+
*
|
|
79
|
+
* Generic (non-`HttpError`) errors map to `500`.
|
|
80
|
+
*
|
|
81
|
+
* @param error - The thrown error to format
|
|
82
|
+
* @returns The default formatted error body
|
|
83
|
+
* @since 0.1.0
|
|
84
|
+
*/
|
|
85
|
+
export const defaultFormatter: ErrorHandlerFormatter = (error: Error): DefaultErrorBody => {
|
|
86
|
+
const isHttp = error instanceof HttpError;
|
|
87
|
+
return {
|
|
88
|
+
statusCode: isHttp ? error.statusCode : 500,
|
|
89
|
+
message: error.message,
|
|
90
|
+
...(isHttp && error.details !== undefined && { details: error.details }),
|
|
91
|
+
};
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
// ---------------------------------------------------------------------------
|
|
95
|
+
// Selector
|
|
96
|
+
// ---------------------------------------------------------------------------
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Resolve the error format configuration to a concrete formatter function.
|
|
100
|
+
*
|
|
101
|
+
* - When `format` is `'default'`, `'rfc9457'`, or the deprecated `'rfc7807'`,
|
|
102
|
+
* the corresponding built-in formatter is returned.
|
|
103
|
+
* - When `format` is already a function, it is returned as-is.
|
|
104
|
+
* - Otherwise a `TypeError` is thrown.
|
|
105
|
+
*
|
|
106
|
+
* @param format - The error format identifier or custom formatter function
|
|
107
|
+
* @returns The resolved formatter function
|
|
108
|
+
* @throws {TypeError} When an unknown format string is provided
|
|
109
|
+
* @since 0.1.0
|
|
110
|
+
*/
|
|
111
|
+
export function selectFormatter(
|
|
112
|
+
format: ErrorFormat | ErrorHandlerFormatter = 'default',
|
|
113
|
+
): ErrorHandlerFormatter {
|
|
114
|
+
if (typeof format === 'function') {
|
|
115
|
+
return format;
|
|
116
|
+
}
|
|
117
|
+
switch (format) {
|
|
118
|
+
case 'default':
|
|
119
|
+
return defaultFormatter;
|
|
120
|
+
case 'rfc9457':
|
|
121
|
+
return rfc9457Formatter;
|
|
122
|
+
case 'rfc7807':
|
|
123
|
+
return rfc7807Formatter;
|
|
124
|
+
default:
|
|
125
|
+
throw new TypeError(`Unknown error format: "${format}"`);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shared Problem Details assembly.
|
|
3
|
+
*
|
|
4
|
+
* Both the RFC 9457 formatter and the deprecated RFC 7807 one produce the same
|
|
5
|
+
* body apart from the `type` member, so the assembly lives here once and each
|
|
6
|
+
* formatter supplies only its own {@linkcode ProblemTypeResolver}
|
|
7
|
+
* (AI_GUIDELINES §11.1).
|
|
8
|
+
*
|
|
9
|
+
* This module is internal: `buildProblemDetails` and `ProblemTypeResolver` are
|
|
10
|
+
* not exported from `src/index.ts`. `ERROR_TYPE_BASE` and `ProblemDetails` are
|
|
11
|
+
* re-exported publicly by `rfc9457-formatter.ts`.
|
|
12
|
+
*
|
|
13
|
+
* @module
|
|
14
|
+
*/
|
|
15
|
+
import type { IRequestContext } from '@setu-ts/common';
|
|
16
|
+
|
|
17
|
+
import { statusTitle } from '../errors/exceptions.ts';
|
|
18
|
+
import { HttpError } from '../errors/http-error.ts';
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* The canonical base URI for framework-produced problem type identifiers.
|
|
22
|
+
*
|
|
23
|
+
* @since 0.1.0
|
|
24
|
+
*/
|
|
25
|
+
export const ERROR_TYPE_BASE = 'https://setu-ts.dev/errors';
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* The problem type URI identifying a validation failure.
|
|
29
|
+
*
|
|
30
|
+
* Spelled to match the literal `@setu-ts/validation-plugin` emits, so both
|
|
31
|
+
* packages identify the same problem type with the same URI. The two cannot
|
|
32
|
+
* share a constant — a package may not import another package's internals
|
|
33
|
+
* (AI_GUIDELINES §2.2) — so the agreement is pinned by a test in each.
|
|
34
|
+
*
|
|
35
|
+
* @since 0.1.0
|
|
36
|
+
*/
|
|
37
|
+
export const VALIDATION_TYPE: string = `${ERROR_TYPE_BASE}/validation`;
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* The RFC 9457 default problem type, meaning "no semantics beyond the HTTP
|
|
41
|
+
* status code" (RFC 9457 §4.2).
|
|
42
|
+
*
|
|
43
|
+
* @since 0.1.0
|
|
44
|
+
*/
|
|
45
|
+
export const ABOUT_BLANK = 'about:blank';
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* A Problem Details object as defined by
|
|
49
|
+
* [RFC 9457](https://www.rfc-editor.org/rfc/rfc9457.html).
|
|
50
|
+
*
|
|
51
|
+
* Extension members beyond the five core fields are allowed (RFC 9457 §3.2),
|
|
52
|
+
* so `errors` and `stack` may be present.
|
|
53
|
+
*
|
|
54
|
+
* @since 0.1.0
|
|
55
|
+
*/
|
|
56
|
+
export interface ProblemDetails {
|
|
57
|
+
/** A URI reference identifying the problem type. */
|
|
58
|
+
readonly type: string;
|
|
59
|
+
/** A short, human-readable summary of the problem type. */
|
|
60
|
+
readonly title: string;
|
|
61
|
+
/** The HTTP status code generated for this occurrence. */
|
|
62
|
+
readonly status: number;
|
|
63
|
+
/** A human-readable explanation specific to this occurrence. */
|
|
64
|
+
readonly detail: string;
|
|
65
|
+
/** A URI reference identifying the specific occurrence (request path). */
|
|
66
|
+
readonly instance?: string;
|
|
67
|
+
/** Optional validation failures extension (present for `422` errors). */
|
|
68
|
+
readonly errors?: ReadonlyArray<{
|
|
69
|
+
field: string;
|
|
70
|
+
message: string;
|
|
71
|
+
code?: string;
|
|
72
|
+
}>;
|
|
73
|
+
/** Optional stack trace (present only when `includeStackTrace` is on). */
|
|
74
|
+
readonly stack?: string;
|
|
75
|
+
/** Allow callers to attach further extension members. */
|
|
76
|
+
readonly [key: string]: unknown;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Marks an {@linkcode HttpError} as one the M70f error responder built, and
|
|
81
|
+
* carries the calling site's disclosure.
|
|
82
|
+
*
|
|
83
|
+
* The responder must hand the disclosure to the Problem Details formatters as
|
|
84
|
+
* the `detail` member while leaving the site's title as the error `message`
|
|
85
|
+
* (which the `default` formatter emits verbatim). It cannot use
|
|
86
|
+
* `HttpError.details` alone as that channel: `details` is a free-form bag an
|
|
87
|
+
* application writes into too, so promoting any `details.detail` would change
|
|
88
|
+
* the body of a thrown error that merely uses the same key name.
|
|
89
|
+
*
|
|
90
|
+
* This marker is a module-private `Symbol` — not `Symbol.for` — because both
|
|
91
|
+
* writer ({@linkcode ../middleware/error-responder-impl.ts}) and reader
|
|
92
|
+
* ({@linkcode buildProblemDetails}) live in this package and import it
|
|
93
|
+
* directly; a global registry key would expose an internal channel that a
|
|
94
|
+
* third party could forge.
|
|
95
|
+
*
|
|
96
|
+
* @internal
|
|
97
|
+
*/
|
|
98
|
+
export const RESPONDER_DETAIL: unique symbol = Symbol('setu.exceptions.responder-detail');
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Resolves the `type` member of a Problem Details body.
|
|
102
|
+
*
|
|
103
|
+
* @param statusCode - The HTTP status code of the occurrence
|
|
104
|
+
* @param hasErrors - Whether the error carries a validation `errors` extension
|
|
105
|
+
* @returns The problem type URI
|
|
106
|
+
* @since 0.1.0
|
|
107
|
+
*/
|
|
108
|
+
export type ProblemTypeResolver = (statusCode: number, hasErrors: boolean) => string;
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Extracts the validation `errors` extension from an error's `details`, if any.
|
|
112
|
+
*
|
|
113
|
+
* @param error - The HTTP error to read
|
|
114
|
+
* @returns The validation failures, or `undefined` when the error carries none
|
|
115
|
+
*/
|
|
116
|
+
function extractErrors(error: HttpError): ProblemDetails['errors'] {
|
|
117
|
+
const details = error.details;
|
|
118
|
+
if (details === undefined || !('errors' in details)) {
|
|
119
|
+
return undefined;
|
|
120
|
+
}
|
|
121
|
+
return details.errors as ProblemDetails['errors'];
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* Assembles a Problem Details body from a thrown error.
|
|
126
|
+
*
|
|
127
|
+
* - `status` and `detail` come from the {@linkcode HttpError} (or default to
|
|
128
|
+
* `500` / the error message for generic `Error`s).
|
|
129
|
+
* - `title` is derived from the status code via the shared status-title map.
|
|
130
|
+
* - `instance` is the request path when a context is supplied.
|
|
131
|
+
* - `errors` is included when the error carries validation details.
|
|
132
|
+
* - `type` is delegated to `resolveType`, the only member the two supported
|
|
133
|
+
* specification versions disagree on.
|
|
134
|
+
*
|
|
135
|
+
* @param error - The thrown error to format
|
|
136
|
+
* @param ctx - Optional request context (used for `instance`)
|
|
137
|
+
* @param resolveType - Strategy supplying the `type` member
|
|
138
|
+
* @returns A Problem Details body
|
|
139
|
+
* @since 0.1.0
|
|
140
|
+
*/
|
|
141
|
+
export function buildProblemDetails(
|
|
142
|
+
error: Error,
|
|
143
|
+
ctx: IRequestContext | undefined,
|
|
144
|
+
resolveType: ProblemTypeResolver,
|
|
145
|
+
): ProblemDetails {
|
|
146
|
+
const isHttp = error instanceof HttpError;
|
|
147
|
+
const statusCode = isHttp ? error.statusCode : 500;
|
|
148
|
+
const errors = isHttp ? extractErrors(error) : undefined;
|
|
149
|
+
// A responder-built error carries the site's disclosure in its structured
|
|
150
|
+
// details under a `detail` key (M70f); the Problem Details `detail` member
|
|
151
|
+
// is that disclosure, falling back to the error message (the title) when a
|
|
152
|
+
// site supplied none.
|
|
153
|
+
//
|
|
154
|
+
// The promotion is gated on {@linkcode RESPONDER_DETAIL}, NOT on the mere
|
|
155
|
+
// presence of a `detail` key, because `HttpError.details` is a free-form bag
|
|
156
|
+
// an application also writes into: reading any `details.detail` would
|
|
157
|
+
// silently change the `detail` member of a thrown
|
|
158
|
+
// `new HttpError(400, 'Invalid payload', { detail: 'code-7' })` from its
|
|
159
|
+
// message to `'code-7'` — a released-behaviour change to every application
|
|
160
|
+
// that happens to use that key name (M70f code review, finding 4). Only an
|
|
161
|
+
// error the responder built carries the marker, so a thrown error keeps the
|
|
162
|
+
// pre-M70f shape: `detail` is the message.
|
|
163
|
+
const carriedDetail =
|
|
164
|
+
isHttp && RESPONDER_DETAIL in error
|
|
165
|
+
? (error as HttpError & { [RESPONDER_DETAIL]?: unknown })[RESPONDER_DETAIL]
|
|
166
|
+
: undefined;
|
|
167
|
+
const detail = typeof carriedDetail === 'string' ? carriedDetail : error.message;
|
|
168
|
+
|
|
169
|
+
return {
|
|
170
|
+
type: resolveType(statusCode, errors !== undefined),
|
|
171
|
+
title: statusTitle(statusCode),
|
|
172
|
+
status: statusCode,
|
|
173
|
+
detail,
|
|
174
|
+
...(ctx !== undefined && { instance: ctx.request.path }),
|
|
175
|
+
...(errors !== undefined && { errors }),
|
|
176
|
+
};
|
|
177
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* RFC 7807 Problem Details error formatter (deprecated).
|
|
3
|
+
*
|
|
4
|
+
* RFC 7807 was obsoleted by [RFC 9457](https://www.rfc-editor.org/rfc/rfc9457.html)
|
|
5
|
+
* in July 2023. This formatter is retained unchanged so existing callers keep
|
|
6
|
+
* their exact body shape through a deprecation period (AI_GUIDELINES §9.2); new
|
|
7
|
+
* code should use `rfc9457-formatter.ts`.
|
|
8
|
+
*
|
|
9
|
+
* It differs from the RFC 9457 formatter in the `type` member alone: this one
|
|
10
|
+
* mints a URI from the status code for every error, which RFC 9457 §4.2 replaces
|
|
11
|
+
* with `about:blank` for problems carrying no semantics beyond that status.
|
|
12
|
+
*
|
|
13
|
+
* @module
|
|
14
|
+
*/
|
|
15
|
+
import type { IRequestContext } from '@setu-ts/common';
|
|
16
|
+
|
|
17
|
+
import type { ErrorHandlerFormatter } from './error-formatter.ts';
|
|
18
|
+
import { buildProblemDetails, ERROR_TYPE_BASE, type ProblemDetails } from './problem-details.ts';
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Resolves the RFC 7807 `type` member: a URI minted from the status code.
|
|
22
|
+
*
|
|
23
|
+
* @param statusCode - The HTTP status code of the occurrence
|
|
24
|
+
* @returns The status-derived problem type URI
|
|
25
|
+
*/
|
|
26
|
+
const resolveRfc7807Type = (statusCode: number): string => `${ERROR_TYPE_BASE}/${statusCode}`;
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Format an error as RFC 7807 Problem Details.
|
|
30
|
+
*
|
|
31
|
+
* @param error - The thrown error to format
|
|
32
|
+
* @param ctx - Optional request context (used for `instance`)
|
|
33
|
+
* @returns An RFC 7807 Problem Details body
|
|
34
|
+
* @deprecated RFC 7807 was obsoleted by RFC 9457. Use `rfc9457Formatter`
|
|
35
|
+
* instead, or the `'rfc9457'` format alias. Will be removed in v1.0.0.
|
|
36
|
+
* @example
|
|
37
|
+
* ```typescript
|
|
38
|
+
* // Before
|
|
39
|
+
* app.middleware.add(errorHandler({ format: 'rfc7807' }));
|
|
40
|
+
* // After — `type` becomes 'about:blank' for status-only problems
|
|
41
|
+
* app.middleware.add(errorHandler({ format: 'rfc9457' }));
|
|
42
|
+
* ```
|
|
43
|
+
* @since 0.1.0
|
|
44
|
+
*/
|
|
45
|
+
export const rfc7807Formatter: ErrorHandlerFormatter = (
|
|
46
|
+
error: Error,
|
|
47
|
+
ctx?: IRequestContext,
|
|
48
|
+
): ProblemDetails => buildProblemDetails(error, ctx, resolveRfc7807Type);
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* RFC 9457 Problem Details error formatter.
|
|
3
|
+
*
|
|
4
|
+
* Produces a JSON body conforming to
|
|
5
|
+
* [RFC 9457](https://www.rfc-editor.org/rfc/rfc9457.html) Problem Details for
|
|
6
|
+
* HTTP APIs, which obsoletes RFC 7807. The body carries `type`, `title`,
|
|
7
|
+
* `status`, and `detail`, with `instance` derived from the request path and an
|
|
8
|
+
* optional `errors` extension for validation failures.
|
|
9
|
+
*
|
|
10
|
+
* @module
|
|
11
|
+
*/
|
|
12
|
+
import type { IRequestContext } from '@setu-ts/common';
|
|
13
|
+
|
|
14
|
+
import type { ErrorHandlerFormatter } from './error-formatter.ts';
|
|
15
|
+
import {
|
|
16
|
+
ABOUT_BLANK,
|
|
17
|
+
buildProblemDetails,
|
|
18
|
+
type ProblemDetails,
|
|
19
|
+
VALIDATION_TYPE,
|
|
20
|
+
} from './problem-details.ts';
|
|
21
|
+
|
|
22
|
+
export { ERROR_TYPE_BASE } from './problem-details.ts';
|
|
23
|
+
export type { ProblemDetails } from './problem-details.ts';
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Resolves the RFC 9457 `type` member.
|
|
27
|
+
*
|
|
28
|
+
* An {@linkcode HttpError} carries no problem-type identity beyond its status
|
|
29
|
+
* code, so a URI minted from that status would identify nothing the `status`
|
|
30
|
+
* member does not already convey — which is exactly the case RFC 9457 §4.2
|
|
31
|
+
* registers `about:blank` for. The one exception is the body produced by
|
|
32
|
+
* `validationError()`, which defines an `errors` extension member and is
|
|
33
|
+
* therefore a distinct problem type deserving its own URI.
|
|
34
|
+
*
|
|
35
|
+
* @param _statusCode - The HTTP status code (not used; the status member carries it)
|
|
36
|
+
* @param hasErrors - Whether the error carries a validation `errors` extension
|
|
37
|
+
* @returns `about:blank`, or the validation problem type URI
|
|
38
|
+
*/
|
|
39
|
+
const resolveRfc9457Type = (_statusCode: number, hasErrors: boolean): string =>
|
|
40
|
+
hasErrors ? VALIDATION_TYPE : ABOUT_BLANK;
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Format an error as RFC 9457 Problem Details.
|
|
44
|
+
*
|
|
45
|
+
* `type` is `about:blank` for problems whose only semantics are their status
|
|
46
|
+
* code, and the framework validation problem type when the error carries an
|
|
47
|
+
* `errors` extension. Every other member is assembled identically to the
|
|
48
|
+
* deprecated RFC 7807 formatter.
|
|
49
|
+
*
|
|
50
|
+
* @param error - The thrown error to format
|
|
51
|
+
* @param ctx - Optional request context (used for `instance`)
|
|
52
|
+
* @returns An RFC 9457 Problem Details body
|
|
53
|
+
* @example
|
|
54
|
+
* ```typescript
|
|
55
|
+
* rfc9457Formatter(notFound('User 42 does not exist'), ctx);
|
|
56
|
+
* // {
|
|
57
|
+
* // type: 'about:blank',
|
|
58
|
+
* // title: 'Not Found',
|
|
59
|
+
* // status: 404,
|
|
60
|
+
* // detail: 'User 42 does not exist',
|
|
61
|
+
* // instance: '/users/42',
|
|
62
|
+
* // }
|
|
63
|
+
* ```
|
|
64
|
+
* @since 0.1.0
|
|
65
|
+
*/
|
|
66
|
+
export const rfc9457Formatter: ErrorHandlerFormatter = (
|
|
67
|
+
error: Error,
|
|
68
|
+
ctx?: IRequestContext,
|
|
69
|
+
): ProblemDetails => buildProblemDetails(error, ctx, resolveRfc9457Type);
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module
|
|
3
|
+
*
|
|
4
|
+
* Exception factory functions, `HttpError`, error formatters, and the global
|
|
5
|
+
* error handler middleware.
|
|
6
|
+
*
|
|
7
|
+
* This is a **plain package** (not a plugin) — it depends on
|
|
8
|
+
* `@setu-ts/common` only and exposes types, factories, and a
|
|
9
|
+
* middleware factory. Register the middleware via the application's pipeline:
|
|
10
|
+
*
|
|
11
|
+
* ```typescript
|
|
12
|
+
* import { errorHandler } from '@setu-ts/exceptions';
|
|
13
|
+
*
|
|
14
|
+
* app.middleware.add(errorHandler({ format: 'rfc9457' }), {
|
|
15
|
+
* priority: 0,
|
|
16
|
+
* name: 'error-handler',
|
|
17
|
+
* });
|
|
18
|
+
* ```
|
|
19
|
+
*
|
|
20
|
+
* Every export here is public API and documented in PUBLIC_API.md
|
|
21
|
+
* (AI_GUIDELINES §10).
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
// Error type
|
|
25
|
+
export { HttpError } from './errors/http-error.ts';
|
|
26
|
+
export type { HttpErrorInit, ValidationError } from './errors/http-error.ts';
|
|
27
|
+
|
|
28
|
+
// Exception factories
|
|
29
|
+
export {
|
|
30
|
+
badRequest,
|
|
31
|
+
conflict,
|
|
32
|
+
forbidden,
|
|
33
|
+
internalServerError,
|
|
34
|
+
notFound,
|
|
35
|
+
notImplemented,
|
|
36
|
+
serviceUnavailable,
|
|
37
|
+
STATUS_TITLES,
|
|
38
|
+
statusTitle,
|
|
39
|
+
tooManyRequests,
|
|
40
|
+
unauthorized,
|
|
41
|
+
validationError,
|
|
42
|
+
} from './errors/exceptions.ts';
|
|
43
|
+
|
|
44
|
+
// Formatters
|
|
45
|
+
export { defaultFormatter, selectFormatter } from './formatters/error-formatter.ts';
|
|
46
|
+
export type {
|
|
47
|
+
DefaultErrorBody,
|
|
48
|
+
ErrorFormat,
|
|
49
|
+
ErrorHandlerFormatter,
|
|
50
|
+
} from './formatters/error-formatter.ts';
|
|
51
|
+
export { ERROR_TYPE_BASE, rfc9457Formatter } from './formatters/rfc9457-formatter.ts';
|
|
52
|
+
export type { ProblemDetails } from './formatters/rfc9457-formatter.ts';
|
|
53
|
+
export { rfc7807Formatter } from './formatters/rfc7807-formatter.ts';
|
|
54
|
+
|
|
55
|
+
// Middleware
|
|
56
|
+
export { errorHandler } from './middleware/error-handler.ts';
|
|
57
|
+
export type { ErrorHandlerOptions } from './middleware/error-handler.ts';
|