unthrown 0.1.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/LICENSE +21 -0
- package/README.md +40 -0
- package/dist/index.cjs +668 -0
- package/dist/index.d.cts +663 -0
- package/dist/index.d.cts.map +1 -0
- package/dist/index.d.mts +663 -0
- package/dist/index.d.mts.map +1 -0
- package/dist/index.mjs +655 -0
- package/dist/index.mjs.map +1 -0
- package/docs/index.md +1163 -0
- package/package.json +69 -0
package/docs/index.md
ADDED
|
@@ -0,0 +1,1163 @@
|
|
|
1
|
+
**unthrown**
|
|
2
|
+
|
|
3
|
+
***
|
|
4
|
+
|
|
5
|
+
# unthrown
|
|
6
|
+
|
|
7
|
+
## Classes
|
|
8
|
+
|
|
9
|
+
### UnwrapError
|
|
10
|
+
|
|
11
|
+
Defined in: [packages/core/src/core.ts:29](https://github.com/btravstack/unthrown/blob/4553631adb8280ef7c869dfae5bdb13be762095c/packages/core/src/core.ts#L29)
|
|
12
|
+
|
|
13
|
+
Thrown by a [Result](#result)'s `unwrap` / `unwrapErr` when the assertion is
|
|
14
|
+
wrong on a *modeled* result — `unwrap()` on an `Err`, or `unwrapErr()` on an
|
|
15
|
+
`Ok`.
|
|
16
|
+
|
|
17
|
+
#### Remarks
|
|
18
|
+
|
|
19
|
+
A `Defect` is never wrapped in an `UnwrapError`: its original cause is
|
|
20
|
+
re-thrown (with its original stack) instead.
|
|
21
|
+
|
|
22
|
+
#### Extends
|
|
23
|
+
|
|
24
|
+
- `Error`
|
|
25
|
+
|
|
26
|
+
#### Type Parameters
|
|
27
|
+
|
|
28
|
+
| Type Parameter | Default type | Description |
|
|
29
|
+
| ------ | ------ | ------ |
|
|
30
|
+
| `E` | `unknown` | the type of the [UnwrapError.error](#error) it carries. |
|
|
31
|
+
|
|
32
|
+
#### Constructors
|
|
33
|
+
|
|
34
|
+
##### Constructor
|
|
35
|
+
|
|
36
|
+
```ts
|
|
37
|
+
new UnwrapError<E>(error): UnwrapError<E>;
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
Defined in: [packages/core/src/core.ts:35](https://github.com/btravstack/unthrown/blob/4553631adb8280ef7c869dfae5bdb13be762095c/packages/core/src/core.ts#L35)
|
|
41
|
+
|
|
42
|
+
###### Parameters
|
|
43
|
+
|
|
44
|
+
| Parameter | Type |
|
|
45
|
+
| ------ | ------ |
|
|
46
|
+
| `error` | `E` |
|
|
47
|
+
|
|
48
|
+
###### Returns
|
|
49
|
+
|
|
50
|
+
[`UnwrapError`](#unwraperror)<`E`>
|
|
51
|
+
|
|
52
|
+
###### Overrides
|
|
53
|
+
|
|
54
|
+
```ts
|
|
55
|
+
Error.constructor
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
#### Properties
|
|
59
|
+
|
|
60
|
+
| Property | Modifier | Type | Description | Inherited from | Defined in |
|
|
61
|
+
| ------ | ------ | ------ | ------ | ------ | ------ |
|
|
62
|
+
| <a id="cause"></a> `cause?` | `public` | `unknown` | - | `Error.cause` | node\_modules/.pnpm/typescript@6.0.3/node\_modules/typescript/lib/lib.es2022.error.d.ts:24 |
|
|
63
|
+
| <a id="error"></a> `error` | `readonly` | `E` | The offending value: the `Err` error for `unwrap()`, or the `Ok` value for `unwrapErr()`. | - | [packages/core/src/core.ts:34](https://github.com/btravstack/unthrown/blob/4553631adb8280ef7c869dfae5bdb13be762095c/packages/core/src/core.ts#L34) |
|
|
64
|
+
| <a id="message"></a> `message` | `public` | `string` | - | `Error.message` | node\_modules/.pnpm/typescript@6.0.3/node\_modules/typescript/lib/lib.es5.d.ts:1075 |
|
|
65
|
+
| <a id="name"></a> `name` | `public` | `string` | - | `Error.name` | node\_modules/.pnpm/typescript@6.0.3/node\_modules/typescript/lib/lib.es5.d.ts:1074 |
|
|
66
|
+
| <a id="stack"></a> `stack?` | `public` | `string` | - | `Error.stack` | node\_modules/.pnpm/typescript@6.0.3/node\_modules/typescript/lib/lib.es5.d.ts:1076 |
|
|
67
|
+
| <a id="stacktracelimit"></a> `stackTraceLimit` | `static` | `number` | The `Error.stackTraceLimit` property specifies the number of stack frames collected by a stack trace (whether generated by `new Error().stack` or `Error.captureStackTrace(obj)`). The default value is `10` but may be set to any valid JavaScript number. Changes will affect any stack trace captured _after_ the value has been changed. If set to a non-number value, or set to a negative number, stack traces will not capture any frames. | `Error.stackTraceLimit` | node\_modules/.pnpm/@types+node@24.13.2/node\_modules/@types/node/globals.d.ts:68 |
|
|
68
|
+
|
|
69
|
+
#### Methods
|
|
70
|
+
|
|
71
|
+
##### captureStackTrace()
|
|
72
|
+
|
|
73
|
+
```ts
|
|
74
|
+
static captureStackTrace(targetObject, constructorOpt?): void;
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
Defined in: node\_modules/.pnpm/@types+node@24.13.2/node\_modules/@types/node/globals.d.ts:52
|
|
78
|
+
|
|
79
|
+
Creates a `.stack` property on `targetObject`, which when accessed returns
|
|
80
|
+
a string representing the location in the code at which
|
|
81
|
+
`Error.captureStackTrace()` was called.
|
|
82
|
+
|
|
83
|
+
```js
|
|
84
|
+
const myObject = {};
|
|
85
|
+
Error.captureStackTrace(myObject);
|
|
86
|
+
myObject.stack; // Similar to `new Error().stack`
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
The first line of the trace will be prefixed with
|
|
90
|
+
`${myObject.name}: ${myObject.message}`.
|
|
91
|
+
|
|
92
|
+
The optional `constructorOpt` argument accepts a function. If given, all frames
|
|
93
|
+
above `constructorOpt`, including `constructorOpt`, will be omitted from the
|
|
94
|
+
generated stack trace.
|
|
95
|
+
|
|
96
|
+
The `constructorOpt` argument is useful for hiding implementation
|
|
97
|
+
details of error generation from the user. For instance:
|
|
98
|
+
|
|
99
|
+
```js
|
|
100
|
+
function a() {
|
|
101
|
+
b();
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
function b() {
|
|
105
|
+
c();
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
function c() {
|
|
109
|
+
// Create an error without stack trace to avoid calculating the stack trace twice.
|
|
110
|
+
const { stackTraceLimit } = Error;
|
|
111
|
+
Error.stackTraceLimit = 0;
|
|
112
|
+
const error = new Error();
|
|
113
|
+
Error.stackTraceLimit = stackTraceLimit;
|
|
114
|
+
|
|
115
|
+
// Capture the stack trace above function b
|
|
116
|
+
Error.captureStackTrace(error, b); // Neither function c, nor b is included in the stack trace
|
|
117
|
+
throw error;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
a();
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
###### Parameters
|
|
124
|
+
|
|
125
|
+
| Parameter | Type |
|
|
126
|
+
| ------ | ------ |
|
|
127
|
+
| `targetObject` | `object` |
|
|
128
|
+
| `constructorOpt?` | `Function` |
|
|
129
|
+
|
|
130
|
+
###### Returns
|
|
131
|
+
|
|
132
|
+
`void`
|
|
133
|
+
|
|
134
|
+
###### Inherited from
|
|
135
|
+
|
|
136
|
+
```ts
|
|
137
|
+
Error.captureStackTrace
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
##### prepareStackTrace()
|
|
141
|
+
|
|
142
|
+
```ts
|
|
143
|
+
static prepareStackTrace(err, stackTraces): any;
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
Defined in: node\_modules/.pnpm/@types+node@24.13.2/node\_modules/@types/node/globals.d.ts:56
|
|
147
|
+
|
|
148
|
+
###### Parameters
|
|
149
|
+
|
|
150
|
+
| Parameter | Type |
|
|
151
|
+
| ------ | ------ |
|
|
152
|
+
| `err` | `Error` |
|
|
153
|
+
| `stackTraces` | `CallSite`[] |
|
|
154
|
+
|
|
155
|
+
###### Returns
|
|
156
|
+
|
|
157
|
+
`any`
|
|
158
|
+
|
|
159
|
+
###### See
|
|
160
|
+
|
|
161
|
+
https://v8.dev/docs/stack-trace-api#customizing-stack-traces
|
|
162
|
+
|
|
163
|
+
###### Inherited from
|
|
164
|
+
|
|
165
|
+
```ts
|
|
166
|
+
Error.prepareStackTrace
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
## Type Aliases
|
|
170
|
+
|
|
171
|
+
### AsyncResult
|
|
172
|
+
|
|
173
|
+
```ts
|
|
174
|
+
type AsyncResult<T, E> = Awaitable<Result<T, E>> & object;
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
Defined in: [packages/core/src/types.ts:278](https://github.com/btravstack/unthrown/blob/4553631adb8280ef7c869dfae5bdb13be762095c/packages/core/src/types.ts#L278)
|
|
178
|
+
|
|
179
|
+
The asynchronous counterpart of [Result](#result): an awaitable wrapper with the
|
|
180
|
+
same method surface, collapsing to a `Result<T, E>` when `await`-ed.
|
|
181
|
+
|
|
182
|
+
#### Type Declaration
|
|
183
|
+
|
|
184
|
+
| Name | Type | Description | Defined in |
|
|
185
|
+
| ------ | ------ | ------ | ------ |
|
|
186
|
+
| `as()` | (`value`) => [`AsyncResult`](#asyncresult)<`U`, `E`> | Asynchronous `as`. | [packages/core/src/types.ts:289](https://github.com/btravstack/unthrown/blob/4553631adb8280ef7c869dfae5bdb13be762095c/packages/core/src/types.ts#L289) |
|
|
187
|
+
| `flatMap()` | (`f`) => [`AsyncResult`](#asyncresult)<`U`, `E` \| `E2`> | Asynchronous `flatMap`. `f` may return a `Result` **or** an `AsyncResult` (never a raw `Promise`); a throw becomes a `Defect`. | [packages/core/src/types.ts:285](https://github.com/btravstack/unthrown/blob/4553631adb8280ef7c869dfae5bdb13be762095c/packages/core/src/types.ts#L285) |
|
|
188
|
+
| `getOrNull()` | () => `Promise`<`T` \| `null`> | Asynchronous `getOrNull`. | [packages/core/src/types.ts:322](https://github.com/btravstack/unthrown/blob/4553631adb8280ef7c869dfae5bdb13be762095c/packages/core/src/types.ts#L322) |
|
|
189
|
+
| `getOrUndefined()` | () => `Promise`<`T` \| `undefined`> | Asynchronous `getOrUndefined`. | [packages/core/src/types.ts:324](https://github.com/btravstack/unthrown/blob/4553631adb8280ef7c869dfae5bdb13be762095c/packages/core/src/types.ts#L324) |
|
|
190
|
+
| `map()` | (`f`) => [`AsyncResult`](#asyncresult)<`U`, `E`> | Asynchronous `map`. `f` is synchronous; a throw becomes a `Defect`. | [packages/core/src/types.ts:280](https://github.com/btravstack/unthrown/blob/4553631adb8280ef7c869dfae5bdb13be762095c/packages/core/src/types.ts#L280) |
|
|
191
|
+
| `mapErr()` | (`f`) => [`AsyncResult`](#asyncresult)<`T`, `E2`> | Asynchronous `mapErr`. `f` is synchronous; a throw becomes a `Defect`. | [packages/core/src/types.ts:292](https://github.com/btravstack/unthrown/blob/4553631adb8280ef7c869dfae5bdb13be762095c/packages/core/src/types.ts#L292) |
|
|
192
|
+
| `match()` | (`cases`) => `Promise`<`R`> | Asynchronous `match`. Handlers are synchronous; resolves to `R`. | [packages/core/src/types.ts:308](https://github.com/btravstack/unthrown/blob/4553631adb8280ef7c869dfae5bdb13be762095c/packages/core/src/types.ts#L308) |
|
|
193
|
+
| `orElse()` | (`f`) => [`AsyncResult`](#asyncresult)<`T` \| `U`, `E2`> | Asynchronous `orElse`. `f` may return a `Result` or an `AsyncResult`. | [packages/core/src/types.ts:294](https://github.com/btravstack/unthrown/blob/4553631adb8280ef7c869dfae5bdb13be762095c/packages/core/src/types.ts#L294) |
|
|
194
|
+
| `recover()` | (`f`) => [`AsyncResult`](#asyncresult)<`T` \| `U`, `never`> | Asynchronous `recover`. `f` is synchronous; a throw becomes a `Defect`. | [packages/core/src/types.ts:296](https://github.com/btravstack/unthrown/blob/4553631adb8280ef7c869dfae5bdb13be762095c/packages/core/src/types.ts#L296) |
|
|
195
|
+
| `recoverDefect()` | (`f`) => [`AsyncResult`](#asyncresult)<`T` \| `U`, `E` \| `E2`> | Asynchronous `recoverDefect`. `f` may return a `Result` or an `AsyncResult`. | [packages/core/src/types.ts:301](https://github.com/btravstack/unthrown/blob/4553631adb8280ef7c869dfae5bdb13be762095c/packages/core/src/types.ts#L301) |
|
|
196
|
+
| `tap()` | (`f`) => [`AsyncResult`](#asyncresult)<`T`, `E`> | Asynchronous `tap`. `f` is synchronous; a throw becomes a `Defect`. | [packages/core/src/types.ts:287](https://github.com/btravstack/unthrown/blob/4553631adb8280ef7c869dfae5bdb13be762095c/packages/core/src/types.ts#L287) |
|
|
197
|
+
| `tapDefect()` | (`f`) => [`AsyncResult`](#asyncresult)<`T`, `E`> | Asynchronous `tapDefect`. | [packages/core/src/types.ts:305](https://github.com/btravstack/unthrown/blob/4553631adb8280ef7c869dfae5bdb13be762095c/packages/core/src/types.ts#L305) |
|
|
198
|
+
| `tapErr()` | (`f`) => [`AsyncResult`](#asyncresult)<`T`, `E`> | Asynchronous `tapErr`. `f` is synchronous; a throw becomes a `Defect`. | [packages/core/src/types.ts:298](https://github.com/btravstack/unthrown/blob/4553631adb8280ef7c869dfae5bdb13be762095c/packages/core/src/types.ts#L298) |
|
|
199
|
+
| `unwrap()` | () => `Promise`<`T`> | Asynchronous `unwrap`. The returned promise rejects on `Err`/`Defect`. | [packages/core/src/types.ts:314](https://github.com/btravstack/unthrown/blob/4553631adb8280ef7c869dfae5bdb13be762095c/packages/core/src/types.ts#L314) |
|
|
200
|
+
| `unwrapErr()` | () => `Promise`<`E`> | Asynchronous `unwrapErr`. | [packages/core/src/types.ts:316](https://github.com/btravstack/unthrown/blob/4553631adb8280ef7c869dfae5bdb13be762095c/packages/core/src/types.ts#L316) |
|
|
201
|
+
| `unwrapOr()` | (`fallback`) => `Promise`<`T`> | Asynchronous `unwrapOr`. | [packages/core/src/types.ts:318](https://github.com/btravstack/unthrown/blob/4553631adb8280ef7c869dfae5bdb13be762095c/packages/core/src/types.ts#L318) |
|
|
202
|
+
| `unwrapOrElse()` | (`f`) => `Promise`<`T`> | Asynchronous `unwrapOrElse`. | [packages/core/src/types.ts:320](https://github.com/btravstack/unthrown/blob/4553631adb8280ef7c869dfae5bdb13be762095c/packages/core/src/types.ts#L320) |
|
|
203
|
+
|
|
204
|
+
#### Type Parameters
|
|
205
|
+
|
|
206
|
+
| Type Parameter | Description |
|
|
207
|
+
| ------ | ------ |
|
|
208
|
+
| `T` | the success value type. |
|
|
209
|
+
| `E` | the modeled error type. |
|
|
210
|
+
|
|
211
|
+
#### Remarks
|
|
212
|
+
|
|
213
|
+
**Combinator callbacks are synchronous.** A raw `Promise` may never enter an
|
|
214
|
+
`AsyncResult` method — that would be an un-qualified async boundary, and its
|
|
215
|
+
rejection would silently become a `Defect`, skipping the triage that
|
|
216
|
+
[fromPromise](#frompromise) forces. To do further async work, re-enter through a
|
|
217
|
+
qualified boundary and compose it: `ar.flatMap((v) => fromPromise(work(v),
|
|
218
|
+
qualify))`. The eliminators (`unwrap`, …) return promises; the binds
|
|
219
|
+
(`flatMap`, `orElse`, `recoverDefect`) additionally accept an `AsyncResult`.
|
|
220
|
+
|
|
221
|
+
To pattern-match an `AsyncResult`, `await` it first: `match(await ar)`.
|
|
222
|
+
|
|
223
|
+
***
|
|
224
|
+
|
|
225
|
+
### Awaitable
|
|
226
|
+
|
|
227
|
+
```ts
|
|
228
|
+
type Awaitable<T> = object;
|
|
229
|
+
```
|
|
230
|
+
|
|
231
|
+
Defined in: [packages/core/src/types.ts:256](https://github.com/btravstack/unthrown/blob/4553631adb8280ef7c869dfae5bdb13be762095c/packages/core/src/types.ts#L256)
|
|
232
|
+
|
|
233
|
+
A success-only thenable: awaitable, but deliberately **not** a full
|
|
234
|
+
`PromiseLike`.
|
|
235
|
+
|
|
236
|
+
#### Remarks
|
|
237
|
+
|
|
238
|
+
An [AsyncResult](#asyncresult)'s internal promise never rejects, so `await`-ing one
|
|
239
|
+
always yields a [Result](#result) and never throws — there is no rejection
|
|
240
|
+
channel to model, and none is advertised. At runtime it is still a thenable
|
|
241
|
+
(the only way `await` can collapse it); the narrowing simply keeps it from
|
|
242
|
+
being treated as a raw promise (e.g. dropped into `Promise.all`).
|
|
243
|
+
|
|
244
|
+
#### Type Parameters
|
|
245
|
+
|
|
246
|
+
| Type Parameter | Description |
|
|
247
|
+
| ------ | ------ |
|
|
248
|
+
| `T` | the value `await` resolves to. |
|
|
249
|
+
|
|
250
|
+
#### Methods
|
|
251
|
+
|
|
252
|
+
##### then()
|
|
253
|
+
|
|
254
|
+
```ts
|
|
255
|
+
then<R>(onfulfilled?): PromiseLike<R>;
|
|
256
|
+
```
|
|
257
|
+
|
|
258
|
+
Defined in: [packages/core/src/types.ts:257](https://github.com/btravstack/unthrown/blob/4553631adb8280ef7c869dfae5bdb13be762095c/packages/core/src/types.ts#L257)
|
|
259
|
+
|
|
260
|
+
###### Type Parameters
|
|
261
|
+
|
|
262
|
+
| Type Parameter | Default type |
|
|
263
|
+
| ------ | ------ |
|
|
264
|
+
| `R` | `T` |
|
|
265
|
+
|
|
266
|
+
###### Parameters
|
|
267
|
+
|
|
268
|
+
| Parameter | Type |
|
|
269
|
+
| ------ | ------ |
|
|
270
|
+
| `onfulfilled?` | ((`value`) => `R` \| `PromiseLike`<`R`>) \| `null` |
|
|
271
|
+
|
|
272
|
+
###### Returns
|
|
273
|
+
|
|
274
|
+
`PromiseLike`<`R`>
|
|
275
|
+
|
|
276
|
+
***
|
|
277
|
+
|
|
278
|
+
### Defect
|
|
279
|
+
|
|
280
|
+
```ts
|
|
281
|
+
type Defect = object;
|
|
282
|
+
```
|
|
283
|
+
|
|
284
|
+
Defined in: [packages/core/src/defect.ts:15](https://github.com/btravstack/unthrown/blob/4553631adb8280ef7c869dfae5bdb13be762095c/packages/core/src/defect.ts#L15)
|
|
285
|
+
|
|
286
|
+
The marker a `qualify` function returns to triage a cause as **unexpected**.
|
|
287
|
+
|
|
288
|
+
#### Remarks
|
|
289
|
+
|
|
290
|
+
`qualify` (passed to [fromPromise](#frompromise) / [fromThrowable](#fromthrowable)) returns
|
|
291
|
+
`E | Defect`: either a modeled domain error, or a `Defect` produced by
|
|
292
|
+
[defect](#defect-2) to say "this failure is not modeled". A `Defect` is opaque —
|
|
293
|
+
it carries the original cause for the boundary to convert into the third
|
|
294
|
+
runtime state of a `Result`.
|
|
295
|
+
|
|
296
|
+
#### Properties
|
|
297
|
+
|
|
298
|
+
| Property | Modifier | Type | Defined in |
|
|
299
|
+
| ------ | ------ | ------ | ------ |
|
|
300
|
+
| <a id="defect-1"></a> `[DEFECT]` | `readonly` | `true` | [packages/core/src/defect.ts:16](https://github.com/btravstack/unthrown/blob/4553631adb8280ef7c869dfae5bdb13be762095c/packages/core/src/defect.ts#L16) |
|
|
301
|
+
| <a id="cause-1"></a> `cause` | `readonly` | `unknown` | [packages/core/src/defect.ts:17](https://github.com/btravstack/unthrown/blob/4553631adb8280ef7c869dfae5bdb13be762095c/packages/core/src/defect.ts#L17) |
|
|
302
|
+
|
|
303
|
+
***
|
|
304
|
+
|
|
305
|
+
### DefectView
|
|
306
|
+
|
|
307
|
+
```ts
|
|
308
|
+
type DefectView<T, E> = ResultMethods<T, E> & object;
|
|
309
|
+
```
|
|
310
|
+
|
|
311
|
+
Defined in: [packages/core/src/types.ts:199](https://github.com/btravstack/unthrown/blob/4553631adb8280ef7c869dfae5bdb13be762095c/packages/core/src/types.ts#L199)
|
|
312
|
+
|
|
313
|
+
The `Defect` variant of a [Result](#result): an unmodeled failure carrying a `cause`.
|
|
314
|
+
|
|
315
|
+
#### Type Declaration
|
|
316
|
+
|
|
317
|
+
| Name | Type | Defined in |
|
|
318
|
+
| ------ | ------ | ------ |
|
|
319
|
+
| `cause` | `unknown` | [packages/core/src/types.ts:201](https://github.com/btravstack/unthrown/blob/4553631adb8280ef7c869dfae5bdb13be762095c/packages/core/src/types.ts#L201) |
|
|
320
|
+
| `tag` | `"Defect"` | [packages/core/src/types.ts:200](https://github.com/btravstack/unthrown/blob/4553631adb8280ef7c869dfae5bdb13be762095c/packages/core/src/types.ts#L200) |
|
|
321
|
+
|
|
322
|
+
#### Type Parameters
|
|
323
|
+
|
|
324
|
+
| Type Parameter | Default type |
|
|
325
|
+
| ------ | ------ |
|
|
326
|
+
| `T` | `never` |
|
|
327
|
+
| `E` | `never` |
|
|
328
|
+
|
|
329
|
+
***
|
|
330
|
+
|
|
331
|
+
### ErrOf
|
|
332
|
+
|
|
333
|
+
```ts
|
|
334
|
+
type ErrOf<R> = R extends object ? E : never;
|
|
335
|
+
```
|
|
336
|
+
|
|
337
|
+
Defined in: [packages/core/src/types.ts:338](https://github.com/btravstack/unthrown/blob/4553631adb8280ef7c869dfae5bdb13be762095c/packages/core/src/types.ts#L338)
|
|
338
|
+
|
|
339
|
+
Extract the error type `E` from a `Result`.
|
|
340
|
+
|
|
341
|
+
#### Type Parameters
|
|
342
|
+
|
|
343
|
+
| Type Parameter | Description |
|
|
344
|
+
| ------ | ------ |
|
|
345
|
+
| `R` | the `Result` type to inspect. |
|
|
346
|
+
|
|
347
|
+
***
|
|
348
|
+
|
|
349
|
+
### ErrView
|
|
350
|
+
|
|
351
|
+
```ts
|
|
352
|
+
type ErrView<E, T> = ResultMethods<T, E> & object;
|
|
353
|
+
```
|
|
354
|
+
|
|
355
|
+
Defined in: [packages/core/src/types.ts:194](https://github.com/btravstack/unthrown/blob/4553631adb8280ef7c869dfae5bdb13be762095c/packages/core/src/types.ts#L194)
|
|
356
|
+
|
|
357
|
+
The `Err` variant of a [Result](#result): a modeled failure carrying an `error`.
|
|
358
|
+
|
|
359
|
+
#### Type Declaration
|
|
360
|
+
|
|
361
|
+
| Name | Type | Defined in |
|
|
362
|
+
| ------ | ------ | ------ |
|
|
363
|
+
| `error` | `E` | [packages/core/src/types.ts:196](https://github.com/btravstack/unthrown/blob/4553631adb8280ef7c869dfae5bdb13be762095c/packages/core/src/types.ts#L196) |
|
|
364
|
+
| `tag` | `"Err"` | [packages/core/src/types.ts:195](https://github.com/btravstack/unthrown/blob/4553631adb8280ef7c869dfae5bdb13be762095c/packages/core/src/types.ts#L195) |
|
|
365
|
+
|
|
366
|
+
#### Type Parameters
|
|
367
|
+
|
|
368
|
+
| Type Parameter | Default type |
|
|
369
|
+
| ------ | ------ |
|
|
370
|
+
| `E` | - |
|
|
371
|
+
| `T` | `never` |
|
|
372
|
+
|
|
373
|
+
***
|
|
374
|
+
|
|
375
|
+
### OkOf
|
|
376
|
+
|
|
377
|
+
```ts
|
|
378
|
+
type OkOf<R> = R extends object ? T : never;
|
|
379
|
+
```
|
|
380
|
+
|
|
381
|
+
Defined in: [packages/core/src/types.ts:332](https://github.com/btravstack/unthrown/blob/4553631adb8280ef7c869dfae5bdb13be762095c/packages/core/src/types.ts#L332)
|
|
382
|
+
|
|
383
|
+
Extract the success type `T` from a `Result`.
|
|
384
|
+
|
|
385
|
+
#### Type Parameters
|
|
386
|
+
|
|
387
|
+
| Type Parameter | Description |
|
|
388
|
+
| ------ | ------ |
|
|
389
|
+
| `R` | the `Result` type to inspect. |
|
|
390
|
+
|
|
391
|
+
***
|
|
392
|
+
|
|
393
|
+
### OkView
|
|
394
|
+
|
|
395
|
+
```ts
|
|
396
|
+
type OkView<T, E> = ResultMethods<T, E> & object;
|
|
397
|
+
```
|
|
398
|
+
|
|
399
|
+
Defined in: [packages/core/src/types.ts:189](https://github.com/btravstack/unthrown/blob/4553631adb8280ef7c869dfae5bdb13be762095c/packages/core/src/types.ts#L189)
|
|
400
|
+
|
|
401
|
+
The `Ok` variant of a [Result](#result): a success carrying a `value`.
|
|
402
|
+
|
|
403
|
+
#### Type Declaration
|
|
404
|
+
|
|
405
|
+
| Name | Type | Defined in |
|
|
406
|
+
| ------ | ------ | ------ |
|
|
407
|
+
| `tag` | `"Ok"` | [packages/core/src/types.ts:190](https://github.com/btravstack/unthrown/blob/4553631adb8280ef7c869dfae5bdb13be762095c/packages/core/src/types.ts#L190) |
|
|
408
|
+
| `value` | `T` | [packages/core/src/types.ts:191](https://github.com/btravstack/unthrown/blob/4553631adb8280ef7c869dfae5bdb13be762095c/packages/core/src/types.ts#L191) |
|
|
409
|
+
|
|
410
|
+
#### Type Parameters
|
|
411
|
+
|
|
412
|
+
| Type Parameter | Default type |
|
|
413
|
+
| ------ | ------ |
|
|
414
|
+
| `T` | - |
|
|
415
|
+
| `E` | `never` |
|
|
416
|
+
|
|
417
|
+
***
|
|
418
|
+
|
|
419
|
+
### Result
|
|
420
|
+
|
|
421
|
+
```ts
|
|
422
|
+
type Result<T, E> = ResultType<T, E>;
|
|
423
|
+
```
|
|
424
|
+
|
|
425
|
+
Defined in: [packages/core/src/facade.ts:31](https://github.com/btravstack/unthrown/blob/4553631adb8280ef7c869dfae5bdb13be762095c/packages/core/src/facade.ts#L31)
|
|
426
|
+
|
|
427
|
+
Companion object grouping the standalone entry points under a single,
|
|
428
|
+
discoverable namespace: [Result.ok](#property-ok), [Result.err](#property-err),
|
|
429
|
+
[Result.defect](#property-defect), [Result.fromNullable](#property-fromnullable), [Result.fromThrowable](#property-fromthrowable),
|
|
430
|
+
[Result.fromPromise](#property-frompromise), [Result.fromSafePromise](#property-fromsafepromise), [Result.all](#property-all),
|
|
431
|
+
[Result.isOk](#property-isok), [Result.isErr](#property-iserr), [Result.isDefect](#property-isdefect).
|
|
432
|
+
|
|
433
|
+
#### Type Parameters
|
|
434
|
+
|
|
435
|
+
| Type Parameter |
|
|
436
|
+
| ------ |
|
|
437
|
+
| `T` |
|
|
438
|
+
| `E` |
|
|
439
|
+
|
|
440
|
+
#### Remarks
|
|
441
|
+
|
|
442
|
+
Purely additive sugar — each member **is** the corresponding free function.
|
|
443
|
+
The free functions remain the primary, tree-shakeable API; importing only
|
|
444
|
+
`{ ok }` never pulls this object in. The value `Result` and the type
|
|
445
|
+
[Result](#result-1) share one name (the companion-object pattern).
|
|
446
|
+
|
|
447
|
+
#### Example
|
|
448
|
+
|
|
449
|
+
```ts
|
|
450
|
+
import { Result } from "unthrown";
|
|
451
|
+
Result.ok(1).flatMap((n) => Result.ok(n + 1)).unwrap(); // 2
|
|
452
|
+
```
|
|
453
|
+
|
|
454
|
+
***
|
|
455
|
+
|
|
456
|
+
### TaggedErrorConstructor
|
|
457
|
+
|
|
458
|
+
```ts
|
|
459
|
+
type TaggedErrorConstructor<Tag> = <A>(args) => TaggedErrorInstance<Tag, A>;
|
|
460
|
+
```
|
|
461
|
+
|
|
462
|
+
Defined in: [packages/core/src/tagged.ts:28](https://github.com/btravstack/unthrown/blob/4553631adb8280ef7c869dfae5bdb13be762095c/packages/core/src/tagged.ts#L28)
|
|
463
|
+
|
|
464
|
+
The class constructor returned by [TaggedError](#taggederror). Generic in its payload:
|
|
465
|
+
apply it with an instantiation expression at the `extends` site.
|
|
466
|
+
|
|
467
|
+
#### Type Parameters
|
|
468
|
+
|
|
469
|
+
| Type Parameter | Description |
|
|
470
|
+
| ------ | ------ |
|
|
471
|
+
| `Tag` *extends* `string` | the string literal discriminant. |
|
|
472
|
+
|
|
473
|
+
#### Parameters
|
|
474
|
+
|
|
475
|
+
| Parameter | Type |
|
|
476
|
+
| ------ | ------ |
|
|
477
|
+
| `args` | keyof `A` *extends* `never` ? `void` : `A` |
|
|
478
|
+
|
|
479
|
+
#### Returns
|
|
480
|
+
|
|
481
|
+
[`TaggedErrorInstance`](#taggederrorinstance)<`Tag`, `A`>
|
|
482
|
+
|
|
483
|
+
#### Remarks
|
|
484
|
+
|
|
485
|
+
When the payload is empty, the constructor takes **no** arguments (the
|
|
486
|
+
`keyof A extends never ? void : A` trick); otherwise it takes the payload.
|
|
487
|
+
|
|
488
|
+
***
|
|
489
|
+
|
|
490
|
+
### TaggedErrorInstance
|
|
491
|
+
|
|
492
|
+
```ts
|
|
493
|
+
type TaggedErrorInstance<Tag, A> = Error & Readonly<A> & object;
|
|
494
|
+
```
|
|
495
|
+
|
|
496
|
+
Defined in: [packages/core/src/tagged.ts:15](https://github.com/btravstack/unthrown/blob/4553631adb8280ef7c869dfae5bdb13be762095c/packages/core/src/tagged.ts#L15)
|
|
497
|
+
|
|
498
|
+
The instance shape produced by a [TaggedError](#taggederror) class: an `Error` plus a
|
|
499
|
+
`_tag` discriminant and the (readonly) payload fields.
|
|
500
|
+
|
|
501
|
+
#### Type Declaration
|
|
502
|
+
|
|
503
|
+
| Name | Type | Defined in |
|
|
504
|
+
| ------ | ------ | ------ |
|
|
505
|
+
| `_tag` | `Tag` | [packages/core/src/tagged.ts:16](https://github.com/btravstack/unthrown/blob/4553631adb8280ef7c869dfae5bdb13be762095c/packages/core/src/tagged.ts#L16) |
|
|
506
|
+
|
|
507
|
+
#### Type Parameters
|
|
508
|
+
|
|
509
|
+
| Type Parameter | Description |
|
|
510
|
+
| ------ | ------ |
|
|
511
|
+
| `Tag` *extends* `string` | the string literal discriminant. |
|
|
512
|
+
| `A` *extends* `Props` | the payload object type. |
|
|
513
|
+
|
|
514
|
+
***
|
|
515
|
+
|
|
516
|
+
### TagHandlers
|
|
517
|
+
|
|
518
|
+
```ts
|
|
519
|
+
type TagHandlers<T, E, R> = object & { [K in E["_tag"]]: (error: Extract<E, { _tag: K }>) => R };
|
|
520
|
+
```
|
|
521
|
+
|
|
522
|
+
Defined in: [packages/core/src/tagged.ts:81](https://github.com/btravstack/unthrown/blob/4553631adb8280ef7c869dfae5bdb13be762095c/packages/core/src/tagged.ts#L81)
|
|
523
|
+
|
|
524
|
+
The handler object [matchTags](#matchtags) requires: a branch per error tag, plus
|
|
525
|
+
`Ok` and `Defect`. Miss a tag and it will not compile — the exhaustiveness is
|
|
526
|
+
enforced by the type, with no `.exhaustive()` to forget.
|
|
527
|
+
|
|
528
|
+
#### Type Declaration
|
|
529
|
+
|
|
530
|
+
| Name | Type | Defined in |
|
|
531
|
+
| ------ | ------ | ------ |
|
|
532
|
+
| `Defect()` | (`cause`) => `R` | [packages/core/src/tagged.ts:83](https://github.com/btravstack/unthrown/blob/4553631adb8280ef7c869dfae5bdb13be762095c/packages/core/src/tagged.ts#L83) |
|
|
533
|
+
| `Ok()` | (`value`) => `R` | [packages/core/src/tagged.ts:82](https://github.com/btravstack/unthrown/blob/4553631adb8280ef7c869dfae5bdb13be762095c/packages/core/src/tagged.ts#L82) |
|
|
534
|
+
|
|
535
|
+
#### Type Parameters
|
|
536
|
+
|
|
537
|
+
| Type Parameter | Description |
|
|
538
|
+
| ------ | ------ |
|
|
539
|
+
| `T` | the success value type. |
|
|
540
|
+
| `E` *extends* `object` | the tagged error union. |
|
|
541
|
+
| `R` | the folded result type. |
|
|
542
|
+
|
|
543
|
+
## Variables
|
|
544
|
+
|
|
545
|
+
### Result
|
|
546
|
+
|
|
547
|
+
```ts
|
|
548
|
+
const Result: object;
|
|
549
|
+
```
|
|
550
|
+
|
|
551
|
+
Defined in: [packages/core/src/facade.ts:31](https://github.com/btravstack/unthrown/blob/4553631adb8280ef7c869dfae5bdb13be762095c/packages/core/src/facade.ts#L31)
|
|
552
|
+
|
|
553
|
+
Companion object grouping the standalone entry points under a single,
|
|
554
|
+
discoverable namespace: [Result.ok](#property-ok), [Result.err](#property-err),
|
|
555
|
+
[Result.defect](#property-defect), [Result.fromNullable](#property-fromnullable), [Result.fromThrowable](#property-fromthrowable),
|
|
556
|
+
[Result.fromPromise](#property-frompromise), [Result.fromSafePromise](#property-fromsafepromise), [Result.all](#property-all),
|
|
557
|
+
[Result.isOk](#property-isok), [Result.isErr](#property-iserr), [Result.isDefect](#property-isdefect).
|
|
558
|
+
|
|
559
|
+
#### Type Declaration
|
|
560
|
+
|
|
561
|
+
| Name | Type | Defined in |
|
|
562
|
+
| ------ | ------ | ------ |
|
|
563
|
+
| <a id="property-all"></a> `all()` | <`Rs`>(`results`) => `Result`<\{ \[K in string \| number \| symbol\]: OkOf\<Rs\[K\]\> \}, [`ErrOf`](#errof)<`Rs`\[`number`\]>> | [packages/core/src/facade.ts:39](https://github.com/btravstack/unthrown/blob/4553631adb8280ef7c869dfae5bdb13be762095c/packages/core/src/facade.ts#L39) |
|
|
564
|
+
| <a id="property-defect"></a> `defect()` | (`cause`) => [`Defect`](#defect) | [packages/core/src/facade.ts:34](https://github.com/btravstack/unthrown/blob/4553631adb8280ef7c869dfae5bdb13be762095c/packages/core/src/facade.ts#L34) |
|
|
565
|
+
| <a id="property-err"></a> `err()` | <`E`>(`error`) => `Result`<`never`, `E`> | [packages/core/src/facade.ts:33](https://github.com/btravstack/unthrown/blob/4553631adb8280ef7c869dfae5bdb13be762095c/packages/core/src/facade.ts#L33) |
|
|
566
|
+
| <a id="property-fromnullable"></a> `fromNullable()` | <`T`, `E`>(`value`, `onAbsent`) => `Result`<`NonNullable`<`T`>, `E`> | [packages/core/src/facade.ts:35](https://github.com/btravstack/unthrown/blob/4553631adb8280ef7c869dfae5bdb13be762095c/packages/core/src/facade.ts#L35) |
|
|
567
|
+
| <a id="property-frompromise"></a> `fromPromise()` | <`T`, `E`>(`promise`, `qualify`) => [`AsyncResult`](#asyncresult)<`T`, `E`> | [packages/core/src/facade.ts:37](https://github.com/btravstack/unthrown/blob/4553631adb8280ef7c869dfae5bdb13be762095c/packages/core/src/facade.ts#L37) |
|
|
568
|
+
| <a id="property-fromsafepromise"></a> `fromSafePromise()` | <`T`>(`promise`) => [`AsyncResult`](#asyncresult)<`T`, `never`> | [packages/core/src/facade.ts:38](https://github.com/btravstack/unthrown/blob/4553631adb8280ef7c869dfae5bdb13be762095c/packages/core/src/facade.ts#L38) |
|
|
569
|
+
| <a id="property-fromthrowable"></a> `fromThrowable()` | <`A`, `T`, `E`>(`fn`, `qualify`) => (...`args`) => `Result`<`T`, `E`> | [packages/core/src/facade.ts:36](https://github.com/btravstack/unthrown/blob/4553631adb8280ef7c869dfae5bdb13be762095c/packages/core/src/facade.ts#L36) |
|
|
570
|
+
| <a id="property-isdefect"></a> `isDefect()` | <`T`, `E`>(`r`) => `r is DefectView<T, E>` | [packages/core/src/facade.ts:42](https://github.com/btravstack/unthrown/blob/4553631adb8280ef7c869dfae5bdb13be762095c/packages/core/src/facade.ts#L42) |
|
|
571
|
+
| <a id="property-iserr"></a> `isErr()` | <`T`, `E`>(`r`) => `r is ErrView<E, T>` | [packages/core/src/facade.ts:41](https://github.com/btravstack/unthrown/blob/4553631adb8280ef7c869dfae5bdb13be762095c/packages/core/src/facade.ts#L41) |
|
|
572
|
+
| <a id="property-isok"></a> `isOk()` | <`T`, `E`>(`r`) => `r is OkView<T, E>` | [packages/core/src/facade.ts:40](https://github.com/btravstack/unthrown/blob/4553631adb8280ef7c869dfae5bdb13be762095c/packages/core/src/facade.ts#L40) |
|
|
573
|
+
| <a id="property-ok"></a> `ok()` | <`T`>(`value`) => `Result`<`T`, `never`> | [packages/core/src/facade.ts:32](https://github.com/btravstack/unthrown/blob/4553631adb8280ef7c869dfae5bdb13be762095c/packages/core/src/facade.ts#L32) |
|
|
574
|
+
|
|
575
|
+
#### Remarks
|
|
576
|
+
|
|
577
|
+
Purely additive sugar — each member **is** the corresponding free function.
|
|
578
|
+
The free functions remain the primary, tree-shakeable API; importing only
|
|
579
|
+
`{ ok }` never pulls this object in. The value `Result` and the type
|
|
580
|
+
[Result](#result-1) share one name (the companion-object pattern).
|
|
581
|
+
|
|
582
|
+
#### Example
|
|
583
|
+
|
|
584
|
+
```ts
|
|
585
|
+
import { Result } from "unthrown";
|
|
586
|
+
Result.ok(1).flatMap((n) => Result.ok(n + 1)).unwrap(); // 2
|
|
587
|
+
```
|
|
588
|
+
|
|
589
|
+
## Functions
|
|
590
|
+
|
|
591
|
+
### all()
|
|
592
|
+
|
|
593
|
+
```ts
|
|
594
|
+
function all<Rs>(results): Result<{ [K in string | number | symbol]: OkOf<Rs[K]> }, ErrOf<Rs[number]>>;
|
|
595
|
+
```
|
|
596
|
+
|
|
597
|
+
Defined in: [packages/core/src/interop.ts:162](https://github.com/btravstack/unthrown/blob/4553631adb8280ef7c869dfae5bdb13be762095c/packages/core/src/interop.ts#L162)
|
|
598
|
+
|
|
599
|
+
Collect a tuple of [Result](#result)s into a single `Result` of the tuple of
|
|
600
|
+
success values.
|
|
601
|
+
|
|
602
|
+
#### Type Parameters
|
|
603
|
+
|
|
604
|
+
| Type Parameter | Description |
|
|
605
|
+
| ------ | ------ |
|
|
606
|
+
| `Rs` *extends* readonly `Result`<`unknown`, `unknown`>[] | the tuple of input `Result` types. |
|
|
607
|
+
|
|
608
|
+
#### Parameters
|
|
609
|
+
|
|
610
|
+
| Parameter | Type | Description |
|
|
611
|
+
| ------ | ------ | ------ |
|
|
612
|
+
| `results` | readonly \[`Rs`\] | the results to combine. |
|
|
613
|
+
|
|
614
|
+
#### Returns
|
|
615
|
+
|
|
616
|
+
`Result`<\{ \[K in string \| number \| symbol\]: OkOf\<Rs\[K\]\> \}, [`ErrOf`](#errof)<`Rs`\[`number`\]>>
|
|
617
|
+
|
|
618
|
+
#### Remarks
|
|
619
|
+
|
|
620
|
+
Short-circuits on the **first** `Err` (later entries are not inspected for
|
|
621
|
+
their error); any `Defect` present **dominates**, winning even over an earlier
|
|
622
|
+
`Err`. Positional types are preserved, so `all([ok(1), ok("a")])` is
|
|
623
|
+
`Result<[number, string], …>`.
|
|
624
|
+
|
|
625
|
+
#### Example
|
|
626
|
+
|
|
627
|
+
```ts
|
|
628
|
+
import { all, ok } from "unthrown";
|
|
629
|
+
all([ok(1), ok("a"), ok(true)]).unwrap(); // [1, "a", true]
|
|
630
|
+
```
|
|
631
|
+
|
|
632
|
+
***
|
|
633
|
+
|
|
634
|
+
### defect()
|
|
635
|
+
|
|
636
|
+
```ts
|
|
637
|
+
function defect(cause): Defect;
|
|
638
|
+
```
|
|
639
|
+
|
|
640
|
+
Defined in: [packages/core/src/defect.ts:36](https://github.com/btravstack/unthrown/blob/4553631adb8280ef7c869dfae5bdb13be762095c/packages/core/src/defect.ts#L36)
|
|
641
|
+
|
|
642
|
+
Wrap a cause as a [Defect](#defect) — the value you return from a `qualify`
|
|
643
|
+
function when a failure is **not** a modeled domain error.
|
|
644
|
+
|
|
645
|
+
#### Parameters
|
|
646
|
+
|
|
647
|
+
| Parameter | Type | Description |
|
|
648
|
+
| ------ | ------ | ------ |
|
|
649
|
+
| `cause` | `unknown` | the original thrown/rejected value. |
|
|
650
|
+
|
|
651
|
+
#### Returns
|
|
652
|
+
|
|
653
|
+
[`Defect`](#defect)
|
|
654
|
+
|
|
655
|
+
an opaque defect marker carrying `cause`.
|
|
656
|
+
|
|
657
|
+
#### Example
|
|
658
|
+
|
|
659
|
+
```ts
|
|
660
|
+
import { fromPromise, defect } from "unthrown";
|
|
661
|
+
|
|
662
|
+
const user = fromPromise(fetchUser(id), (cause) =>
|
|
663
|
+
cause instanceof NotFoundError ? cause : defect(cause),
|
|
664
|
+
);
|
|
665
|
+
```
|
|
666
|
+
|
|
667
|
+
***
|
|
668
|
+
|
|
669
|
+
### err()
|
|
670
|
+
|
|
671
|
+
```ts
|
|
672
|
+
function err<E>(error): Result<never, E>;
|
|
673
|
+
```
|
|
674
|
+
|
|
675
|
+
Defined in: [packages/core/src/constructors.ts:34](https://github.com/btravstack/unthrown/blob/4553631adb8280ef7c869dfae5bdb13be762095c/packages/core/src/constructors.ts#L34)
|
|
676
|
+
|
|
677
|
+
Construct a failed [Result](#result) carrying a **modeled** error.
|
|
678
|
+
|
|
679
|
+
#### Type Parameters
|
|
680
|
+
|
|
681
|
+
| Type Parameter | Description |
|
|
682
|
+
| ------ | ------ |
|
|
683
|
+
| `E` | the modeled error type. |
|
|
684
|
+
|
|
685
|
+
#### Parameters
|
|
686
|
+
|
|
687
|
+
| Parameter | Type | Description |
|
|
688
|
+
| ------ | ------ | ------ |
|
|
689
|
+
| `error` | `E` | the domain error to wrap. |
|
|
690
|
+
|
|
691
|
+
#### Returns
|
|
692
|
+
|
|
693
|
+
`Result`<`never`, `E`>
|
|
694
|
+
|
|
695
|
+
#### Example
|
|
696
|
+
|
|
697
|
+
```ts
|
|
698
|
+
import { err } from "unthrown";
|
|
699
|
+
err("not_found").unwrapErr(); // "not_found"
|
|
700
|
+
```
|
|
701
|
+
|
|
702
|
+
***
|
|
703
|
+
|
|
704
|
+
### fromNullable()
|
|
705
|
+
|
|
706
|
+
```ts
|
|
707
|
+
function fromNullable<T, E>(value, onAbsent): Result<NonNullable<T>, E>;
|
|
708
|
+
```
|
|
709
|
+
|
|
710
|
+
Defined in: [packages/core/src/interop.ts:29](https://github.com/btravstack/unthrown/blob/4553631adb8280ef7c869dfae5bdb13be762095c/packages/core/src/interop.ts#L29)
|
|
711
|
+
|
|
712
|
+
Bridge a nullable value into a [Result](#result): absence becomes a **modeled**
|
|
713
|
+
`Err`. The sanctioned alternative to an `Option` type.
|
|
714
|
+
|
|
715
|
+
#### Type Parameters
|
|
716
|
+
|
|
717
|
+
| Type Parameter | Description |
|
|
718
|
+
| ------ | ------ |
|
|
719
|
+
| `T` | the (nullable) value type. |
|
|
720
|
+
| `E` | the error produced when the value is absent. |
|
|
721
|
+
|
|
722
|
+
#### Parameters
|
|
723
|
+
|
|
724
|
+
| Parameter | Type | Description |
|
|
725
|
+
| ------ | ------ | ------ |
|
|
726
|
+
| `value` | `T` \| `null` \| `undefined` | the possibly-absent value. |
|
|
727
|
+
| `onAbsent` | () => `E` | lazily produces the error for the absent case. |
|
|
728
|
+
|
|
729
|
+
#### Returns
|
|
730
|
+
|
|
731
|
+
`Result`<`NonNullable`<`T`>, `E`>
|
|
732
|
+
|
|
733
|
+
#### Remarks
|
|
734
|
+
|
|
735
|
+
`null` and `undefined` map to `err(onAbsent())`; any other value (including
|
|
736
|
+
falsy ones like `0`, `""`, `false`) maps to `Ok`.
|
|
737
|
+
|
|
738
|
+
#### Example
|
|
739
|
+
|
|
740
|
+
```ts
|
|
741
|
+
import { fromNullable } from "unthrown";
|
|
742
|
+
fromNullable(map.get(key), () => "missing").unwrap();
|
|
743
|
+
```
|
|
744
|
+
|
|
745
|
+
***
|
|
746
|
+
|
|
747
|
+
### fromPromise()
|
|
748
|
+
|
|
749
|
+
```ts
|
|
750
|
+
function fromPromise<T, E>(promise, qualify): AsyncResult<T, E>;
|
|
751
|
+
```
|
|
752
|
+
|
|
753
|
+
Defined in: [packages/core/src/interop.ts:95](https://github.com/btravstack/unthrown/blob/4553631adb8280ef7c869dfae5bdb13be762095c/packages/core/src/interop.ts#L95)
|
|
754
|
+
|
|
755
|
+
Wrap a `Promise` (or a thunk producing one) as an [AsyncResult](#asyncresult), forcing
|
|
756
|
+
every rejection to be triaged.
|
|
757
|
+
|
|
758
|
+
#### Type Parameters
|
|
759
|
+
|
|
760
|
+
| Type Parameter | Description |
|
|
761
|
+
| ------ | ------ |
|
|
762
|
+
| `T` | the resolved value type. |
|
|
763
|
+
| `E` | the modeled error type. |
|
|
764
|
+
|
|
765
|
+
#### Parameters
|
|
766
|
+
|
|
767
|
+
| Parameter | Type | Description |
|
|
768
|
+
| ------ | ------ | ------ |
|
|
769
|
+
| `promise` | `Promise`<`T`> \| (() => `Promise`<`T`>) | the promise, or a thunk returning one. |
|
|
770
|
+
| `qualify` | (`cause`) => [`Defect`](#defect) \| `E` | triages a rejection cause into `E` or a `Defect`. |
|
|
771
|
+
|
|
772
|
+
#### Returns
|
|
773
|
+
|
|
774
|
+
[`AsyncResult`](#asyncresult)<`T`, `E`>
|
|
775
|
+
|
|
776
|
+
#### Remarks
|
|
777
|
+
|
|
778
|
+
`qualify` **must** map each rejection cause into a modeled error `E` or a
|
|
779
|
+
[Defect](#defect). The returned `AsyncResult`'s internal promise never rejects;
|
|
780
|
+
`await`-ing it always yields a `Result`. A throw inside `qualify` is itself a
|
|
781
|
+
`Defect`.
|
|
782
|
+
|
|
783
|
+
#### Example
|
|
784
|
+
|
|
785
|
+
```ts
|
|
786
|
+
import { fromPromise, defect } from "unthrown";
|
|
787
|
+
const user = await fromPromise(fetchUser(id), (cause) =>
|
|
788
|
+
cause instanceof NotFoundError ? ("not_found" as const) : defect(cause),
|
|
789
|
+
);
|
|
790
|
+
```
|
|
791
|
+
|
|
792
|
+
***
|
|
793
|
+
|
|
794
|
+
### fromSafePromise()
|
|
795
|
+
|
|
796
|
+
```ts
|
|
797
|
+
function fromSafePromise<T>(promise): AsyncResult<T, never>;
|
|
798
|
+
```
|
|
799
|
+
|
|
800
|
+
Defined in: [packages/core/src/interop.ts:119](https://github.com/btravstack/unthrown/blob/4553631adb8280ef7c869dfae5bdb13be762095c/packages/core/src/interop.ts#L119)
|
|
801
|
+
|
|
802
|
+
Wrap a `Promise` asserted **not** to fail in any modeled way: any rejection
|
|
803
|
+
becomes a `Defect`.
|
|
804
|
+
|
|
805
|
+
#### Type Parameters
|
|
806
|
+
|
|
807
|
+
| Type Parameter | Description |
|
|
808
|
+
| ------ | ------ |
|
|
809
|
+
| `T` | the resolved value type. |
|
|
810
|
+
|
|
811
|
+
#### Parameters
|
|
812
|
+
|
|
813
|
+
| Parameter | Type | Description |
|
|
814
|
+
| ------ | ------ | ------ |
|
|
815
|
+
| `promise` | `Promise`<`T`> \| (() => `Promise`<`T`>) | the promise, or a thunk returning one. |
|
|
816
|
+
|
|
817
|
+
#### Returns
|
|
818
|
+
|
|
819
|
+
[`AsyncResult`](#asyncresult)<`T`, `never`>
|
|
820
|
+
|
|
821
|
+
#### Remarks
|
|
822
|
+
|
|
823
|
+
Use this only when a rejection genuinely indicates a bug rather than an
|
|
824
|
+
anticipated outcome — the error channel is `never`, so there is nothing to
|
|
825
|
+
triage. (`await`-ing still yields a `Result`; it never throws.)
|
|
826
|
+
|
|
827
|
+
***
|
|
828
|
+
|
|
829
|
+
### fromThrowable()
|
|
830
|
+
|
|
831
|
+
```ts
|
|
832
|
+
function fromThrowable<A, T, E>(fn, qualify): (...args) => Result<T, E>;
|
|
833
|
+
```
|
|
834
|
+
|
|
835
|
+
Defined in: [packages/core/src/interop.ts:59](https://github.com/btravstack/unthrown/blob/4553631adb8280ef7c869dfae5bdb13be762095c/packages/core/src/interop.ts#L59)
|
|
836
|
+
|
|
837
|
+
Wrap a throwing synchronous function so it returns a [Result](#result) instead of
|
|
838
|
+
throwing.
|
|
839
|
+
|
|
840
|
+
#### Type Parameters
|
|
841
|
+
|
|
842
|
+
| Type Parameter | Description |
|
|
843
|
+
| ------ | ------ |
|
|
844
|
+
| `A` *extends* `unknown`[] | the wrapped function's argument tuple. |
|
|
845
|
+
| `T` | the wrapped function's return type. |
|
|
846
|
+
| `E` | the modeled error type. |
|
|
847
|
+
|
|
848
|
+
#### Parameters
|
|
849
|
+
|
|
850
|
+
| Parameter | Type | Description |
|
|
851
|
+
| ------ | ------ | ------ |
|
|
852
|
+
| `fn` | (...`args`) => `T` | the throwing function to wrap. |
|
|
853
|
+
| `qualify` | (`cause`) => [`Defect`](#defect) \| `E` | triages a thrown cause into `E` or a `Defect`. |
|
|
854
|
+
|
|
855
|
+
#### Returns
|
|
856
|
+
|
|
857
|
+
a function with the same arguments returning `Result<T, E>`.
|
|
858
|
+
|
|
859
|
+
(...`args`) => `Result`<`T`, `E`>
|
|
860
|
+
|
|
861
|
+
#### Remarks
|
|
862
|
+
|
|
863
|
+
`qualify` **must** triage every thrown cause into a modeled error `E` or a
|
|
864
|
+
[Defect](#defect) (via [defect](#defect-2)) — there is no path that leaves `unknown`
|
|
865
|
+
in `E`. A throw inside `qualify` itself is treated as a `Defect`.
|
|
866
|
+
|
|
867
|
+
#### Example
|
|
868
|
+
|
|
869
|
+
```ts
|
|
870
|
+
import { fromThrowable, defect } from "unthrown";
|
|
871
|
+
const parse = fromThrowable(JSON.parse, (cause) => defect(cause));
|
|
872
|
+
parse("{}").unwrap();
|
|
873
|
+
```
|
|
874
|
+
|
|
875
|
+
***
|
|
876
|
+
|
|
877
|
+
### isDefect()
|
|
878
|
+
|
|
879
|
+
```ts
|
|
880
|
+
function isDefect<T, E>(r): r is DefectView<T, E>;
|
|
881
|
+
```
|
|
882
|
+
|
|
883
|
+
Defined in: [packages/core/src/constructors.ts:66](https://github.com/btravstack/unthrown/blob/4553631adb8280ef7c869dfae5bdb13be762095c/packages/core/src/constructors.ts#L66)
|
|
884
|
+
|
|
885
|
+
Type guard: narrow a [Result](#result) to its `Defect` variant, exposing `.cause`.
|
|
886
|
+
|
|
887
|
+
#### Type Parameters
|
|
888
|
+
|
|
889
|
+
| Type Parameter |
|
|
890
|
+
| ------ |
|
|
891
|
+
| `T` |
|
|
892
|
+
| `E` |
|
|
893
|
+
|
|
894
|
+
#### Parameters
|
|
895
|
+
|
|
896
|
+
| Parameter | Type |
|
|
897
|
+
| ------ | ------ |
|
|
898
|
+
| `r` | `Result`<`T`, `E`> |
|
|
899
|
+
|
|
900
|
+
#### Returns
|
|
901
|
+
|
|
902
|
+
`r is DefectView<T, E>`
|
|
903
|
+
|
|
904
|
+
`true` when `r` is a `Defect`.
|
|
905
|
+
|
|
906
|
+
***
|
|
907
|
+
|
|
908
|
+
### isErr()
|
|
909
|
+
|
|
910
|
+
```ts
|
|
911
|
+
function isErr<T, E>(r): r is ErrView<E, T>;
|
|
912
|
+
```
|
|
913
|
+
|
|
914
|
+
Defined in: [packages/core/src/constructors.ts:58](https://github.com/btravstack/unthrown/blob/4553631adb8280ef7c869dfae5bdb13be762095c/packages/core/src/constructors.ts#L58)
|
|
915
|
+
|
|
916
|
+
Type guard: narrow a [Result](#result) to its `Err` variant, exposing `.error`.
|
|
917
|
+
|
|
918
|
+
#### Type Parameters
|
|
919
|
+
|
|
920
|
+
| Type Parameter |
|
|
921
|
+
| ------ |
|
|
922
|
+
| `T` |
|
|
923
|
+
| `E` |
|
|
924
|
+
|
|
925
|
+
#### Parameters
|
|
926
|
+
|
|
927
|
+
| Parameter | Type |
|
|
928
|
+
| ------ | ------ |
|
|
929
|
+
| `r` | `Result`<`T`, `E`> |
|
|
930
|
+
|
|
931
|
+
#### Returns
|
|
932
|
+
|
|
933
|
+
`r is ErrView<E, T>`
|
|
934
|
+
|
|
935
|
+
`true` when `r` is `Err`.
|
|
936
|
+
|
|
937
|
+
***
|
|
938
|
+
|
|
939
|
+
### isOk()
|
|
940
|
+
|
|
941
|
+
```ts
|
|
942
|
+
function isOk<T, E>(r): r is OkView<T, E>;
|
|
943
|
+
```
|
|
944
|
+
|
|
945
|
+
Defined in: [packages/core/src/constructors.ts:50](https://github.com/btravstack/unthrown/blob/4553631adb8280ef7c869dfae5bdb13be762095c/packages/core/src/constructors.ts#L50)
|
|
946
|
+
|
|
947
|
+
Type guard: narrow a [Result](#result) to its `Ok` variant, exposing `.value`.
|
|
948
|
+
|
|
949
|
+
#### Type Parameters
|
|
950
|
+
|
|
951
|
+
| Type Parameter |
|
|
952
|
+
| ------ |
|
|
953
|
+
| `T` |
|
|
954
|
+
| `E` |
|
|
955
|
+
|
|
956
|
+
#### Parameters
|
|
957
|
+
|
|
958
|
+
| Parameter | Type |
|
|
959
|
+
| ------ | ------ |
|
|
960
|
+
| `r` | `Result`<`T`, `E`> |
|
|
961
|
+
|
|
962
|
+
#### Returns
|
|
963
|
+
|
|
964
|
+
`r is OkView<T, E>`
|
|
965
|
+
|
|
966
|
+
`true` when `r` is `Ok`.
|
|
967
|
+
|
|
968
|
+
#### Example
|
|
969
|
+
|
|
970
|
+
```ts
|
|
971
|
+
import { isOk, type Result } from "unthrown";
|
|
972
|
+
declare const r: Result<number, string>;
|
|
973
|
+
if (isOk(r)) r.value; // number, narrowed
|
|
974
|
+
```
|
|
975
|
+
|
|
976
|
+
***
|
|
977
|
+
|
|
978
|
+
### matchTags()
|
|
979
|
+
|
|
980
|
+
#### Call Signature
|
|
981
|
+
|
|
982
|
+
```ts
|
|
983
|
+
function matchTags<T, E, R>(result, handlers): R;
|
|
984
|
+
```
|
|
985
|
+
|
|
986
|
+
Defined in: [packages/core/src/tagged.ts:116](https://github.com/btravstack/unthrown/blob/4553631adb8280ef7c869dfae5bdb13be762095c/packages/core/src/tagged.ts#L116)
|
|
987
|
+
|
|
988
|
+
Exhaustively fold a [Result](#result) (or [AsyncResult](#asyncresult)) whose error type is
|
|
989
|
+
a tagged union, dispatching each error to the handler matching its `_tag`.
|
|
990
|
+
|
|
991
|
+
##### Type Parameters
|
|
992
|
+
|
|
993
|
+
| Type Parameter | Description |
|
|
994
|
+
| ------ | ------ |
|
|
995
|
+
| `T` | the success value type. |
|
|
996
|
+
| `E` *extends* `object` | the tagged error union (`E extends { _tag: string }`). |
|
|
997
|
+
| `R` | the folded result type. |
|
|
998
|
+
|
|
999
|
+
##### Parameters
|
|
1000
|
+
|
|
1001
|
+
| Parameter | Type | Description |
|
|
1002
|
+
| ------ | ------ | ------ |
|
|
1003
|
+
| `result` | `Result`<`T`, `E`> | the result to fold. |
|
|
1004
|
+
| `handlers` | [`TagHandlers`](#taghandlers)<`T`, `E`, `R`> | one branch per channel/tag. |
|
|
1005
|
+
|
|
1006
|
+
##### Returns
|
|
1007
|
+
|
|
1008
|
+
`R`
|
|
1009
|
+
|
|
1010
|
+
##### Remarks
|
|
1011
|
+
|
|
1012
|
+
The `handlers` object must provide `Ok`, `Defect`, and exactly one function
|
|
1013
|
+
per error tag; each tag's handler receives the narrowed error variant. A
|
|
1014
|
+
missing tag is a compile error. For an `AsyncResult`, the fold resolves to a
|
|
1015
|
+
`Promise<R>`.
|
|
1016
|
+
|
|
1017
|
+
##### Example
|
|
1018
|
+
|
|
1019
|
+
```ts
|
|
1020
|
+
class NotFound extends TaggedError("NotFound") {}
|
|
1021
|
+
class Forbidden extends TaggedError("Forbidden")<{ user: string }> {}
|
|
1022
|
+
|
|
1023
|
+
declare const r: Result<number, NotFound | Forbidden>;
|
|
1024
|
+
matchTags(r, {
|
|
1025
|
+
Ok: (n) => `got ${n}`,
|
|
1026
|
+
Defect: (cause) => `bug: ${String(cause)}`,
|
|
1027
|
+
NotFound: () => "404",
|
|
1028
|
+
Forbidden: (e) => `403 for ${e.user}`,
|
|
1029
|
+
});
|
|
1030
|
+
```
|
|
1031
|
+
|
|
1032
|
+
#### Call Signature
|
|
1033
|
+
|
|
1034
|
+
```ts
|
|
1035
|
+
function matchTags<T, E, R>(result, handlers): Promise<R>;
|
|
1036
|
+
```
|
|
1037
|
+
|
|
1038
|
+
Defined in: [packages/core/src/tagged.ts:120](https://github.com/btravstack/unthrown/blob/4553631adb8280ef7c869dfae5bdb13be762095c/packages/core/src/tagged.ts#L120)
|
|
1039
|
+
|
|
1040
|
+
Exhaustively fold a [Result](#result) (or [AsyncResult](#asyncresult)) whose error type is
|
|
1041
|
+
a tagged union, dispatching each error to the handler matching its `_tag`.
|
|
1042
|
+
|
|
1043
|
+
##### Type Parameters
|
|
1044
|
+
|
|
1045
|
+
| Type Parameter | Description |
|
|
1046
|
+
| ------ | ------ |
|
|
1047
|
+
| `T` | the success value type. |
|
|
1048
|
+
| `E` *extends* `object` | the tagged error union (`E extends { _tag: string }`). |
|
|
1049
|
+
| `R` | the folded result type. |
|
|
1050
|
+
|
|
1051
|
+
##### Parameters
|
|
1052
|
+
|
|
1053
|
+
| Parameter | Type | Description |
|
|
1054
|
+
| ------ | ------ | ------ |
|
|
1055
|
+
| `result` | [`AsyncResult`](#asyncresult)<`T`, `E`> | the result to fold. |
|
|
1056
|
+
| `handlers` | [`TagHandlers`](#taghandlers)<`T`, `E`, `R`> | one branch per channel/tag. |
|
|
1057
|
+
|
|
1058
|
+
##### Returns
|
|
1059
|
+
|
|
1060
|
+
`Promise`<`R`>
|
|
1061
|
+
|
|
1062
|
+
##### Remarks
|
|
1063
|
+
|
|
1064
|
+
The `handlers` object must provide `Ok`, `Defect`, and exactly one function
|
|
1065
|
+
per error tag; each tag's handler receives the narrowed error variant. A
|
|
1066
|
+
missing tag is a compile error. For an `AsyncResult`, the fold resolves to a
|
|
1067
|
+
`Promise<R>`.
|
|
1068
|
+
|
|
1069
|
+
##### Example
|
|
1070
|
+
|
|
1071
|
+
```ts
|
|
1072
|
+
class NotFound extends TaggedError("NotFound") {}
|
|
1073
|
+
class Forbidden extends TaggedError("Forbidden")<{ user: string }> {}
|
|
1074
|
+
|
|
1075
|
+
declare const r: Result<number, NotFound | Forbidden>;
|
|
1076
|
+
matchTags(r, {
|
|
1077
|
+
Ok: (n) => `got ${n}`,
|
|
1078
|
+
Defect: (cause) => `bug: ${String(cause)}`,
|
|
1079
|
+
NotFound: () => "404",
|
|
1080
|
+
Forbidden: (e) => `403 for ${e.user}`,
|
|
1081
|
+
});
|
|
1082
|
+
```
|
|
1083
|
+
|
|
1084
|
+
***
|
|
1085
|
+
|
|
1086
|
+
### ok()
|
|
1087
|
+
|
|
1088
|
+
```ts
|
|
1089
|
+
function ok<T>(value): Result<T, never>;
|
|
1090
|
+
```
|
|
1091
|
+
|
|
1092
|
+
Defined in: [packages/core/src/constructors.ts:18](https://github.com/btravstack/unthrown/blob/4553631adb8280ef7c869dfae5bdb13be762095c/packages/core/src/constructors.ts#L18)
|
|
1093
|
+
|
|
1094
|
+
Construct a successful [Result](#result).
|
|
1095
|
+
|
|
1096
|
+
#### Type Parameters
|
|
1097
|
+
|
|
1098
|
+
| Type Parameter | Description |
|
|
1099
|
+
| ------ | ------ |
|
|
1100
|
+
| `T` | the success value type. |
|
|
1101
|
+
|
|
1102
|
+
#### Parameters
|
|
1103
|
+
|
|
1104
|
+
| Parameter | Type | Description |
|
|
1105
|
+
| ------ | ------ | ------ |
|
|
1106
|
+
| `value` | `T` | the success value to wrap. |
|
|
1107
|
+
|
|
1108
|
+
#### Returns
|
|
1109
|
+
|
|
1110
|
+
`Result`<`T`, `never`>
|
|
1111
|
+
|
|
1112
|
+
#### Example
|
|
1113
|
+
|
|
1114
|
+
```ts
|
|
1115
|
+
import { ok } from "unthrown";
|
|
1116
|
+
ok(42).unwrap(); // 42
|
|
1117
|
+
```
|
|
1118
|
+
|
|
1119
|
+
***
|
|
1120
|
+
|
|
1121
|
+
### TaggedError()
|
|
1122
|
+
|
|
1123
|
+
```ts
|
|
1124
|
+
function TaggedError<Tag>(tag): TaggedErrorConstructor<Tag>;
|
|
1125
|
+
```
|
|
1126
|
+
|
|
1127
|
+
Defined in: [packages/core/src/tagged.ts:54](https://github.com/btravstack/unthrown/blob/4553631adb8280ef7c869dfae5bdb13be762095c/packages/core/src/tagged.ts#L54)
|
|
1128
|
+
|
|
1129
|
+
Build a base class for a tagged error — a class extending `Error` with a
|
|
1130
|
+
`_tag` string discriminant, in the style of Effect's `Data.TaggedError`.
|
|
1131
|
+
|
|
1132
|
+
#### Type Parameters
|
|
1133
|
+
|
|
1134
|
+
| Type Parameter | Description |
|
|
1135
|
+
| ------ | ------ |
|
|
1136
|
+
| `Tag` *extends* `string` | the string literal discriminant. |
|
|
1137
|
+
|
|
1138
|
+
#### Parameters
|
|
1139
|
+
|
|
1140
|
+
| Parameter | Type | Description |
|
|
1141
|
+
| ------ | ------ | ------ |
|
|
1142
|
+
| `tag` | `Tag` | the discriminant value, also used as the error `name`. |
|
|
1143
|
+
|
|
1144
|
+
#### Returns
|
|
1145
|
+
|
|
1146
|
+
[`TaggedErrorConstructor`](#taggederrorconstructor)<`Tag`>
|
|
1147
|
+
|
|
1148
|
+
#### Remarks
|
|
1149
|
+
|
|
1150
|
+
Extend the returned class to declare a concrete error. Supply the payload with
|
|
1151
|
+
an instantiation expression; omit it for a payload-less error. A `message`
|
|
1152
|
+
field in the payload is forwarded to `Error`. The `_tag` always reflects
|
|
1153
|
+
`tag` and cannot be overridden by the payload.
|
|
1154
|
+
|
|
1155
|
+
#### Example
|
|
1156
|
+
|
|
1157
|
+
```ts
|
|
1158
|
+
class NotFound extends TaggedError("NotFound") {}
|
|
1159
|
+
class HttpError extends TaggedError("HttpError")<{ status: number }> {}
|
|
1160
|
+
|
|
1161
|
+
new NotFound()._tag; // "NotFound"
|
|
1162
|
+
new HttpError({ status: 500 }).status; // 500
|
|
1163
|
+
```
|