@venizia/ignis-inversion 0.4.7-2 → 0.4.7-4
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/dist/esm/index.d.ts
CHANGED
package/dist/esm/index.js
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import 'reflect-metadata';
|
|
2
1
|
import { getError } from './modules/error/app-error.js';
|
|
3
2
|
import { MessageCode } from './modules/error/message-code.js';
|
|
4
3
|
export * from './common/index.js';
|
|
5
4
|
export * from './modules/index.js';
|
|
6
5
|
// Lives in the package entry, the one module every consumer evaluates and the only one
|
|
7
6
|
// declared in `sideEffects`: a bundler keeps it, so MessageCode throws ApplicationError in a
|
|
8
|
-
// bundle too.
|
|
7
|
+
// bundle too. `reflect-metadata` is not imported here: a module that calls `Reflect.*Metadata`
|
|
8
|
+
// imports it itself, so a browser that only throws an error does not ship it.
|
|
9
9
|
MessageCode.useErrorFactory({ factory: getError });
|
|
10
10
|
//# sourceMappingURL=index.js.map
|
package/dist/esm/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,2BAA2B,CAAC;AACrD,OAAO,EAAE,WAAW,EAAE,MAAM,8BAA8B,CAAC;AAE3D,cAAc,UAAU,CAAC;AACzB,cAAc,WAAW,CAAC;AAE1B,uFAAuF;AACvF,6FAA6F;AAC7F,+FAA+F;AAC/F,8EAA8E;AAC9E,WAAW,CAAC,eAAe,CAAC,EAAE,OAAO,EAAE,QAAQ,EAAE,CAAC,CAAC","sourcesContent":["import { getError } from './modules/error/app-error';\nimport { MessageCode } from './modules/error/message-code';\n\nexport * from './common';\nexport * from './modules';\n\n// Lives in the package entry, the one module every consumer evaluates and the only one\n// declared in `sideEffects`: a bundler keeps it, so MessageCode throws ApplicationError in a\n// bundle too. `reflect-metadata` is not imported here: a module that calls `Reflect.*Metadata`\n// imports it itself, so a browser that only throws an error does not ship it.\nMessageCode.useErrorFactory({ factory: getError });\n"]}
|
|
@@ -1,4 +1,12 @@
|
|
|
1
1
|
import type { TError, TErrorLogLevel, TErrorNormalized, TResponsedError } from './common/index.js';
|
|
2
|
+
/**
|
|
3
|
+
* An `ApplicationError` by shape - a numeric `statusCode` and a `normalized` with its `text` - so it
|
|
4
|
+
* holds across the CJS and ESM copies of this class, where `instanceof` does not. A plain `Error`
|
|
5
|
+
* that merely carries a `statusCode` (an HTTP client's, an engine's) is not one.
|
|
6
|
+
*
|
|
7
|
+
* An error from a copy older than 0.2.2-0 matches but has no `toFilledText()`.
|
|
8
|
+
*/
|
|
9
|
+
export declare const isApplicationError: (error: unknown) => error is ApplicationError;
|
|
2
10
|
/** The error type the framework throws; `normalized` holds the resolved text, code and args a client renders. */
|
|
3
11
|
export declare class ApplicationError extends Error {
|
|
4
12
|
/** @default 400 */
|
|
@@ -14,6 +22,34 @@ export declare class ApplicationError extends Error {
|
|
|
14
22
|
*/
|
|
15
23
|
toFilledText(): string;
|
|
16
24
|
static getError(opts: TError): ApplicationError;
|
|
25
|
+
/** One property read that cannot throw: a getter or a Proxy trap that throws reads as absent. */
|
|
26
|
+
private static readProperty;
|
|
27
|
+
/**
|
|
28
|
+
* The status a wrapped value is allowed to keep, or `undefined` when it has none: an integer `statusCode` in 400-599
|
|
29
|
+
* (what `getError` sets), else an integer `status` in 400-499 (Hono's `HTTPException` and an HTTP client's error:
|
|
30
|
+
* a 5xx `status` is an upstream's, never the caller's to show).
|
|
31
|
+
*/
|
|
32
|
+
private static readWrappedStatus;
|
|
33
|
+
/** The value's own string `message` on one line (every control character escaped, as a log line must not break), else the fixed text. */
|
|
34
|
+
private static readWrappedMessage;
|
|
35
|
+
/**
|
|
36
|
+
* The `ApplicationError` for a caught value of any type: the same instance when it already is one
|
|
37
|
+
* (`isApplicationError` shape, so an error from another copy of the class counts), else a new one
|
|
38
|
+
* with the original as `cause`. Never throws, whatever the value.
|
|
39
|
+
*
|
|
40
|
+
* What the client may see depends on what the value carried:
|
|
41
|
+
* - a valid HTTP status (an integer `statusCode` in 400-599, or a `status` in 400-499) is kept, with the value's
|
|
42
|
+
* string `message`: the thrower chose to answer with it;
|
|
43
|
+
* - anything else becomes a 500 that the error middleware answers as an UNEXPECTED error: a generic message to the
|
|
44
|
+
* client in production, while the real message (one line, control characters escaped, or a fixed text when there
|
|
45
|
+
* is none) and the original as `cause` stay in the log only. A driver or engine error carries SQL, paths and
|
|
46
|
+
* hosts, so it must not reach a response by being wrapped.
|
|
47
|
+
*
|
|
48
|
+
* The marker that makes it unexpected is private (a symbol that does not enumerate), not a public flag.
|
|
49
|
+
*/
|
|
50
|
+
static fromUnknown(opts: {
|
|
51
|
+
error: unknown;
|
|
52
|
+
}): ApplicationError;
|
|
17
53
|
static fromError(opts: {
|
|
18
54
|
error: TResponsedError;
|
|
19
55
|
}): ApplicationError;
|
|
@@ -24,12 +60,4 @@ export declare const getError: (opts: TError) => ApplicationError;
|
|
|
24
60
|
export declare const fromError: (opts: {
|
|
25
61
|
error: TResponsedError;
|
|
26
62
|
}) => ApplicationError;
|
|
27
|
-
/**
|
|
28
|
-
* An `ApplicationError` by shape - a numeric `statusCode` and a `normalized` with its `text` - so it
|
|
29
|
-
* holds across the CJS and ESM copies of this class, where `instanceof` does not. A plain `Error`
|
|
30
|
-
* that merely carries a `statusCode` (an HTTP client's, an engine's) is not one.
|
|
31
|
-
*
|
|
32
|
-
* An error from a copy older than 0.2.2-0 matches but has no `toFilledText()`.
|
|
33
|
-
*/
|
|
34
|
-
export declare const isApplicationError: (error: unknown) => error is ApplicationError;
|
|
35
63
|
//# sourceMappingURL=app-error.d.ts.map
|
|
@@ -14,6 +14,38 @@ const KNOWN_KEYS = [
|
|
|
14
14
|
'logLevel',
|
|
15
15
|
'name',
|
|
16
16
|
];
|
|
17
|
+
/** The HTTP error range: a wrapped value's own status counts only inside it, so a stray 200 or 0 never becomes the response status. */
|
|
18
|
+
const MIN_WRAPPED_STATUS = 400;
|
|
19
|
+
const MAX_WRAPPED_STATUS = 599;
|
|
20
|
+
const MAX_CLIENT_STATUS = 499;
|
|
21
|
+
const DEFAULT_WRAPPED_STATUS = 500;
|
|
22
|
+
/**
|
|
23
|
+
* Set (not enumerable) on an error `fromUnknown` wrapped from a value that carried no usable HTTP status. It is the
|
|
24
|
+
* private signal the kernel error middleware reads (by this registry key, so the two packages share no export) to
|
|
25
|
+
* answer it as an UNEXPECTED error: a generic message to the client in production, the real one in the log only.
|
|
26
|
+
* A symbol key, so it never reaches `JSON.stringify`, `extra` or a response.
|
|
27
|
+
*/
|
|
28
|
+
const UNEXPECTED_ERROR_MARKER = Symbol.for('@venizia/ignis-inversion:unexpected-error');
|
|
29
|
+
/** Fixed text for a value that carries no message; the value itself is never printed, it only rides as the cause. */
|
|
30
|
+
const UNKNOWN_VALUE_MESSAGE = 'An unknown value was thrown';
|
|
31
|
+
/**
|
|
32
|
+
* An `ApplicationError` by shape - a numeric `statusCode` and a `normalized` with its `text` - so it
|
|
33
|
+
* holds across the CJS and ESM copies of this class, where `instanceof` does not. A plain `Error`
|
|
34
|
+
* that merely carries a `statusCode` (an HTTP client's, an engine's) is not one.
|
|
35
|
+
*
|
|
36
|
+
* An error from a copy older than 0.2.2-0 matches but has no `toFilledText()`.
|
|
37
|
+
*/
|
|
38
|
+
export const isApplicationError = (error) => {
|
|
39
|
+
if (!(error instanceof Error) || !('statusCode' in error) || !('normalized' in error)) {
|
|
40
|
+
return false;
|
|
41
|
+
}
|
|
42
|
+
const { statusCode, normalized } = error;
|
|
43
|
+
return (typeof statusCode === 'number' &&
|
|
44
|
+
typeof normalized === 'object' &&
|
|
45
|
+
normalized !== null &&
|
|
46
|
+
'text' in normalized &&
|
|
47
|
+
typeof normalized.text === 'string');
|
|
48
|
+
};
|
|
17
49
|
/** The error type the framework throws; `normalized` holds the resolved text, code and args a client renders. */
|
|
18
50
|
export class ApplicationError extends Error {
|
|
19
51
|
constructor(opts) {
|
|
@@ -59,6 +91,99 @@ export class ApplicationError extends Error {
|
|
|
59
91
|
static getError(opts) {
|
|
60
92
|
return new ApplicationError(opts);
|
|
61
93
|
}
|
|
94
|
+
/** One property read that cannot throw: a getter or a Proxy trap that throws reads as absent. */
|
|
95
|
+
static readProperty(opts) {
|
|
96
|
+
const { value, key } = opts;
|
|
97
|
+
// A primitive holds no own data; `null` is the one object that cannot be read.
|
|
98
|
+
if ((typeof value !== 'object' && typeof value !== 'function') || value === null) {
|
|
99
|
+
return undefined;
|
|
100
|
+
}
|
|
101
|
+
try {
|
|
102
|
+
return Reflect.get(value, key);
|
|
103
|
+
// eslint-disable-next-line venizia/no-silent-catch -- the contract is "never throws": a throwing getter or Proxy trap reads as absent
|
|
104
|
+
}
|
|
105
|
+
catch {
|
|
106
|
+
return undefined;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* The status a wrapped value is allowed to keep, or `undefined` when it has none: an integer `statusCode` in 400-599
|
|
111
|
+
* (what `getError` sets), else an integer `status` in 400-499 (Hono's `HTTPException` and an HTTP client's error:
|
|
112
|
+
* a 5xx `status` is an upstream's, never the caller's to show).
|
|
113
|
+
*/
|
|
114
|
+
static readWrappedStatus(opts) {
|
|
115
|
+
const { value } = opts;
|
|
116
|
+
const limits = [
|
|
117
|
+
{ key: 'statusCode', max: MAX_WRAPPED_STATUS },
|
|
118
|
+
{ key: 'status', max: MAX_CLIENT_STATUS },
|
|
119
|
+
];
|
|
120
|
+
for (const { key, max } of limits) {
|
|
121
|
+
const candidate = ApplicationError.readProperty({ value, key });
|
|
122
|
+
if (typeof candidate === 'number' &&
|
|
123
|
+
Number.isInteger(candidate) &&
|
|
124
|
+
candidate >= MIN_WRAPPED_STATUS &&
|
|
125
|
+
candidate <= max) {
|
|
126
|
+
return candidate;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
return undefined;
|
|
130
|
+
}
|
|
131
|
+
/** The value's own string `message` on one line (every control character escaped, as a log line must not break), else the fixed text. */
|
|
132
|
+
static readWrappedMessage(opts) {
|
|
133
|
+
const message = ApplicationError.readProperty({ value: opts.value, key: 'message' });
|
|
134
|
+
if (typeof message !== 'string' || message.trim().length === 0) {
|
|
135
|
+
return UNKNOWN_VALUE_MESSAGE;
|
|
136
|
+
}
|
|
137
|
+
let safe = '';
|
|
138
|
+
let copiedUpTo = 0;
|
|
139
|
+
for (let index = 0; index < message.length; index++) {
|
|
140
|
+
const code = message.charCodeAt(index);
|
|
141
|
+
const isUnsafe = code < 0x20 || (code >= 0x7f && code <= 0x9f) || code === 0x2028 || code === 0x2029;
|
|
142
|
+
if (!isUnsafe) {
|
|
143
|
+
continue;
|
|
144
|
+
}
|
|
145
|
+
safe += `${message.slice(copiedUpTo, index)}\\u${code.toString(16).padStart(4, '0')}`;
|
|
146
|
+
copiedUpTo = index + 1;
|
|
147
|
+
}
|
|
148
|
+
return copiedUpTo === 0 ? message : safe + message.slice(copiedUpTo);
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* The `ApplicationError` for a caught value of any type: the same instance when it already is one
|
|
152
|
+
* (`isApplicationError` shape, so an error from another copy of the class counts), else a new one
|
|
153
|
+
* with the original as `cause`. Never throws, whatever the value.
|
|
154
|
+
*
|
|
155
|
+
* What the client may see depends on what the value carried:
|
|
156
|
+
* - a valid HTTP status (an integer `statusCode` in 400-599, or a `status` in 400-499) is kept, with the value's
|
|
157
|
+
* string `message`: the thrower chose to answer with it;
|
|
158
|
+
* - anything else becomes a 500 that the error middleware answers as an UNEXPECTED error: a generic message to the
|
|
159
|
+
* client in production, while the real message (one line, control characters escaped, or a fixed text when there
|
|
160
|
+
* is none) and the original as `cause` stay in the log only. A driver or engine error carries SQL, paths and
|
|
161
|
+
* hosts, so it must not reach a response by being wrapped.
|
|
162
|
+
*
|
|
163
|
+
* The marker that makes it unexpected is private (a symbol that does not enumerate), not a public flag.
|
|
164
|
+
*/
|
|
165
|
+
static fromUnknown(opts) {
|
|
166
|
+
const { error } = opts;
|
|
167
|
+
try {
|
|
168
|
+
if (error instanceof ApplicationError || isApplicationError(error)) {
|
|
169
|
+
return error;
|
|
170
|
+
}
|
|
171
|
+
// eslint-disable-next-line venizia/no-silent-catch -- a Proxy or hostile getter threw while the shape was checked: it is foreign, so it is wrapped below
|
|
172
|
+
}
|
|
173
|
+
catch {
|
|
174
|
+
// Falls through to the wrap.
|
|
175
|
+
}
|
|
176
|
+
const status = ApplicationError.readWrappedStatus({ value: error });
|
|
177
|
+
const wrapped = ApplicationError.getError({
|
|
178
|
+
message: ApplicationError.readWrappedMessage({ value: error }),
|
|
179
|
+
statusCode: status ?? DEFAULT_WRAPPED_STATUS,
|
|
180
|
+
cause: error,
|
|
181
|
+
});
|
|
182
|
+
if (status === undefined) {
|
|
183
|
+
Object.defineProperty(wrapped, UNEXPECTED_ERROR_MARKER, { value: true, enumerable: false });
|
|
184
|
+
}
|
|
185
|
+
return wrapped;
|
|
186
|
+
}
|
|
62
187
|
static fromError(opts) {
|
|
63
188
|
const { error } = opts;
|
|
64
189
|
return this.getError({
|
|
@@ -77,22 +202,4 @@ export class ApplicationError extends Error {
|
|
|
77
202
|
export const getError = (opts) => ApplicationError.getError(opts);
|
|
78
203
|
/** Rebuilds an `ApplicationError` from the wire payload a client received; same as `ApplicationError.fromError`. */
|
|
79
204
|
export const fromError = (opts) => ApplicationError.fromError(opts);
|
|
80
|
-
/**
|
|
81
|
-
* An `ApplicationError` by shape - a numeric `statusCode` and a `normalized` with its `text` - so it
|
|
82
|
-
* holds across the CJS and ESM copies of this class, where `instanceof` does not. A plain `Error`
|
|
83
|
-
* that merely carries a `statusCode` (an HTTP client's, an engine's) is not one.
|
|
84
|
-
*
|
|
85
|
-
* An error from a copy older than 0.2.2-0 matches but has no `toFilledText()`.
|
|
86
|
-
*/
|
|
87
|
-
export const isApplicationError = (error) => {
|
|
88
|
-
if (!(error instanceof Error) || !('statusCode' in error) || !('normalized' in error)) {
|
|
89
|
-
return false;
|
|
90
|
-
}
|
|
91
|
-
const { statusCode, normalized } = error;
|
|
92
|
-
return (typeof statusCode === 'number' &&
|
|
93
|
-
typeof normalized === 'object' &&
|
|
94
|
-
normalized !== null &&
|
|
95
|
-
'text' in normalized &&
|
|
96
|
-
typeof normalized.text === 'string');
|
|
97
|
-
};
|
|
98
205
|
//# sourceMappingURL=app-error.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"app-error.js","sourceRoot":"","sources":["../../../../src/modules/error/app-error.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;AACnD,OAAO,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAC;AAC7C,OAAO,EAAE,eAAe,EAAE,MAAM,oBAAoB,CAAC;AAGrD,yDAAyD;AACzD,MAAM,UAAU,GAAG;IACjB,OAAO;IACP,SAAS;IACT,aAAa;IACb,YAAY;IACZ,aAAa;IACb,OAAO;IACP,OAAO;IACP,WAAW;IACX,UAAU;IACV,MAAM;CACE,CAAC;AAEX,iHAAiH;AACjH,MAAM,OAAO,gBAAiB,SAAQ,KAAK;IAOzC,YAAY,IAAY;QACtB,MAAM,EAAE,UAAU,EAAE,WAAW,EAAE,KAAK,EAAE,KAAK,EAAE,SAAS,EAAE,QAAQ,EAAE,GAAG,IAAI,CAAC;QAE5E,2EAA2E;QAC3E,MAAM,UAAU,GAAG,OAAO,IAAI,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,SAAS,CAAC;QAE5D,MAAM,KAAK,GAAG,IAAI,CAAC,OAAO,CAAC;QAC3B,MAAM,QAAQ,GAAG,OAAO,KAAK,KAAK,QAAQ,CAAC,CAAC,CAAC,EAAE,IAAI,EAAE,KAAK,EAAE,CAAC,CAAC,CAAC,KAAK,CAAC;QAEr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AC,KAAK,YAAY,KAAK,CAAC,IAAI,CAAC,CAAC,YAAY,IAAI,KAAK,CAAC,IAAI,CAAC,CAAC,YAAY,IAAI,KAAK,CAAC,EAAE,CAAC;QACtF,OAAO,KAAK,CAAC;IACf,CAAC;IAED,MAAM,EAAE,UAAU,EAAE,UAAU,EAAE,GAAG,KAAK,CAAC;IAEzC,OAAO,CACL,OAAO,UAAU,KAAK,QAAQ;QAC9B,OAAO,UAAU,KAAK,QAAQ;QAC9B,UAAU,KAAK,IAAI;QACnB,MAAM,IAAI,UAAU;QACpB,OAAO,UAAU,CAAC,IAAI,KAAK,QAAQ,CACpC,CAAC;AACJ,CAAC,CAAC","sourcesContent":["import { type TNullable } from '@/common/types';\nimport { ObjectUtility } from '@/common/utilities';\nimport { MessageCode } from './message-code';\nimport { MessageTemplate } from './message-template';\nimport type { TError, TErrorLogLevel, TErrorNormalized, TResponsedError } from './common';\n\n/** Consumed keys. Everything else rides into `extra`. */\nconst KNOWN_KEYS = [\n 'error',\n 'message',\n 'messageCode',\n 'statusCode',\n 'messageArgs',\n 'cause',\n 'extra',\n 'transform',\n 'logLevel',\n 'name',\n] as const;\n\n/** The error type the framework throws; `normalized` holds the resolved text, code and args a client renders. */\nexport class ApplicationError extends Error {\n /** @default 400 */\n statusCode: number;\n normalized: TErrorNormalized;\n extra?: Record<string, unknown>;\n readonly logLevel?: TErrorLogLevel;\n\n constructor(opts: TError) {\n const { statusCode, messageArgs, cause, extra, transform, logLevel } = opts;\n\n // Index signature defeats `in` narrowing - read the discriminant and cast.\n const definition = 'error' in opts ? opts.error : undefined;\n\n const input = opts.message;\n const override = typeof input === 'string' ? { text: input } : input;\n\n // `?.message?.` has to guard BOTH: `error` reaches here from an untyped call site as anything at all, and a bare `definition?.message.text` throws on a malformed one.\n const message = override?.text ?? definition?.message?.text ?? '';\n const messageCode = MessageCode.resolve(\n override?.code ?? definition?.message?.code ?? (opts.messageCode as TNullable<string>),\n );\n const args = override?.args ?? messageArgs ?? definition?.message?.args;\n\n super(message, cause === undefined ? undefined : { cause });\n\n this.statusCode = statusCode ?? definition?.statusCode ?? 400;\n this.logLevel = logLevel;\n\n // Explicit `extra` wins over swept keys.\n const merged = {\n ...ObjectUtility.omit({ source: opts, keys: KNOWN_KEYS }),\n ...extra,\n };\n this.extra = Object.keys(merged).length > 0 ? merged : undefined;\n\n const normalized: TErrorNormalized = {\n text: message,\n code: messageCode,\n args: args ?? {},\n };\n\n this.normalized = transform\n ? transform({\n message: normalized,\n statusCode: this.statusCode,\n extra: this.extra,\n })\n : normalized;\n }\n\n /**\n * The message with its args filled in: `Only 2 left of TRA-01`. `message` itself stays the template,\n * so a log or an error tracker never prints a value outside the args it redacts by key - call this\n * where a plain reader stores or shows the text, such as a queue's failure reason.\n */\n toFilledText(): string {\n return MessageTemplate.render({ text: this.message, args: this.normalized.args }).text;\n }\n\n static getError(opts: TError) {\n return new ApplicationError(opts);\n }\n\n static fromError(opts: { error: TResponsedError }): ApplicationError {\n const { error } = opts;\n\n return this.getError({\n message: {\n text: error.normalized?.text ?? error.message ?? '',\n code: error.normalized?.code,\n args: error.normalized?.args,\n },\n statusCode: error.statusCode,\n extra: error.extra,\n ...(error.requestId === undefined ? {} : { requestId: error.requestId }),\n });\n }\n}\n\n/** Builds an `ApplicationError`; same as `ApplicationError.getError`. */\nexport const getError = (opts: TError): ApplicationError => ApplicationError.getError(opts);\n\n/** Rebuilds an `ApplicationError` from the wire payload a client received; same as `ApplicationError.fromError`. */\nexport const fromError = (opts: { error: TResponsedError }): ApplicationError =>\n ApplicationError.fromError(opts);\n\n/**\n * An `ApplicationError` by shape - a numeric `statusCode` and a `normalized` with its `text` - so it\n * holds across the CJS and ESM copies of this class, where `instanceof` does not. A plain `Error`\n * that merely carries a `statusCode` (an HTTP client's, an engine's) is not one.\n *\n * An error from a copy older than 0.2.2-0 matches but has no `toFilledText()`.\n */\nexport const isApplicationError = (error: unknown): error is ApplicationError => {\n if (!(error instanceof Error) || !('statusCode' in error) || !('normalized' in error)) {\n return false;\n }\n\n const { statusCode, normalized } = error;\n\n return (\n typeof statusCode === 'number' &&\n typeof normalized === 'object' &&\n normalized !== null &&\n 'text' in normalized &&\n typeof normalized.text === 'string'\n );\n};\n"]}
|
|
1
|
+
{"version":3,"file":"app-error.js","sourceRoot":"","sources":["../../../../src/modules/error/app-error.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;AACnD,OAAO,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAC;AAC7C,OAAO,EAAE,eAAe,EAAE,MAAM,oBAAoB,CAAC;AAGrD,yDAAyD;AACzD,MAAM,UAAU,GAAG;IACjB,OAAO;IACP,SAAS;IACT,aAAa;IACb,YAAY;IACZ,aAAa;IACb,OAAO;IACP,OAAO;IACP,WAAW;IACX,UAAU;IACV,MAAM;CACE,CAAC;AAEX,uIAAuI;AACvI,MAAM,kBAAkB,GAAG,GAAG,CAAC;AAC/B,MAAM,kBAAkB,GAAG,GAAG,CAAC;AAC/B,MAAM,iBAAiB,GAAG,GAAG,CAAC;AAC9B,MAAM,sBAAsB,GAAG,GAAG,CAAC;AAEnC;;;;;GAKG;AACH,MAAM,uBAAuB,GAAG,MAAM,CAAC,GAAG,CAAC,2CAA2C,CAAC,CAAC;AAExF,qHAAqH;AACrH,MAAM,qBAAqB,GAAG,6BAA6B,CAAC;AAE5D;;;;;;GAMG;AACH,MAAM,CAAC,MAAM,kBAAkB,GAAG,CAAC,KAAc,EAA6B,EAAE;IAC9E,IAAI,CAAC,CAAC,KAAK,YAAY,KAAK,CAAC,IAAI,CAAC,CAAC,YAAY,IAAI,KAAK,CAAC,IAAI,CAAC,CAAC,YAAY,IAAI,KAAK,CAAC,EAAE,CAAC;QACtF,OAAO,KAAK,CAAC;IACf,CAAC;IAED,MAAM,EAAE,UAAU,EAAE,UAAU,EAAE,GAAG,KAAK,CAAC;IAEzC,OAAO,CACL,OAAO,UAAU,KAAK,QAAQ;QAC9B,OAAO,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from './message-code';\nimport { MessageTemplate } from './message-template';\nimport type { TError, TErrorLogLevel, TErrorNormalized, TResponsedError } from './common';\n\n/** Consumed keys. Everything else rides into `extra`. */\nconst KNOWN_KEYS = [\n 'error',\n 'message',\n 'messageCode',\n 'statusCode',\n 'messageArgs',\n 'cause',\n 'extra',\n 'transform',\n 'logLevel',\n 'name',\n] as const;\n\n/** The HTTP error range: a wrapped value's own status counts only inside it, so a stray 200 or 0 never becomes the response status. */\nconst MIN_WRAPPED_STATUS = 400;\nconst MAX_WRAPPED_STATUS = 599;\nconst MAX_CLIENT_STATUS = 499;\nconst DEFAULT_WRAPPED_STATUS = 500;\n\n/**\n * Set (not enumerable) on an error `fromUnknown` wrapped from a value that carried no usable HTTP status. It is the\n * private signal the kernel error middleware reads (by this registry key, so the two packages share no export) to\n * answer it as an UNEXPECTED error: a generic message to the client in production, the real one in the log only.\n * A symbol key, so it never reaches `JSON.stringify`, `extra` or a response.\n */\nconst UNEXPECTED_ERROR_MARKER = Symbol.for('@venizia/ignis-inversion:unexpected-error');\n\n/** Fixed text for a value that carries no message; the value itself is never printed, it only rides as the cause. */\nconst UNKNOWN_VALUE_MESSAGE = 'An unknown value was thrown';\n\n/**\n * An `ApplicationError` by shape - a numeric `statusCode` and a `normalized` with its `text` - so it\n * holds across the CJS and ESM copies of this class, where `instanceof` does not. A plain `Error`\n * that merely carries a `statusCode` (an HTTP client's, an engine's) is not one.\n *\n * An error from a copy older than 0.2.2-0 matches but has no `toFilledText()`.\n */\nexport const isApplicationError = (error: unknown): error is ApplicationError => {\n if (!(error instanceof Error) || !('statusCode' in error) || !('normalized' in error)) {\n return false;\n }\n\n const { statusCode, normalized } = error;\n\n return (\n typeof statusCode === 'number' &&\n typeof normalized === 'object' &&\n normalized !== null &&\n 'text' in normalized &&\n typeof normalized.text === 'string'\n );\n};\n\n/** The error type the framework throws; `normalized` holds the resolved text, code and args a client renders. */\nexport class ApplicationError extends Error {\n /** @default 400 */\n statusCode: number;\n normalized: TErrorNormalized;\n extra?: Record<string, unknown>;\n readonly logLevel?: TErrorLogLevel;\n\n constructor(opts: TError) {\n const { statusCode, messageArgs, cause, extra, transform, logLevel } = opts;\n\n // Index signature defeats `in` narrowing - read the discriminant and cast.\n const definition = 'error' in opts ? opts.error : undefined;\n\n const input = opts.message;\n const override = typeof input === 'string' ? { text: input } : input;\n\n // `?.message?.` has to guard BOTH: `error` reaches here from an untyped call site as anything at all, and a bare `definition?.message.text` throws on a malformed one.\n const message = override?.text ?? definition?.message?.text ?? '';\n const messageCode = MessageCode.resolve(\n override?.code ?? definition?.message?.code ?? (opts.messageCode as TNullable<string>),\n );\n const args = override?.args ?? messageArgs ?? definition?.message?.args;\n\n super(message, cause === undefined ? undefined : { cause });\n\n this.statusCode = statusCode ?? definition?.statusCode ?? 400;\n this.logLevel = logLevel;\n\n // Explicit `extra` wins over swept keys.\n const merged = {\n ...ObjectUtility.omit({ source: opts, keys: KNOWN_KEYS }),\n ...extra,\n };\n this.extra = Object.keys(merged).length > 0 ? merged : undefined;\n\n const normalized: TErrorNormalized = {\n text: message,\n code: messageCode,\n args: args ?? {},\n };\n\n this.normalized = transform\n ? transform({\n message: normalized,\n statusCode: this.statusCode,\n extra: this.extra,\n })\n : normalized;\n }\n\n /**\n * The message with its args filled in: `Only 2 left of TRA-01`. `message` itself stays the template,\n * so a log or an error tracker never prints a value outside the args it redacts by key - call this\n * where a plain reader stores or shows the text, such as a queue's failure reason.\n */\n toFilledText(): string {\n return MessageTemplate.render({ text: this.message, args: this.normalized.args }).text;\n }\n\n static getError(opts: TError) {\n return new ApplicationError(opts);\n }\n\n /** One property read that cannot throw: a getter or a Proxy trap that throws reads as absent. */\n private static readProperty(opts: { value: unknown; key: string }): unknown {\n const { value, key } = opts;\n\n // A primitive holds no own data; `null` is the one object that cannot be read.\n if ((typeof value !== 'object' && typeof value !== 'function') || value === null) {\n return undefined;\n }\n\n try {\n return Reflect.get(value, key);\n // eslint-disable-next-line venizia/no-silent-catch -- the contract is \"never throws\": a throwing getter or Proxy trap reads as absent\n } catch {\n return undefined;\n }\n }\n\n /**\n * The status a wrapped value is allowed to keep, or `undefined` when it has none: an integer `statusCode` in 400-599\n * (what `getError` sets), else an integer `status` in 400-499 (Hono's `HTTPException` and an HTTP client's error:\n * a 5xx `status` is an upstream's, never the caller's to show).\n */\n private static readWrappedStatus(opts: { value: unknown }): number | undefined {\n const { value } = opts;\n const limits = [\n { key: 'statusCode', max: MAX_WRAPPED_STATUS },\n { key: 'status', max: MAX_CLIENT_STATUS },\n ];\n\n for (const { key, max } of limits) {\n const candidate = ApplicationError.readProperty({ value, key });\n\n if (\n typeof candidate === 'number' &&\n Number.isInteger(candidate) &&\n candidate >= MIN_WRAPPED_STATUS &&\n candidate <= max\n ) {\n return candidate;\n }\n }\n\n return undefined;\n }\n\n /** The value's own string `message` on one line (every control character escaped, as a log line must not break), else the fixed text. */\n private static readWrappedMessage(opts: { value: unknown }): string {\n const message = ApplicationError.readProperty({ value: opts.value, key: 'message' });\n\n if (typeof message !== 'string' || message.trim().length === 0) {\n return UNKNOWN_VALUE_MESSAGE;\n }\n\n let safe = '';\n let copiedUpTo = 0;\n\n for (let index = 0; index < message.length; index++) {\n const code = message.charCodeAt(index);\n const isUnsafe =\n code < 0x20 || (code >= 0x7f && code <= 0x9f) || code === 0x2028 || code === 0x2029;\n\n if (!isUnsafe) {\n continue;\n }\n\n safe += `${message.slice(copiedUpTo, index)}\\\\u${code.toString(16).padStart(4, '0')}`;\n copiedUpTo = index + 1;\n }\n\n return copiedUpTo === 0 ? message : safe + message.slice(copiedUpTo);\n }\n\n /**\n * The `ApplicationError` for a caught value of any type: the same instance when it already is one\n * (`isApplicationError` shape, so an error from another copy of the class counts), else a new one\n * with the original as `cause`. Never throws, whatever the value.\n *\n * What the client may see depends on what the value carried:\n * - a valid HTTP status (an integer `statusCode` in 400-599, or a `status` in 400-499) is kept, with the value's\n * string `message`: the thrower chose to answer with it;\n * - anything else becomes a 500 that the error middleware answers as an UNEXPECTED error: a generic message to the\n * client in production, while the real message (one line, control characters escaped, or a fixed text when there\n * is none) and the original as `cause` stay in the log only. A driver or engine error carries SQL, paths and\n * hosts, so it must not reach a response by being wrapped.\n *\n * The marker that makes it unexpected is private (a symbol that does not enumerate), not a public flag.\n */\n static fromUnknown(opts: { error: unknown }): ApplicationError {\n const { error } = opts;\n\n try {\n if (error instanceof ApplicationError || isApplicationError(error)) {\n return error;\n }\n // eslint-disable-next-line venizia/no-silent-catch -- a Proxy or hostile getter threw while the shape was checked: it is foreign, so it is wrapped below\n } catch {\n // Falls through to the wrap.\n }\n\n const status = ApplicationError.readWrappedStatus({ value: error });\n const wrapped = ApplicationError.getError({\n message: ApplicationError.readWrappedMessage({ value: error }),\n statusCode: status ?? DEFAULT_WRAPPED_STATUS,\n cause: error,\n });\n\n if (status === undefined) {\n Object.defineProperty(wrapped, UNEXPECTED_ERROR_MARKER, { value: true, enumerable: false });\n }\n\n return wrapped;\n }\n\n static fromError(opts: { error: TResponsedError }): ApplicationError {\n const { error } = opts;\n\n return this.getError({\n message: {\n text: error.normalized?.text ?? error.message ?? '',\n code: error.normalized?.code,\n args: error.normalized?.args,\n },\n statusCode: error.statusCode,\n extra: error.extra,\n ...(error.requestId === undefined ? {} : { requestId: error.requestId }),\n });\n }\n}\n\n/** Builds an `ApplicationError`; same as `ApplicationError.getError`. */\nexport const getError = (opts: TError): ApplicationError => ApplicationError.getError(opts);\n\n/** Rebuilds an `ApplicationError` from the wire payload a client received; same as `ApplicationError.fromError`. */\nexport const fromError = (opts: { error: TResponsedError }): ApplicationError =>\n ApplicationError.fromError(opts);\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@venizia/ignis-inversion",
|
|
3
|
-
"version": "0.4.7-
|
|
3
|
+
"version": "0.4.7-4",
|
|
4
4
|
"description": "A small, standalone Dependency Injection and Inversion of Control container for TypeScript. Features decorator-based constructor and property injection, fluent Binding API, singleton/transient scoping, namespace auto-tagging, Provider pattern, tag-based discovery, and reflect-metadata integration. Zero framework lock-in — works standalone or with Ignis.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"architecture",
|
|
@@ -88,7 +88,7 @@
|
|
|
88
88
|
},
|
|
89
89
|
"devDependencies": {
|
|
90
90
|
"@types/bun": "^1.4.3",
|
|
91
|
-
"@venizia/dev-configs": "0.4.8-
|
|
91
|
+
"@venizia/dev-configs": "0.4.8-9",
|
|
92
92
|
"eslint": "^10.13.0",
|
|
93
93
|
"oxlint": "1.89.0",
|
|
94
94
|
"oxlint-tsgolint": "7.0.2004",
|