unthrown 2.0.0 → 3.0.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 +3 -3
- package/dist/index.cjs +65 -67
- package/dist/index.d.cts +52 -59
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +52 -59
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +66 -67
- package/dist/index.mjs.map +1 -1
- package/docs/index.md +93 -152
- package/package.json +3 -2
package/dist/index.d.mts
CHANGED
|
@@ -191,7 +191,7 @@ type ResultMethods<T, E> = {
|
|
|
191
191
|
recoverDefect<U, E2>(f: (cause: unknown) => Result$1<U, E2>): Result$1<T | U, E | E2>;
|
|
192
192
|
/**
|
|
193
193
|
* Run a side effect on a present `Defect`'s cause (e.g. logging) and pass the
|
|
194
|
-
* `Defect` through unchanged.
|
|
194
|
+
* `Defect` through unchanged. If `f` throws, the throw becomes a new `Defect`.
|
|
195
195
|
*
|
|
196
196
|
* @param f - the side effect over the unknown cause.
|
|
197
197
|
*/
|
|
@@ -488,6 +488,11 @@ declare function isDefect<T, E>(r: Result$1<T, E>): r is DefectView<T, E>;
|
|
|
488
488
|
* `Ok`.
|
|
489
489
|
*
|
|
490
490
|
* @remarks
|
|
491
|
+
* The offending value is exposed two ways: the typed {@link UnwrapError.error}
|
|
492
|
+
* property for programmatic access, and the standard `Error.cause` for the
|
|
493
|
+
* runtime and devtools to chain — when `E` is an `Error` (e.g. a `TaggedError`)
|
|
494
|
+
* its original stack is printed under "caused by".
|
|
495
|
+
*
|
|
491
496
|
* A `Defect` is never wrapped in an `UnwrapError`: its original cause is
|
|
492
497
|
* re-thrown (with its original stack) instead.
|
|
493
498
|
*
|
|
@@ -515,40 +520,6 @@ declare class UnwrapError<E = unknown> extends Error {
|
|
|
515
520
|
*/
|
|
516
521
|
declare function isResult(x: unknown): x is Result$1<unknown, unknown>;
|
|
517
522
|
//#endregion
|
|
518
|
-
//#region src/defect.d.ts
|
|
519
|
-
declare const DEFECT: unique symbol;
|
|
520
|
-
/**
|
|
521
|
-
* The marker a `qualify` function returns to triage a cause as **unexpected**.
|
|
522
|
-
*
|
|
523
|
-
* @remarks
|
|
524
|
-
* `qualify` (passed to {@link fromPromise} / {@link fromThrowable}) returns
|
|
525
|
-
* `E | Defect`: either a modeled domain error, or a `Defect` produced by
|
|
526
|
-
* {@link Defect} to say "this failure is not modeled". A `Defect` is opaque —
|
|
527
|
-
* it carries the original cause for the boundary to convert into the third
|
|
528
|
-
* runtime state of a `Result`.
|
|
529
|
-
*/
|
|
530
|
-
type Defect = {
|
|
531
|
-
readonly [DEFECT]: true;
|
|
532
|
-
readonly cause: unknown;
|
|
533
|
-
};
|
|
534
|
-
/**
|
|
535
|
-
* Wrap a cause as a {@link Defect} — the value you return from a `qualify`
|
|
536
|
-
* function when a failure is **not** a modeled domain error.
|
|
537
|
-
*
|
|
538
|
-
* @param cause - the original thrown/rejected value.
|
|
539
|
-
* @returns an opaque Defect marker carrying `cause`.
|
|
540
|
-
*
|
|
541
|
-
* @example
|
|
542
|
-
* ```ts
|
|
543
|
-
* import { fromPromise, Defect } from "unthrown";
|
|
544
|
-
*
|
|
545
|
-
* const user = fromPromise(fetchUser(id), (cause) =>
|
|
546
|
-
* cause instanceof NotFoundError ? cause : Defect(cause),
|
|
547
|
-
* );
|
|
548
|
-
* ```
|
|
549
|
-
*/
|
|
550
|
-
declare function Defect(cause: unknown): Defect;
|
|
551
|
-
//#endregion
|
|
552
523
|
//#region src/do.d.ts
|
|
553
524
|
/**
|
|
554
525
|
* Start a do-notation chain with an empty object scope, grown step by step with
|
|
@@ -574,6 +545,27 @@ declare function Defect(cause: unknown): Defect;
|
|
|
574
545
|
*/
|
|
575
546
|
declare function Do(): Result$1<{}, never>;
|
|
576
547
|
//#endregion
|
|
548
|
+
//#region src/defect.d.ts
|
|
549
|
+
declare const DEFECT: unique symbol;
|
|
550
|
+
/**
|
|
551
|
+
* The opaque marker a `qualify` function returns to triage a cause as
|
|
552
|
+
* **unexpected**.
|
|
553
|
+
*
|
|
554
|
+
* @remarks
|
|
555
|
+
* `qualify` (passed to {@link fromPromise} / {@link fromThrowable}) returns
|
|
556
|
+
* `E | Defect`: either a modeled domain error, or a `Defect` produced by the
|
|
557
|
+
* injected `defect` helper to say "this failure is not modeled". A `Defect` is
|
|
558
|
+
* opaque — it carries the original cause for the boundary to convert into the
|
|
559
|
+
* third runtime state of a `Result`. It is **not** a public value; the only way
|
|
560
|
+
* to mint one is the `defect` helper the boundary passes to `qualify`.
|
|
561
|
+
*
|
|
562
|
+
* @internal
|
|
563
|
+
*/
|
|
564
|
+
type Defect = {
|
|
565
|
+
readonly [DEFECT]: true;
|
|
566
|
+
readonly cause: unknown;
|
|
567
|
+
};
|
|
568
|
+
//#endregion
|
|
577
569
|
//#region src/interop.d.ts
|
|
578
570
|
/**
|
|
579
571
|
* Bridge a nullable value into a {@link Result}: absence becomes a **modeled**
|
|
@@ -601,12 +593,13 @@ declare function fromNullable<T, E>(value: T | null | undefined, onAbsent: () =>
|
|
|
601
593
|
*
|
|
602
594
|
* @remarks
|
|
603
595
|
* `qualify` **must** triage every thrown cause into a modeled error `E` or a
|
|
604
|
-
*
|
|
605
|
-
* in `E`. A throw inside `qualify` itself is treated
|
|
596
|
+
* `Defect` (via the injected `defect` helper, its second argument) — there is no
|
|
597
|
+
* path that leaves `unknown` in `E`. A throw inside `qualify` itself is treated
|
|
598
|
+
* as a `Defect`.
|
|
606
599
|
*
|
|
607
600
|
* The modeled error type is `Exclude<R, Defect>` — the `Defect` arm of
|
|
608
601
|
* `qualify`'s return is **subtracted** from `E`, never inferred into it. So a
|
|
609
|
-
* `qualify` that returns *only* `
|
|
602
|
+
* `qualify` that returns *only* `defect(cause)` yields `E = never` (a Defect is
|
|
610
603
|
* out-of-band and must not pollute the error channel); reach for
|
|
611
604
|
* {@link fromSafePromise} when every failure is a Defect.
|
|
612
605
|
*
|
|
@@ -615,47 +608,49 @@ declare function fromNullable<T, E>(value: T | null | undefined, onAbsent: () =>
|
|
|
615
608
|
* @typeParam R - `qualify`'s return type; the modeled error `E` is
|
|
616
609
|
* `Exclude<R, Defect>` (its `Defect` arm, if any, is subtracted).
|
|
617
610
|
* @param fn - the throwing function to wrap.
|
|
618
|
-
* @param qualify - triages a thrown cause into `E
|
|
611
|
+
* @param qualify - triages a thrown `cause` into a modeled `E`, or marks it
|
|
612
|
+
* unmodeled by returning `defect(cause)` (the helper passed as its second arg).
|
|
619
613
|
* @returns a function with the same arguments returning `Result<T, E>`.
|
|
620
614
|
*
|
|
621
615
|
* @example
|
|
622
616
|
* ```ts
|
|
623
|
-
* import { fromThrowable
|
|
624
|
-
* const parse = fromThrowable(JSON.parse, (cause) =>
|
|
617
|
+
* import { fromThrowable } from "unthrown";
|
|
618
|
+
* const parse = fromThrowable(JSON.parse, (cause, defect) => defect(cause));
|
|
625
619
|
* parse("{}").unwrap();
|
|
626
620
|
* ```
|
|
627
621
|
*/
|
|
628
|
-
declare function fromThrowable<A extends unknown[], T, R>(fn: (...args: A) => T, qualify: (cause: unknown) => R): (...args: A) => Result$1<T, Exclude<R, Defect>>;
|
|
622
|
+
declare function fromThrowable<A extends unknown[], T, R>(fn: (...args: A) => T, qualify: (cause: unknown, defect: (cause: unknown) => Defect) => R): (...args: A) => Result$1<T, Exclude<R, Defect>>;
|
|
629
623
|
/**
|
|
630
624
|
* Wrap a `Promise` (or a thunk producing one) as an {@link AsyncResult}, forcing
|
|
631
625
|
* every rejection to be triaged.
|
|
632
626
|
*
|
|
633
627
|
* @remarks
|
|
634
628
|
* `qualify` **must** map each rejection cause into a modeled error `E` or a
|
|
635
|
-
*
|
|
636
|
-
* `await`-ing it always yields a
|
|
637
|
-
* `Defect`.
|
|
629
|
+
* `Defect` (via the injected `defect` helper, its second argument). The returned
|
|
630
|
+
* `AsyncResult`'s internal promise never rejects; `await`-ing it always yields a
|
|
631
|
+
* `Result`. A throw inside `qualify` is itself a `Defect`.
|
|
638
632
|
*
|
|
639
633
|
* The modeled error type is `Exclude<R, Defect>` — the `Defect` arm of
|
|
640
634
|
* `qualify`'s return is **subtracted** from `E`, never inferred into it. So a
|
|
641
|
-
* `qualify` that returns *only* `
|
|
635
|
+
* `qualify` that returns *only* `defect(cause)` yields `E = never`; when every
|
|
642
636
|
* rejection is a Defect, prefer {@link fromSafePromise}.
|
|
643
637
|
*
|
|
644
638
|
* @typeParam T - the resolved value type.
|
|
645
639
|
* @typeParam R - `qualify`'s return type; the modeled error `E` is
|
|
646
640
|
* `Exclude<R, Defect>` (its `Defect` arm, if any, is subtracted).
|
|
647
641
|
* @param promise - the promise, or a thunk returning one.
|
|
648
|
-
* @param qualify - triages a rejection cause into `E
|
|
642
|
+
* @param qualify - triages a rejection `cause` into a modeled `E`, or marks it
|
|
643
|
+
* unmodeled by returning `defect(cause)` (the helper passed as its second arg).
|
|
649
644
|
*
|
|
650
645
|
* @example
|
|
651
646
|
* ```ts
|
|
652
|
-
* import { fromPromise
|
|
653
|
-
* const user = await fromPromise(fetchUser(id), (cause) =>
|
|
654
|
-
* cause instanceof NotFoundError ? ("not_found" as const) :
|
|
647
|
+
* import { fromPromise } from "unthrown";
|
|
648
|
+
* const user = await fromPromise(fetchUser(id), (cause, defect) =>
|
|
649
|
+
* cause instanceof NotFoundError ? ("not_found" as const) : defect(cause),
|
|
655
650
|
* );
|
|
656
651
|
* ```
|
|
657
652
|
*/
|
|
658
|
-
declare function fromPromise<T, R>(promise: Promise<T> | (() => Promise<T>), qualify: (cause: unknown) => R): AsyncResult$1<T, Exclude<R, Defect>>;
|
|
653
|
+
declare function fromPromise<T, R>(promise: Promise<T> | (() => Promise<T>), qualify: (cause: unknown, defect: (cause: unknown) => Defect) => R): AsyncResult$1<T, Exclude<R, Defect>>;
|
|
659
654
|
/**
|
|
660
655
|
* Wrap a `Promise` asserted **not** to fail in any modeled way: any rejection
|
|
661
656
|
* becomes a `Defect`.
|
|
@@ -765,10 +760,9 @@ declare function allFromDictAsync<R extends AsyncResultRecord>(results: R): Asyn
|
|
|
765
760
|
/**
|
|
766
761
|
* Companion object grouping the **`Result`-producing** entry points under a
|
|
767
762
|
* single, discoverable namespace: {@link Result.Ok}, {@link Result.Err},
|
|
768
|
-
* {@link Result.
|
|
769
|
-
* {@link Result.
|
|
770
|
-
* {@link Result.
|
|
771
|
-
* {@link Result.isResult}.
|
|
763
|
+
* {@link Result.Do}, {@link Result.fromNullable}, {@link Result.fromThrowable},
|
|
764
|
+
* {@link Result.all}, {@link Result.allFromDict}, {@link Result.isOk},
|
|
765
|
+
* {@link Result.isErr}, {@link Result.isDefect}, {@link Result.isResult}.
|
|
772
766
|
*
|
|
773
767
|
* @remarks
|
|
774
768
|
* Purely additive sugar — each member **is** the corresponding free function.
|
|
@@ -789,7 +783,6 @@ declare function allFromDictAsync<R extends AsyncResultRecord>(results: R): Asyn
|
|
|
789
783
|
declare const Result: {
|
|
790
784
|
readonly Ok: typeof Ok;
|
|
791
785
|
readonly Err: typeof Err;
|
|
792
|
-
readonly Defect: typeof Defect;
|
|
793
786
|
readonly Do: typeof Do;
|
|
794
787
|
readonly fromNullable: typeof fromNullable;
|
|
795
788
|
readonly fromThrowable: typeof fromThrowable;
|
|
@@ -818,8 +811,8 @@ type Result<T, E> = Result$1<T, E>;
|
|
|
818
811
|
*
|
|
819
812
|
* @example
|
|
820
813
|
* ```ts
|
|
821
|
-
* import { AsyncResult
|
|
822
|
-
* const user = await AsyncResult.fromPromise(fetchUser(id), (c) =>
|
|
814
|
+
* import { AsyncResult } from "unthrown";
|
|
815
|
+
* const user = await AsyncResult.fromPromise(fetchUser(id), (c, defect) => defect(c));
|
|
823
816
|
* ```
|
|
824
817
|
*/
|
|
825
818
|
declare const AsyncResult: {
|
|
@@ -952,5 +945,5 @@ declare function matchTags<T, E extends {
|
|
|
952
945
|
_tag: string;
|
|
953
946
|
}, R>(result: AsyncResult$1<T, E>, handlers: TagHandlers<T, E, R>): Promise<R>;
|
|
954
947
|
//#endregion
|
|
955
|
-
export { type AsyncErrOf, type AsyncOkOf, AsyncResult, type Awaitable,
|
|
948
|
+
export { type AsyncErrOf, type AsyncOkOf, AsyncResult, type Awaitable, type DefectView, Do, Err, type ErrOf, type ErrView, Ok, type OkOf, type OkView, Result, type TagHandlers, TaggedError, type TaggedErrorConstructor, type TaggedErrorInstance, UnwrapError, all, allAsync, allFromDict, allFromDictAsync, fromNullable, fromPromise, fromSafePromise, fromThrowable, isDefect, isErr, isOk, isResult, matchTags };
|
|
956
949
|
//# sourceMappingURL=index.d.mts.map
|
package/dist/index.d.mts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.mts","names":[],"sources":["../src/types.ts","../src/constructors.ts","../src/core.ts","../src/
|
|
1
|
+
{"version":3,"file":"index.d.mts","names":[],"sources":["../src/types.ts","../src/constructors.ts","../src/core.ts","../src/do.ts","../src/defect.ts","../src/interop.ts","../src/facade.ts","../src/tagged.ts"],"mappings":";;AAQA;;;;;KAAY,QAAA,oBAA4B,CAAA,GAAI,CAAA,CAAE,CAAA;;;;;;;AAAC;AAU/C;KAAY,KAAA,2BAAgC,QAAA,CAAS,IAAA,CAAK,CAAA,EAAG,CAAA,qBAAsB,CAAA,GAAI,CAAA;;;;;;;;;;KAW3E,aAAA;EAXgC;;;;;;;;AAA4C;EAqBtF,GAAA,IAAO,CAAA,GAAI,KAAA,EAAO,CAAA,KAAM,CAAA,GAAI,QAAA,CAAO,CAAA,EAAG,CAAA;EAVf;;;;;;;;;;EAqBvB,OAAA,QAAe,CAAA,GAAI,KAAA,EAAO,CAAA,KAAM,QAAA,CAAO,CAAA,EAAG,EAAA,IAAM,QAAA,CAAO,CAAA,EAAG,CAAA,GAAI,EAAA;EAAP;;;;;;;;EASvD,GAAA,CAAI,CAAA,GAAI,KAAA,EAAO,CAAA,YAAa,QAAA,CAAO,CAAA,EAAG,CAAA;EAiBO;;;;;;;;;;;;;;;;EAA7C,OAAA,KAAY,CAAA,GAAI,KAAA,EAAO,CAAA,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P,mBAAA,+BAAkD,KAAA,IAAS,KAAA,GACrE,QAAA,CAAS,CAAA;EAAA,SAAgB,IAAA,EAAM,GAAA;AAAA;;;;APPc;AAU/C;;;;;;KOSY,sBAAA;EAAA,eACK,KAAA,OAAY,IAAA,QAAY,CAAA,wBAAyB,CAAA,GAAI,mBAAA,CAAoB,GAAA,EAAK,CAAA;AAAA;;;;;;;;;;;;;;APVP;AAWxF;;;;;;;;;;;;;;;;;;;;;;;;;;iBO0CgB,WAAA,qBACd,GAAA,EAAK,GAAA,EACL,OAAA;EAAA,SAAqB,IAAA;AAAA,IACpB,sBAAA,CAAuB,GAAA;;;;;;;;;;KA4Bd,WAAA;EAA2B,IAAA;AAAA;EACrC,EAAA,GAAK,KAAA,EAAO,CAAA,KAAM,CAAA;EAClB,MAAA,GAAS,KAAA,cAAmB,CAAA;AAAA,YAClB,CAAA,YAAa,KAAA,EAAO,OAAA,CAAQ,CAAA;EAAK,IAAA,EAAM,CAAA;AAAA,OAAS,CAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBAgC5C,SAAA;EAAyB,IAAA;AAAA,MACvC,MAAA,EAAQ,QAAA,CAAO,CAAA,EAAG,CAAA,GAClB,QAAA,EAAU,WAAA,CAAY,CAAA,EAAG,CAAA,EAAG,CAAA,IAC3B,CAAA;AAAA,iBACa,SAAA;EAAyB,IAAA;AAAA,MACvC,MAAA,EAAQ,aAAA,CAAY,CAAA,EAAG,CAAA,GACvB,QAAA,EAAU,WAAA,CAAY,CAAA,EAAG,CAAA,EAAG,CAAA,IAC3B,OAAA,CAAQ,CAAA"}
|
package/dist/index.mjs
CHANGED
|
@@ -5,6 +5,11 @@
|
|
|
5
5
|
* `Ok`.
|
|
6
6
|
*
|
|
7
7
|
* @remarks
|
|
8
|
+
* The offending value is exposed two ways: the typed {@link UnwrapError.error}
|
|
9
|
+
* property for programmatic access, and the standard `Error.cause` for the
|
|
10
|
+
* runtime and devtools to chain — when `E` is an `Error` (e.g. a `TaggedError`)
|
|
11
|
+
* its original stack is printed under "caused by".
|
|
12
|
+
*
|
|
8
13
|
* A `Defect` is never wrapped in an `UnwrapError`: its original cause is
|
|
9
14
|
* re-thrown (with its original stack) instead.
|
|
10
15
|
*
|
|
@@ -17,7 +22,7 @@ var UnwrapError = class extends Error {
|
|
|
17
22
|
*/
|
|
18
23
|
error;
|
|
19
24
|
constructor(error) {
|
|
20
|
-
super("unthrown: called unwrap on a non-matching Result");
|
|
25
|
+
super("unthrown: called unwrap on a non-matching Result", { cause: error });
|
|
21
26
|
this.name = "UnwrapError";
|
|
22
27
|
this.error = error;
|
|
23
28
|
Object.setPrototypeOf(this, new.target.prototype);
|
|
@@ -278,7 +283,7 @@ function passThrough(self) {
|
|
|
278
283
|
* though, so a primitive `Ok` (e.g. `Ok(5).bind(...)`, or a chain whose value was
|
|
279
284
|
* `map`-ped away from its scope) could reach it. Rather than let `{ ...5 }`
|
|
280
285
|
* silently collapse to `{}` and drop the prior scope, we throw here — the
|
|
281
|
-
* surrounding `try` turns it into a
|
|
286
|
+
* surrounding `try` turns it into a `Defect`, surfacing the misuse as the
|
|
282
287
|
* bug it is (a defect is a bug, not an absent value). A `this: object` constraint
|
|
283
288
|
* was rejected: TypeScript does not hard-enforce a constraint inferred solely
|
|
284
289
|
* from `this`, and it breaks `AsyncRes implements AsyncResult`.
|
|
@@ -463,11 +468,7 @@ var AsyncRes = class AsyncRes {
|
|
|
463
468
|
return this.promise.then((r) => r.unwrapOr(fallback));
|
|
464
469
|
}
|
|
465
470
|
unwrapOrElse(f) {
|
|
466
|
-
return this.promise.then((r) =>
|
|
467
|
-
if (r.tag === "Ok") return r.value;
|
|
468
|
-
if (r.tag === "Defect") throw r.cause;
|
|
469
|
-
return f(r.error);
|
|
470
|
-
});
|
|
471
|
+
return this.promise.then((r) => r.unwrapOrElse(f));
|
|
471
472
|
}
|
|
472
473
|
getOrNull() {
|
|
473
474
|
return this.promise.then((r) => r.getOrNull());
|
|
@@ -540,41 +541,6 @@ function isDefect(r) {
|
|
|
540
541
|
return r.tag === "Defect";
|
|
541
542
|
}
|
|
542
543
|
//#endregion
|
|
543
|
-
//#region src/defect.ts
|
|
544
|
-
const DEFECT = Symbol("unthrown/Defect");
|
|
545
|
-
/**
|
|
546
|
-
* Wrap a cause as a {@link Defect} — the value you return from a `qualify`
|
|
547
|
-
* function when a failure is **not** a modeled domain error.
|
|
548
|
-
*
|
|
549
|
-
* @param cause - the original thrown/rejected value.
|
|
550
|
-
* @returns an opaque Defect marker carrying `cause`.
|
|
551
|
-
*
|
|
552
|
-
* @example
|
|
553
|
-
* ```ts
|
|
554
|
-
* import { fromPromise, Defect } from "unthrown";
|
|
555
|
-
*
|
|
556
|
-
* const user = fromPromise(fetchUser(id), (cause) =>
|
|
557
|
-
* cause instanceof NotFoundError ? cause : Defect(cause),
|
|
558
|
-
* );
|
|
559
|
-
* ```
|
|
560
|
-
*/
|
|
561
|
-
function Defect(cause) {
|
|
562
|
-
return {
|
|
563
|
-
[DEFECT]: true,
|
|
564
|
-
cause
|
|
565
|
-
};
|
|
566
|
-
}
|
|
567
|
-
/**
|
|
568
|
-
* Internal guard for the qualify-time marker. Distinct from the public
|
|
569
|
-
* {@link isDefect} state guard — this one narrows the `E | Defect` union a
|
|
570
|
-
* `qualify` function returns, not a `Result`.
|
|
571
|
-
*
|
|
572
|
-
* @internal
|
|
573
|
-
*/
|
|
574
|
-
function isDefectMarker(x) {
|
|
575
|
-
return typeof x === "object" && x !== null && x[DEFECT] === true;
|
|
576
|
-
}
|
|
577
|
-
//#endregion
|
|
578
544
|
//#region src/do.ts
|
|
579
545
|
/**
|
|
580
546
|
* Start a do-notation chain with an empty object scope, grown step by step with
|
|
@@ -602,6 +568,36 @@ function Do() {
|
|
|
602
568
|
return Ok({});
|
|
603
569
|
}
|
|
604
570
|
//#endregion
|
|
571
|
+
//#region src/defect.ts
|
|
572
|
+
const DEFECT = Symbol("unthrown/Defect");
|
|
573
|
+
/**
|
|
574
|
+
* Wrap a cause as a `Defect` marker — the value returned from a `qualify`
|
|
575
|
+
* function when a failure is **not** a modeled domain error. The boundary
|
|
576
|
+
* (`fromPromise` / `fromThrowable`) passes this in as `qualify`'s second
|
|
577
|
+
* argument, so domain code never imports it.
|
|
578
|
+
*
|
|
579
|
+
* @param cause - the original thrown/rejected value.
|
|
580
|
+
* @returns an opaque Defect marker carrying `cause`.
|
|
581
|
+
*
|
|
582
|
+
* @internal
|
|
583
|
+
*/
|
|
584
|
+
function defect(cause) {
|
|
585
|
+
return {
|
|
586
|
+
[DEFECT]: true,
|
|
587
|
+
cause
|
|
588
|
+
};
|
|
589
|
+
}
|
|
590
|
+
/**
|
|
591
|
+
* Internal guard for the qualify-time marker. Distinct from the public
|
|
592
|
+
* {@link isDefect} state guard — this one narrows the `E | Defect` union a
|
|
593
|
+
* `qualify` function returns, not a `Result`.
|
|
594
|
+
*
|
|
595
|
+
* @internal
|
|
596
|
+
*/
|
|
597
|
+
function isDefectMarker(x) {
|
|
598
|
+
return typeof x === "object" && x !== null && x[DEFECT] === true;
|
|
599
|
+
}
|
|
600
|
+
//#endregion
|
|
605
601
|
//#region src/interop.ts
|
|
606
602
|
/**
|
|
607
603
|
* Bridge a nullable value into a {@link Result}: absence becomes a **modeled**
|
|
@@ -631,12 +627,13 @@ function fromNullable(value, onAbsent) {
|
|
|
631
627
|
*
|
|
632
628
|
* @remarks
|
|
633
629
|
* `qualify` **must** triage every thrown cause into a modeled error `E` or a
|
|
634
|
-
*
|
|
635
|
-
* in `E`. A throw inside `qualify` itself is treated
|
|
630
|
+
* `Defect` (via the injected `defect` helper, its second argument) — there is no
|
|
631
|
+
* path that leaves `unknown` in `E`. A throw inside `qualify` itself is treated
|
|
632
|
+
* as a `Defect`.
|
|
636
633
|
*
|
|
637
634
|
* The modeled error type is `Exclude<R, Defect>` — the `Defect` arm of
|
|
638
635
|
* `qualify`'s return is **subtracted** from `E`, never inferred into it. So a
|
|
639
|
-
* `qualify` that returns *only* `
|
|
636
|
+
* `qualify` that returns *only* `defect(cause)` yields `E = never` (a Defect is
|
|
640
637
|
* out-of-band and must not pollute the error channel); reach for
|
|
641
638
|
* {@link fromSafePromise} when every failure is a Defect.
|
|
642
639
|
*
|
|
@@ -645,13 +642,14 @@ function fromNullable(value, onAbsent) {
|
|
|
645
642
|
* @typeParam R - `qualify`'s return type; the modeled error `E` is
|
|
646
643
|
* `Exclude<R, Defect>` (its `Defect` arm, if any, is subtracted).
|
|
647
644
|
* @param fn - the throwing function to wrap.
|
|
648
|
-
* @param qualify - triages a thrown cause into `E
|
|
645
|
+
* @param qualify - triages a thrown `cause` into a modeled `E`, or marks it
|
|
646
|
+
* unmodeled by returning `defect(cause)` (the helper passed as its second arg).
|
|
649
647
|
* @returns a function with the same arguments returning `Result<T, E>`.
|
|
650
648
|
*
|
|
651
649
|
* @example
|
|
652
650
|
* ```ts
|
|
653
|
-
* import { fromThrowable
|
|
654
|
-
* const parse = fromThrowable(JSON.parse, (cause) =>
|
|
651
|
+
* import { fromThrowable } from "unthrown";
|
|
652
|
+
* const parse = fromThrowable(JSON.parse, (cause, defect) => defect(cause));
|
|
655
653
|
* parse("{}").unwrap();
|
|
656
654
|
* ```
|
|
657
655
|
*/
|
|
@@ -671,26 +669,27 @@ function fromThrowable(fn, qualify) {
|
|
|
671
669
|
*
|
|
672
670
|
* @remarks
|
|
673
671
|
* `qualify` **must** map each rejection cause into a modeled error `E` or a
|
|
674
|
-
*
|
|
675
|
-
* `await`-ing it always yields a
|
|
676
|
-
* `Defect`.
|
|
672
|
+
* `Defect` (via the injected `defect` helper, its second argument). The returned
|
|
673
|
+
* `AsyncResult`'s internal promise never rejects; `await`-ing it always yields a
|
|
674
|
+
* `Result`. A throw inside `qualify` is itself a `Defect`.
|
|
677
675
|
*
|
|
678
676
|
* The modeled error type is `Exclude<R, Defect>` — the `Defect` arm of
|
|
679
677
|
* `qualify`'s return is **subtracted** from `E`, never inferred into it. So a
|
|
680
|
-
* `qualify` that returns *only* `
|
|
678
|
+
* `qualify` that returns *only* `defect(cause)` yields `E = never`; when every
|
|
681
679
|
* rejection is a Defect, prefer {@link fromSafePromise}.
|
|
682
680
|
*
|
|
683
681
|
* @typeParam T - the resolved value type.
|
|
684
682
|
* @typeParam R - `qualify`'s return type; the modeled error `E` is
|
|
685
683
|
* `Exclude<R, Defect>` (its `Defect` arm, if any, is subtracted).
|
|
686
684
|
* @param promise - the promise, or a thunk returning one.
|
|
687
|
-
* @param qualify - triages a rejection cause into `E
|
|
685
|
+
* @param qualify - triages a rejection `cause` into a modeled `E`, or marks it
|
|
686
|
+
* unmodeled by returning `defect(cause)` (the helper passed as its second arg).
|
|
688
687
|
*
|
|
689
688
|
* @example
|
|
690
689
|
* ```ts
|
|
691
|
-
* import { fromPromise
|
|
692
|
-
* const user = await fromPromise(fetchUser(id), (cause) =>
|
|
693
|
-
* cause instanceof NotFoundError ? ("not_found" as const) :
|
|
690
|
+
* import { fromPromise } from "unthrown";
|
|
691
|
+
* const user = await fromPromise(fetchUser(id), (cause, defect) =>
|
|
692
|
+
* cause instanceof NotFoundError ? ("not_found" as const) : defect(cause),
|
|
694
693
|
* );
|
|
695
694
|
* ```
|
|
696
695
|
*/
|
|
@@ -715,7 +714,7 @@ function fromSafePromise(promise) {
|
|
|
715
714
|
}
|
|
716
715
|
function qualifyToResult(cause, qualify) {
|
|
717
716
|
try {
|
|
718
|
-
const q = qualify(cause);
|
|
717
|
+
const q = qualify(cause, defect);
|
|
719
718
|
return isDefectMarker(q) ? defectRes(q.cause) : errRes(q);
|
|
720
719
|
} catch (qErr) {
|
|
721
720
|
return defectRes(qErr);
|
|
@@ -731,8 +730,10 @@ function foldArray(results) {
|
|
|
731
730
|
let firstErr;
|
|
732
731
|
let firstDefect;
|
|
733
732
|
const values = [];
|
|
734
|
-
for (const r of results) if (r.tag === "Defect")
|
|
735
|
-
|
|
733
|
+
for (const r of results) if (r.tag === "Defect") {
|
|
734
|
+
firstDefect ??= r;
|
|
735
|
+
break;
|
|
736
|
+
} else if (r.tag === "Err") firstErr ??= r;
|
|
736
737
|
else values.push(r.value);
|
|
737
738
|
return firstDefect ?? firstErr ?? Ok(values);
|
|
738
739
|
}
|
|
@@ -846,10 +847,9 @@ function allFromDictAsync(results) {
|
|
|
846
847
|
/**
|
|
847
848
|
* Companion object grouping the **`Result`-producing** entry points under a
|
|
848
849
|
* single, discoverable namespace: {@link Result.Ok}, {@link Result.Err},
|
|
849
|
-
* {@link Result.
|
|
850
|
-
* {@link Result.
|
|
851
|
-
* {@link Result.
|
|
852
|
-
* {@link Result.isResult}.
|
|
850
|
+
* {@link Result.Do}, {@link Result.fromNullable}, {@link Result.fromThrowable},
|
|
851
|
+
* {@link Result.all}, {@link Result.allFromDict}, {@link Result.isOk},
|
|
852
|
+
* {@link Result.isErr}, {@link Result.isDefect}, {@link Result.isResult}.
|
|
853
853
|
*
|
|
854
854
|
* @remarks
|
|
855
855
|
* Purely additive sugar — each member **is** the corresponding free function.
|
|
@@ -870,7 +870,6 @@ function allFromDictAsync(results) {
|
|
|
870
870
|
const Result = {
|
|
871
871
|
Ok,
|
|
872
872
|
Err,
|
|
873
|
-
Defect,
|
|
874
873
|
Do,
|
|
875
874
|
fromNullable,
|
|
876
875
|
fromThrowable,
|
|
@@ -898,8 +897,8 @@ const Result = {
|
|
|
898
897
|
*
|
|
899
898
|
* @example
|
|
900
899
|
* ```ts
|
|
901
|
-
* import { AsyncResult
|
|
902
|
-
* const user = await AsyncResult.fromPromise(fetchUser(id), (c) =>
|
|
900
|
+
* import { AsyncResult } from "unthrown";
|
|
901
|
+
* const user = await AsyncResult.fromPromise(fetchUser(id), (c, defect) => defect(c));
|
|
903
902
|
* ```
|
|
904
903
|
*/
|
|
905
904
|
const AsyncResult = {
|
|
@@ -976,6 +975,6 @@ function matchTags(result, handlers) {
|
|
|
976
975
|
});
|
|
977
976
|
}
|
|
978
977
|
//#endregion
|
|
979
|
-
export { AsyncResult,
|
|
978
|
+
export { AsyncResult, Do, Err, Ok, Result, TaggedError, UnwrapError, all, allAsync, allFromDict, allFromDictAsync, fromNullable, fromPromise, fromSafePromise, fromThrowable, isDefect, isErr, isOk, isResult, matchTags };
|
|
980
979
|
|
|
981
980
|
//# sourceMappingURL=index.mjs.map
|