@nyssa-ai/result 0.0.0-stage → 0.1.1
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 +111 -2
- package/dist/Err.d.ts +53 -0
- package/dist/Err.d.ts.map +1 -0
- package/dist/Err.js +172 -0
- package/dist/Err.js.map +1 -0
- package/dist/Error.d.ts +47 -0
- package/dist/Error.d.ts.map +1 -0
- package/dist/Error.js +306 -0
- package/dist/Error.js.map +1 -0
- package/dist/ErrorCategory.d.ts +27 -0
- package/dist/ErrorCategory.d.ts.map +1 -0
- package/dist/ErrorCategory.js +25 -0
- package/dist/ErrorCategory.js.map +1 -0
- package/dist/Guard.d.ts +54 -0
- package/dist/Guard.d.ts.map +1 -0
- package/dist/Guard.js +229 -0
- package/dist/Guard.js.map +1 -0
- package/dist/Ok.d.ts +66 -0
- package/dist/Ok.d.ts.map +1 -0
- package/dist/Ok.js +151 -0
- package/dist/Ok.js.map +1 -0
- package/dist/Result.d.ts +112 -0
- package/dist/Result.d.ts.map +1 -0
- package/dist/Result.js +195 -0
- package/dist/Result.js.map +1 -0
- package/dist/ResultException.d.ts +13 -0
- package/dist/ResultException.d.ts.map +1 -0
- package/dist/ResultException.js +30 -0
- package/dist/ResultException.js.map +1 -0
- package/dist/index.d.ts +81 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +58 -0
- package/dist/index.js.map +1 -0
- package/package.json +48 -4
package/README.md
CHANGED
|
@@ -1,3 +1,112 @@
|
|
|
1
|
-
#
|
|
1
|
+
# NyssaAI.Result (TypeScript)
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
`@nyssa-ai/result` gives TypeScript applications a consistent way to report whether an operation succeeded and, when it did not, what went wrong. A function returns `Result<T, E>` holding either the successful payload (`Ok<T>`) or structured errors (`Err<E>`).
|
|
4
|
+
|
|
5
|
+
The library implements Railway-Oriented Programming (ROP) with pure combinators and explicit exception boundaries, adhering to Jeremy Maynor's `dev-standards` (v0.6.3).
|
|
6
|
+
|
|
7
|
+
---
|
|
8
|
+
|
|
9
|
+
## Core Invariant: Pure Combinators vs. Explicit Exception Boundaries
|
|
10
|
+
|
|
11
|
+
> **Fail fast on programmer defects; use `Result` for expected operational outcomes.**
|
|
12
|
+
> Unexpected exceptions (e.g., `TypeError`, `ReferenceError`, `RangeError`, coding defects) bubble immediately.
|
|
13
|
+
> External fallible I/O (e.g., HTTP drivers, database adapters, filesystem operations) must be wrapped exclusively at explicit boundaries via `Result.try` / `Result.tryAsync` (or `try_` / `tryAsync`).
|
|
14
|
+
|
|
15
|
+
### 1. Pure Monadic Combinators (`then`, `thenAsync`, `transform` / `map`, `transformAsync` / `mapAsync`)
|
|
16
|
+
Monadic chaining methods do **not** swallow or intercept unexpected exceptions in callbacks:
|
|
17
|
+
- If the current result is a failure (`ok === false`), errors propagate immediately without executing the callback.
|
|
18
|
+
- If the current result is a success (`ok === true`), the callback is executed directly. Any unexpected exception bubbles up the stack immediately so unit tests and telemetry identify the defect immediately.
|
|
19
|
+
|
|
20
|
+
### 2. Explicit Exception Boundaries (`Result.try` / `try_`, `Result.tryAsync` / `tryAsync`)
|
|
21
|
+
Volatile, external fallible boundaries catch exceptions and convert them into structured failures:
|
|
22
|
+
- Standard translation maps exceptions to immutable `Error` value objects while preserving the causal chain (`cause`) and stack trace without retaining live exception references.
|
|
23
|
+
- Custom error handlers (`errorHandler: (error: unknown) => Error`) allow translation to specific categories.
|
|
24
|
+
- Cancellation evidence (`AbortSignal`, `TimeoutError`, cancellation tokens) classifies outcomes accurately as `ErrorCategory.Canceled` vs. `ErrorCategory.Unavailable`.
|
|
25
|
+
|
|
26
|
+
---
|
|
27
|
+
|
|
28
|
+
## Quickstart
|
|
29
|
+
|
|
30
|
+
```typescript
|
|
31
|
+
import { Error, ErrorCategory, Guard, Result, ok, fail, try_ } from "@nyssa-ai/result";
|
|
32
|
+
|
|
33
|
+
// 1. Success and Failure factories
|
|
34
|
+
const resOk = Result.ok(42);
|
|
35
|
+
const resFail = Result.fail(Error.fromMessage("Entity not found", ErrorCategory.NotFound));
|
|
36
|
+
|
|
37
|
+
// Shorthand helpers
|
|
38
|
+
const s = ok("payload");
|
|
39
|
+
const f = fail("something broke");
|
|
40
|
+
|
|
41
|
+
// 2. Discriminated Union Narrowing
|
|
42
|
+
if (resOk.ok) {
|
|
43
|
+
console.log(`Value: ${resOk.value}`);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
if (!resFail.ok) {
|
|
47
|
+
for (const err of resFail.errors) {
|
|
48
|
+
console.log(`[${err.category}] ${err.userMessage}: ${err.technicalMessage}`);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// 3. Monadic Chaining (Railway-Oriented Pipeline)
|
|
53
|
+
const pipelineResult = Guard.against
|
|
54
|
+
.nullOrWhitespace(username, "username")
|
|
55
|
+
.then((validName) => findUser(validName))
|
|
56
|
+
.transform((user) => user.toDto());
|
|
57
|
+
|
|
58
|
+
// 4. Explicit Exception Boundary for External I/O
|
|
59
|
+
const dbResult = try_(
|
|
60
|
+
() => databaseDriver.queryUser(userId),
|
|
61
|
+
(exc) =>
|
|
62
|
+
Error.fromMessages(
|
|
63
|
+
"Database service currently unavailable.",
|
|
64
|
+
`Driver failure: ${String(exc)}`,
|
|
65
|
+
ErrorCategory.Unavailable
|
|
66
|
+
)
|
|
67
|
+
);
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
---
|
|
71
|
+
|
|
72
|
+
## API Overview
|
|
73
|
+
|
|
74
|
+
### `Result<T, E = Error> = Ok<T> | Err<E>`
|
|
75
|
+
- `ok: boolean`: Discriminator (`true` on `Ok<T>`, `false` on `Err<E>`).
|
|
76
|
+
- `value: T`: The success payload on `Ok<T>`.
|
|
77
|
+
- `error: E`: The primary error on `Err<E>`.
|
|
78
|
+
- `errors: readonly Error[]`: Structured failures (empty on `Ok<T>`).
|
|
79
|
+
- `isSuccess / isFailure: boolean`: Boolean status flags.
|
|
80
|
+
- `then(nextOperation)`: Monadic bind (synchronous, pure).
|
|
81
|
+
- `thenAsync(nextOperation)`: Monadic bind (asynchronous, pure).
|
|
82
|
+
- `transform(fn)` / `map(fn)`: Functor projection map (synchronous, pure).
|
|
83
|
+
- `transformAsync(fn)` / `mapAsync(fn)`: Functor projection map (asynchronous, pure).
|
|
84
|
+
- `resolve(onSuccess, onFailure)`: Fold / match into a unified value.
|
|
85
|
+
- `resolveAsync(onSuccess, onFailure)`: Async fold / match.
|
|
86
|
+
- `throwIfError()` / `unwrap()`: Returns payload on success, or throws `ResultException`.
|
|
87
|
+
- `tryGetValue()`: Safe extraction tuple `readonly [true, T] | readonly [false, undefined]`.
|
|
88
|
+
- `try_(action, errorHandler?, cancellationToken?)`: Synchronous explicit boundary.
|
|
89
|
+
- `tryAsync(action, errorHandler?, cancellationToken?)`: Asynchronous explicit boundary.
|
|
90
|
+
|
|
91
|
+
### `Error`
|
|
92
|
+
- `userMessage: string`: Message safe for end users.
|
|
93
|
+
- `technicalMessage: string`: Diagnostic details, exception types, and chained causes.
|
|
94
|
+
- `category: ErrorCategory`: Categorization (`Unexpected`, `Validation`, `NotFound`, `Conflict`, `Forbidden`, `Unavailable`, `Canceled`, `Indeterminate`).
|
|
95
|
+
- `stackTrace: string | null`: Captured stack trace if created from an exception.
|
|
96
|
+
- `causeType: string | null`: Captured exception constructor/type name.
|
|
97
|
+
- Named factories: `fromMessage`, `fromMessages`, `fromException`, `fromExceptions`, `canceled`, `fromCancellation`, `fromCaught`, `atLeastOne`.
|
|
98
|
+
|
|
99
|
+
### `Guard` / `Guard.against`
|
|
100
|
+
Input validation guards returning `Result<T, Error>` with `ErrorCategory.Validation`:
|
|
101
|
+
- `null` / `none` / `againstNull`
|
|
102
|
+
- `nullOrEmpty` / `empty` / `againstEmpty`
|
|
103
|
+
- `nullOrWhitespace` / `nullOrWhiteSpace` / `againstNullOrWhitespace`
|
|
104
|
+
- `outOfRange(value, minVal, maxVal)` / `againstOutOfRange`
|
|
105
|
+
- `lessThan`, `lessThanOrEqual`
|
|
106
|
+
- `greaterThan`, `greaterThanOrEqual`
|
|
107
|
+
- `lengthOutOfRange(value, minLength, maxLength)`
|
|
108
|
+
- `patternMismatch(value, pattern)`
|
|
109
|
+
- `true(conditionOrValue, predicateOrMessage)`
|
|
110
|
+
- `false(conditionOrValue, predicateOrMessage)`
|
|
111
|
+
- `contains(collection, predicate, message)`
|
|
112
|
+
- `notContains(collection, predicate, message)`
|
package/dist/Err.d.ts
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { Error } from "./Error.js";
|
|
2
|
+
import { ErrorCategory } from "./ErrorCategory.js";
|
|
3
|
+
import type { Result } from "./Result.js";
|
|
4
|
+
/**
|
|
5
|
+
* Err holds a caller-actionable failure or structured Error and short-circuits subsequent
|
|
6
|
+
* railway combinators without invoking their callbacks.
|
|
7
|
+
*/
|
|
8
|
+
export declare class Err<E = Error> {
|
|
9
|
+
readonly ok: false;
|
|
10
|
+
readonly error: E;
|
|
11
|
+
private readonly _errors;
|
|
12
|
+
constructor(error: E, allErrors?: readonly Error[]);
|
|
13
|
+
get isSuccess(): false;
|
|
14
|
+
get isFailure(): true;
|
|
15
|
+
get errors(): readonly Error[];
|
|
16
|
+
get category(): ErrorCategory | string;
|
|
17
|
+
get message(): string;
|
|
18
|
+
/** Explicitly throws a ResultException carrying all structured errors. */
|
|
19
|
+
throwIfError(): never;
|
|
20
|
+
/** Explicitly throws a ResultException carrying all structured errors. */
|
|
21
|
+
unwrap(): never;
|
|
22
|
+
/** Returns a tuple of [false, undefined]. */
|
|
23
|
+
tryGetValue(): readonly [false, undefined];
|
|
24
|
+
/** Short-circuits and propagates this Err without executing nextOperation. */
|
|
25
|
+
then<U, F = E>(nextOperation: (value: never) => Result<U, F>): Err<E>;
|
|
26
|
+
then(nextOperation: (value: {
|
|
27
|
+
readonly ok: false;
|
|
28
|
+
readonly error: E;
|
|
29
|
+
readonly isSuccess: false;
|
|
30
|
+
readonly isFailure: true;
|
|
31
|
+
readonly errors: readonly Error[];
|
|
32
|
+
readonly category: ErrorCategory | string;
|
|
33
|
+
readonly message: string;
|
|
34
|
+
throwIfError(): never;
|
|
35
|
+
unwrap(): never;
|
|
36
|
+
tryGetValue(): readonly [false, undefined];
|
|
37
|
+
}) => unknown): unknown;
|
|
38
|
+
/** Short-circuits and propagates this Err asynchronously without executing nextOperation. */
|
|
39
|
+
thenAsync<U, F = E>(nextOperation: (value: never) => PromiseLike<Result<U, F>> | Result<U, F>): Promise<Err<E>>;
|
|
40
|
+
/** Short-circuits and propagates this Err without executing transformation. */
|
|
41
|
+
transform<U>(transformation: (value: never) => U): Err<E>;
|
|
42
|
+
/** Alias for transform. */
|
|
43
|
+
map<U>(transformation: (value: never) => U): Err<E>;
|
|
44
|
+
/** Short-circuits and propagates this Err asynchronously without executing transformation. */
|
|
45
|
+
transformAsync<U>(transformation: (value: never) => PromiseLike<U> | U): Promise<Err<E>>;
|
|
46
|
+
/** Alias for transformAsync. */
|
|
47
|
+
mapAsync<U>(transformation: (value: never) => PromiseLike<U> | U): Promise<Err<E>>;
|
|
48
|
+
/** Eliminates the Result by applying onFailure to the accumulated errors. */
|
|
49
|
+
resolve<R>(onSuccess: (value: never) => R, onFailure: (errors: readonly Error[]) => R): R;
|
|
50
|
+
/** Eliminates the Result asynchronously by applying onFailure to the accumulated errors. */
|
|
51
|
+
resolveAsync<R>(onSuccess: (value: never) => PromiseLike<R> | R, onFailure: (errors: readonly Error[]) => PromiseLike<R> | R): Promise<R>;
|
|
52
|
+
}
|
|
53
|
+
//# sourceMappingURL=Err.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Err.d.ts","sourceRoot":"","sources":["../src/Err.ts"],"names":[],"mappings":"AAuBA,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AACnC,OAAO,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;AACnD,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,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EAC/C,SAAS,EAAE,CAAC,MAAM,EAAE,SAAS,KAAK,EAAE,KAAK,WAAW,CAAC,CAAC,CAAC,GAAG,CAAC,GAC1D,OAAO,CAAC,CAAC,CAAC;CAMd"}
|
package/dist/Err.js
ADDED
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
// Responsibility: Represent the failed arm of a railway-oriented Result discriminated union.
|
|
2
|
+
// Public interface:
|
|
3
|
+
// `-- Err
|
|
4
|
+
// +-- category
|
|
5
|
+
// +-- error
|
|
6
|
+
// +-- errors
|
|
7
|
+
// +-- isFailure
|
|
8
|
+
// +-- isSuccess
|
|
9
|
+
// +-- map(transformation)
|
|
10
|
+
// +-- mapAsync(transformation)
|
|
11
|
+
// +-- message
|
|
12
|
+
// +-- new(error, allErrors)
|
|
13
|
+
// +-- ok
|
|
14
|
+
// +-- resolve(onSuccess, onFailure)
|
|
15
|
+
// +-- resolveAsync(onSuccess, onFailure)
|
|
16
|
+
// +-- then(nextOperation)
|
|
17
|
+
// +-- thenAsync(nextOperation)
|
|
18
|
+
// +-- throwIfError()
|
|
19
|
+
// +-- transform(transformation)
|
|
20
|
+
// +-- transformAsync(transformation)
|
|
21
|
+
// +-- tryGetValue()
|
|
22
|
+
// `-- unwrap()
|
|
23
|
+
import { Error } from "./Error.js";
|
|
24
|
+
import { ErrorCategory } from "./ErrorCategory.js";
|
|
25
|
+
import { ResultException } from "./ResultException.js";
|
|
26
|
+
/**
|
|
27
|
+
* Err holds a caller-actionable failure or structured Error and short-circuits subsequent
|
|
28
|
+
* railway combinators without invoking their callbacks.
|
|
29
|
+
*/
|
|
30
|
+
export class Err {
|
|
31
|
+
ok = false;
|
|
32
|
+
error;
|
|
33
|
+
_errors;
|
|
34
|
+
constructor(error, allErrors) {
|
|
35
|
+
this.error = error;
|
|
36
|
+
if (allErrors !== undefined && allErrors.length > 0) {
|
|
37
|
+
this._errors = Object.freeze([...allErrors]);
|
|
38
|
+
}
|
|
39
|
+
else if (error instanceof Error) {
|
|
40
|
+
this._errors = Object.freeze([error]);
|
|
41
|
+
}
|
|
42
|
+
else if (Array.isArray(error)) {
|
|
43
|
+
this._errors = Error.atLeastOne(error);
|
|
44
|
+
}
|
|
45
|
+
else if (typeof error === "object" && error !== null && "message" in error) {
|
|
46
|
+
const candidate = error;
|
|
47
|
+
const msg = String(candidate.message ?? "");
|
|
48
|
+
const cat = typeof candidate.category === "string"
|
|
49
|
+
? candidate.category
|
|
50
|
+
: ErrorCategory.Unexpected;
|
|
51
|
+
const causeType = typeof candidate.causeType === "string" ? candidate.causeType : null;
|
|
52
|
+
this._errors = Object.freeze([Error.fromMessages(msg, msg, cat, causeType)]);
|
|
53
|
+
}
|
|
54
|
+
else {
|
|
55
|
+
this._errors = Object.freeze([Error.fromMessage(String(error))]);
|
|
56
|
+
}
|
|
57
|
+
Object.freeze(this);
|
|
58
|
+
}
|
|
59
|
+
get isSuccess() {
|
|
60
|
+
return false;
|
|
61
|
+
}
|
|
62
|
+
get isFailure() {
|
|
63
|
+
return true;
|
|
64
|
+
}
|
|
65
|
+
get errors() {
|
|
66
|
+
return this._errors;
|
|
67
|
+
}
|
|
68
|
+
get category() {
|
|
69
|
+
if (this.error instanceof Error) {
|
|
70
|
+
return this.error.category;
|
|
71
|
+
}
|
|
72
|
+
if (typeof this.error === "object" && this.error !== null && "category" in this.error) {
|
|
73
|
+
const cat = this.error.category;
|
|
74
|
+
if (typeof cat === "string") {
|
|
75
|
+
return cat;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
return ErrorCategory.Unexpected;
|
|
79
|
+
}
|
|
80
|
+
get message() {
|
|
81
|
+
if (this.error instanceof Error) {
|
|
82
|
+
return this.error.message;
|
|
83
|
+
}
|
|
84
|
+
if (typeof this.error === "object" && this.error !== null && "message" in this.error) {
|
|
85
|
+
return String(this.error.message ?? "");
|
|
86
|
+
}
|
|
87
|
+
return String(this.error);
|
|
88
|
+
}
|
|
89
|
+
/** Explicitly throws a ResultException carrying all structured errors. */
|
|
90
|
+
throwIfError() {
|
|
91
|
+
throw new ResultException(this._errors);
|
|
92
|
+
}
|
|
93
|
+
/** Explicitly throws a ResultException carrying all structured errors. */
|
|
94
|
+
unwrap() {
|
|
95
|
+
throw new ResultException(this._errors);
|
|
96
|
+
}
|
|
97
|
+
/** Returns a tuple of [false, undefined]. */
|
|
98
|
+
tryGetValue() {
|
|
99
|
+
return Object.freeze([false, undefined]);
|
|
100
|
+
}
|
|
101
|
+
then(nextOperation) {
|
|
102
|
+
if (typeof nextOperation !== "function") {
|
|
103
|
+
throw new TypeError("nextOperation cannot be null or undefined");
|
|
104
|
+
}
|
|
105
|
+
return this;
|
|
106
|
+
}
|
|
107
|
+
/** Short-circuits and propagates this Err asynchronously without executing nextOperation. */
|
|
108
|
+
thenAsync(nextOperation) {
|
|
109
|
+
if (typeof nextOperation !== "function") {
|
|
110
|
+
return Promise.reject(new TypeError("nextOperation cannot be null or undefined"));
|
|
111
|
+
}
|
|
112
|
+
return Promise.resolve(this);
|
|
113
|
+
}
|
|
114
|
+
/** Short-circuits and propagates this Err without executing transformation. */
|
|
115
|
+
transform(transformation) {
|
|
116
|
+
if (typeof transformation !== "function") {
|
|
117
|
+
throw new TypeError("transformation cannot be null or undefined");
|
|
118
|
+
}
|
|
119
|
+
return this;
|
|
120
|
+
}
|
|
121
|
+
/** Alias for transform. */
|
|
122
|
+
map(transformation) {
|
|
123
|
+
return this.transform(transformation);
|
|
124
|
+
}
|
|
125
|
+
/** Short-circuits and propagates this Err asynchronously without executing transformation. */
|
|
126
|
+
transformAsync(transformation) {
|
|
127
|
+
if (typeof transformation !== "function") {
|
|
128
|
+
return Promise.reject(new TypeError("transformation cannot be null or undefined"));
|
|
129
|
+
}
|
|
130
|
+
return Promise.resolve(this);
|
|
131
|
+
}
|
|
132
|
+
/** Alias for transformAsync. */
|
|
133
|
+
mapAsync(transformation) {
|
|
134
|
+
return this.transformAsync(transformation);
|
|
135
|
+
}
|
|
136
|
+
/** Eliminates the Result by applying onFailure to the accumulated errors. */
|
|
137
|
+
resolve(onSuccess, onFailure) {
|
|
138
|
+
if (typeof onSuccess !== "function" || typeof onFailure !== "function") {
|
|
139
|
+
throw new TypeError("resolve handlers cannot be null or undefined");
|
|
140
|
+
}
|
|
141
|
+
return onFailure(this._errors);
|
|
142
|
+
}
|
|
143
|
+
/** Eliminates the Result asynchronously by applying onFailure to the accumulated errors. */
|
|
144
|
+
async resolveAsync(onSuccess, onFailure) {
|
|
145
|
+
if (typeof onSuccess !== "function" || typeof onFailure !== "function") {
|
|
146
|
+
throw new TypeError("resolve handlers cannot be null or undefined");
|
|
147
|
+
}
|
|
148
|
+
return await onFailure(this._errors);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
const resolvingErr = new WeakSet();
|
|
152
|
+
Object.defineProperty(Err.prototype, "then", {
|
|
153
|
+
configurable: true,
|
|
154
|
+
enumerable: false,
|
|
155
|
+
get() {
|
|
156
|
+
if (resolvingErr.has(this)) {
|
|
157
|
+
resolvingErr.delete(this);
|
|
158
|
+
return undefined;
|
|
159
|
+
}
|
|
160
|
+
return (nextOperation, onRejected) => {
|
|
161
|
+
if (typeof onRejected === "function") {
|
|
162
|
+
resolvingErr.add(this);
|
|
163
|
+
return nextOperation(this);
|
|
164
|
+
}
|
|
165
|
+
if (typeof nextOperation !== "function") {
|
|
166
|
+
throw new TypeError("nextOperation cannot be null or undefined");
|
|
167
|
+
}
|
|
168
|
+
return this;
|
|
169
|
+
};
|
|
170
|
+
},
|
|
171
|
+
});
|
|
172
|
+
//# sourceMappingURL=Err.js.map
|
package/dist/Err.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Err.js","sourceRoot":"","sources":["../src/Err.ts"],"names":[],"mappings":"AAAA,6FAA6F;AAC7F,oBAAoB;AACpB,UAAU;AACV,mBAAmB;AACnB,gBAAgB;AAChB,iBAAiB;AACjB,oBAAoB;AACpB,oBAAoB;AACpB,8BAA8B;AAC9B,mCAAmC;AACnC,k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AY,EAAE,IAAI;IAClB,UAAU,EAAE,KAAK;IACjB,GAAG;QACD,IAAI,YAAY,CAAC,GAAG,CAAC,IAAI,CAAC,EAAE,CAAC;YAC3B,YAAY,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;YAC1B,OAAO,SAAS,CAAC;QACnB,CAAC;QACD,OAAO,CACL,aAA6C,EAC7C,UAAoB,EACX,EAAE;YACX,IAAI,OAAO,UAAU,KAAK,UAAU,EAAE,CAAC;gBACrC,YAAY,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;gBACvB,OAAQ,aAAyC,CAAC,IAAI,CAAC,CAAC;YAC1D,CAAC;YACD,IAAI,OAAO,aAAa,KAAK,UAAU,EAAE,CAAC;gBACxC,MAAM,IAAI,SAAS,CAAC,2CAA2C,CAAC,CAAC;YACnE,CAAC;YACD,OAAO,IAAI,CAAC;QACd,CAAC,CAAC;IACJ,CAAC;CACF,CAAC,CAAC"}
|
package/dist/Error.d.ts
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { ErrorCategory } from "./ErrorCategory.js";
|
|
2
|
+
/** The user message applied when a caller supplies an exception or null message without a user message. */
|
|
3
|
+
export declare const DEFAULT_USER_MESSAGE = "An unexpected error occurred";
|
|
4
|
+
/** The user message every cancellation error carries. */
|
|
5
|
+
export declare const CANCELED_USER_MESSAGE = "The operation was cancelled.";
|
|
6
|
+
/** The user message a timeout carries. */
|
|
7
|
+
export declare const TIMED_OUT_USER_MESSAGE = "The operation timed out.";
|
|
8
|
+
/**
|
|
9
|
+
* Error is an immutable description of a single failure, carrying a user-safe message, a
|
|
10
|
+
* technical message, a failure category, and captured stack trace and cause type without
|
|
11
|
+
* retaining live external exception references.
|
|
12
|
+
*/
|
|
13
|
+
export declare class Error {
|
|
14
|
+
static readonly DEFAULT_USER_MESSAGE = "An unexpected error occurred";
|
|
15
|
+
static readonly CANCELED_USER_MESSAGE = "The operation was cancelled.";
|
|
16
|
+
static readonly TIMED_OUT_USER_MESSAGE = "The operation timed out.";
|
|
17
|
+
static readonly DefaultUserMessage = "An unexpected error occurred";
|
|
18
|
+
static readonly CanceledUserMessage = "The operation was cancelled.";
|
|
19
|
+
static readonly TimedOutUserMessage = "The operation timed out.";
|
|
20
|
+
readonly userMessage: string;
|
|
21
|
+
readonly technicalMessage: string;
|
|
22
|
+
readonly category: ErrorCategory;
|
|
23
|
+
readonly stackTrace: string | null;
|
|
24
|
+
readonly causeType: string | null;
|
|
25
|
+
constructor(userMessage?: string | null | undefined, technicalMessage?: string | null, category?: ErrorCategory | string, stackTrace?: string | null, causeType?: string | null);
|
|
26
|
+
/** Gets the diagnostic technical message if non-empty, otherwise the user message. */
|
|
27
|
+
get message(): string;
|
|
28
|
+
/** Returns true when a non-empty stack trace was captured. */
|
|
29
|
+
get hasStackTrace(): boolean;
|
|
30
|
+
/** Creates an error whose single message serves both user and technical contexts. */
|
|
31
|
+
static fromMessage(message?: string | null | undefined, category?: ErrorCategory | string): Error;
|
|
32
|
+
/** Creates an error with distinct user-facing and technical diagnostic messages. */
|
|
33
|
+
static fromMessages(userMessage?: string | null | undefined, technicalMessage?: string | null | undefined, category?: ErrorCategory | string, causeType?: string | null): Error;
|
|
34
|
+
/** Creates an error capturing message chain, stack trace, and causeType from an exception. */
|
|
35
|
+
static fromException(exception: unknown, userMessage?: string | null | undefined, category?: ErrorCategory | string): Error;
|
|
36
|
+
/** Expands an exception, AggregateError, ResultException, or iterable of exceptions into flat structured errors. */
|
|
37
|
+
static fromExceptions(exceptionOrExceptions: unknown, userMessage?: string | null | undefined, category?: ErrorCategory | string): readonly Error[];
|
|
38
|
+
/** Creates a cancellation error carrying ErrorCategory.Canceled. */
|
|
39
|
+
static canceled(canceled?: unknown): Error;
|
|
40
|
+
/** Classifies a cancellation by caller evidence versus timeout. */
|
|
41
|
+
static fromCancellation(canceled?: unknown, callerToken?: unknown): Error;
|
|
42
|
+
/** Translates any caught exception at an explicit adapter boundary. */
|
|
43
|
+
static fromCaught(exception: unknown, callerToken?: unknown): readonly Error[];
|
|
44
|
+
/** Normalizes an error sequence, substituting a sentinel error if null or empty. */
|
|
45
|
+
static atLeastOne(errors?: Iterable<Error | null | undefined> | null): readonly Error[];
|
|
46
|
+
}
|
|
47
|
+
//# sourceMappingURL=Error.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Error.d.ts","sourceRoot":"","sources":["../src/Error.ts"],"names":[],"mappings":"AA6BA,OAAO,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;AAEnD,2GAA2G;AAC3G,eAAO,MAAM,oBAAoB,iCAAiC,CAAC;AAEnE,yDAAyD;AACzD,eAAO,MAAM,qBAAqB,iCAAiC,CAAC;AAEpE,0CAA0C;AAC1C,eAAO,MAAM,sBAAsB,6BAA6B,CAAC;AAuIjE;;;;GAIG;AACH,qBAAa,KAAK;IAChB,gBAAuB,oBAAoB,kCAAwB;IACnE,gBAAuB,qBAAqB,kCAAyB;IACrE,gBAAuB,sBAAsB,8BAA0B;IACvE,gBAAuB,kBAAkB,kCAAwB;IACjE,gBAAuB,mBAAmB,kCAAyB;IACnE,gBAAuB,mBAAmB,8BAA0B;IAEpE,SAAgB,WAAW,EAAE,MAAM,CAAC;IACpC,SAAgB,gBAAgB,EAAE,MAAM,CAAC;IACzC,SAAgB,QAAQ,EAAE,aAAa,CAAC;IACxC,SAAgB,UAAU,EAAE,MAAM,GAAG,IAAI,CAAC;IAC1C,SAAgB,SAAS,EAAE,MAAM,GAAG,IAAI,CAAC;gBAGvC,WAAW,GAAE,MAAM,GAAG,IAAI,GAAG,SAAgC,EAC7D,gBAAgB,CAAC,EAAE,MAAM,GAAG,IAAI,EAChC,QAAQ,GAAE,aAAa,GAAG,MAAiC,EAC3D,UAAU,GAAE,MAAM,GAAG,IAAW,EAChC,SAAS,GAAE,MAAM,GAAG,IAAW;IAWjC,sFAAsF;IACtF,IAAW,OAAO,IAAI,MAAM,CAE3B;IAED,8DAA8D;IAC9D,IAAW,aAAa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