go-go-try 7.3.0 → 7.4.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 +129 -22
- package/dist/index.cjs +63 -17
- package/dist/index.d.cts +96 -22
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +96 -22
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +63 -18
- package/package.json +3 -3
- package/src/goTryAll.ts +55 -11
- package/src/goTryRaw.ts +64 -27
- package/src/index.test.ts +320 -26
- package/src/index.ts +5 -0
- package/src/types.ts +24 -0
- package/src/unknown-error.ts +20 -0
package/README.md
CHANGED
|
@@ -46,10 +46,10 @@ const [err, todos = []] = await goTry(fetchTodos()) // err is string | undefined
|
|
|
46
46
|
if (err) sendToErrorTrackingService(err)
|
|
47
47
|
|
|
48
48
|
// goTry extracts the error message from the error object, if you want the raw error object, use goTryRaw
|
|
49
|
-
const [err, value] = goTryRaw(() => JSON.parse('{/}')) // err is
|
|
49
|
+
const [err, value] = goTryRaw(() => JSON.parse('{/}')) // err is UnknownError | undefined, value is T | undefined
|
|
50
50
|
|
|
51
51
|
// fetch todos, fallback to empty array, send error to your error tracking service
|
|
52
|
-
const [err, todos = []] = await goTryRaw(fetchTodos()) // err is
|
|
52
|
+
const [err, todos = []] = await goTryRaw(fetchTodos()) // err is UnknownError | undefined
|
|
53
53
|
if (err) sendToErrorTrackingService(err)
|
|
54
54
|
```
|
|
55
55
|
|
|
@@ -233,10 +233,10 @@ type AppErrorVerbose =
|
|
|
233
233
|
|
|
234
234
|
// Use in functions with typed error returns
|
|
235
235
|
async function fetchUser(id: string): Promise<Result<AppError, User>> {
|
|
236
|
-
const [dbErr, user] = await goTryRaw(queryDatabase(id), DatabaseError)
|
|
236
|
+
const [dbErr, user] = await goTryRaw(queryDatabase(id), { errorClass: DatabaseError })
|
|
237
237
|
if (dbErr) return failure(dbErr)
|
|
238
238
|
|
|
239
|
-
const [netErr, enriched] = await goTryRaw(enrichUserData(user!), NetworkError)
|
|
239
|
+
const [netErr, enriched] = await goTryRaw(enrichUserData(user!), { errorClass: NetworkError })
|
|
240
240
|
if (netErr) return failure(netErr)
|
|
241
241
|
|
|
242
242
|
return [undefined, enriched] as const
|
|
@@ -318,32 +318,51 @@ Executes a function, promise, or value and returns a Result type with error mess
|
|
|
318
318
|
function goTry<T>(value: T | Promise<T> | (() => T | Promise<T>)): Result<string, T> | Promise<Result<string, T>>
|
|
319
319
|
```
|
|
320
320
|
|
|
321
|
-
### `goTryRaw<T, E>(value,
|
|
321
|
+
### `goTryRaw<T, E>(value, options?)`
|
|
322
322
|
|
|
323
323
|
Like `goTry` but returns the raw Error object instead of just the message.
|
|
324
324
|
|
|
325
|
-
|
|
325
|
+
By default, errors are wrapped in `UnknownError` (a tagged error class). You can customize this behavior with the options object:
|
|
326
326
|
|
|
327
327
|
```ts
|
|
328
|
-
|
|
329
|
-
|
|
328
|
+
type GoTryRawOptions<E> =
|
|
329
|
+
| { errorClass: ErrorConstructor<E> } // Wrap ALL errors in this class
|
|
330
|
+
| { systemErrorClass: ErrorConstructor<E> } // Only wrap non-tagged errors
|
|
331
|
+
| {} // Use defaults
|
|
332
|
+
```
|
|
333
|
+
|
|
334
|
+
> **Note:** `errorClass` and `systemErrorClass` are **mutually exclusive**. TypeScript will show an error if you try to pass both.
|
|
335
|
+
> - Use `errorClass` when you want all errors wrapped in a specific type
|
|
336
|
+
> - Use `systemErrorClass` when you want tagged errors to pass through and only wrap untagged errors
|
|
337
|
+
|
|
338
|
+
```ts
|
|
339
|
+
// Without options - err is UnknownError | undefined
|
|
340
|
+
function goTryRaw<T>(value: T | Promise<T> | (() => T | Promise<T>)): Result<UnknownError, T> | Promise<Result<UnknownError, T>>
|
|
330
341
|
|
|
331
|
-
// With
|
|
332
|
-
function goTryRaw<T, E>(value: T | Promise<T> | (() => T | Promise<T>),
|
|
342
|
+
// With options object - err is E | undefined
|
|
343
|
+
function goTryRaw<T, E>(value: T | Promise<T> | (() => T | Promise<T>), options: GoTryRawOptions<E>): Result<E, T> | Promise<Result<E, T>>
|
|
333
344
|
```
|
|
334
345
|
|
|
335
|
-
**
|
|
346
|
+
**Examples:**
|
|
347
|
+
|
|
336
348
|
```ts
|
|
337
349
|
const DatabaseError = taggedError('DatabaseError')
|
|
350
|
+
const NetworkError = taggedError('NetworkError')
|
|
338
351
|
|
|
339
|
-
//
|
|
352
|
+
// Default - errors wrapped in UnknownError
|
|
340
353
|
const [err1, data1] = await goTryRaw(fetchData())
|
|
341
|
-
// err1 is
|
|
354
|
+
// err1 is UnknownError | undefined
|
|
355
|
+
// err1?._tag === 'UnknownError'
|
|
342
356
|
|
|
343
|
-
//
|
|
344
|
-
const [err2, data2] = await goTryRaw(fetchData(), DatabaseError)
|
|
357
|
+
// Options object - wrap all errors
|
|
358
|
+
const [err2, data2] = await goTryRaw(fetchData(), { errorClass: DatabaseError })
|
|
345
359
|
// err2 is DatabaseError | undefined
|
|
346
|
-
// err2
|
|
360
|
+
// err2?._tag === 'DatabaseError'
|
|
361
|
+
|
|
362
|
+
// Options object - systemErrorClass only wraps non-tagged errors
|
|
363
|
+
const [err3, data3] = await goTryRaw(fetchData(), { systemErrorClass: NetworkError })
|
|
364
|
+
// If fetchData throws a tagged error (e.g., DatabaseError), it passes through
|
|
365
|
+
// If fetchData throws a plain Error, it gets wrapped in NetworkError
|
|
347
366
|
```
|
|
348
367
|
|
|
349
368
|
### `goTryAll<T>(items, options?)`
|
|
@@ -389,15 +408,50 @@ const [errors, results] = await goTryAll([
|
|
|
389
408
|
// - Limit expensive computation resources
|
|
390
409
|
```
|
|
391
410
|
|
|
392
|
-
### `goTryAllRaw<T>(items, options?)`
|
|
411
|
+
### `goTryAllRaw<T, E>(items, options?)`
|
|
393
412
|
|
|
394
413
|
Like `goTryAll`, but returns raw Error objects instead of error messages.
|
|
414
|
+
Non-tagged errors are wrapped in `UnknownError` by default (consistent with `goTryRaw`).
|
|
415
|
+
Tagged errors pass through unchanged.
|
|
416
|
+
|
|
417
|
+
Supports the same `errorClass` and `systemErrorClass` options as `goTryRaw`, plus `concurrency` control.
|
|
395
418
|
|
|
396
419
|
```ts
|
|
397
|
-
|
|
420
|
+
interface GoTryAllRawOptions<E> {
|
|
421
|
+
concurrency?: number
|
|
422
|
+
// errorClass and systemErrorClass are mutually exclusive
|
|
423
|
+
errorClass?: ErrorConstructor<E> // Wrap ALL errors
|
|
424
|
+
systemErrorClass?: ErrorConstructor<E> // Only wrap non-tagged errors
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
function goTryAllRaw<T extends readonly unknown[], E = UnknownError>(
|
|
398
428
|
items: { [K in keyof T]: Promise<T[K]> | (() => Promise<T[K]>) },
|
|
399
|
-
options?:
|
|
400
|
-
): Promise<[{ [K in keyof T]:
|
|
429
|
+
options?: GoTryAllRawOptions<E>
|
|
430
|
+
): Promise<[{ [K in keyof T]: E | undefined }, { [K in keyof T]: T[K] | undefined }]>
|
|
431
|
+
```
|
|
432
|
+
|
|
433
|
+
**Example:**
|
|
434
|
+
```ts
|
|
435
|
+
const DatabaseError = taggedError('DatabaseError')
|
|
436
|
+
|
|
437
|
+
// Default behavior - non-tagged errors wrapped in UnknownError
|
|
438
|
+
const [errors1] = await goTryAllRaw([
|
|
439
|
+
fetchUser(1),
|
|
440
|
+
fetchUser(2),
|
|
441
|
+
])
|
|
442
|
+
|
|
443
|
+
// Wrap all errors in DatabaseError
|
|
444
|
+
const [errors2] = await goTryAllRaw([
|
|
445
|
+
fetchUser(1),
|
|
446
|
+
fetchUser(2),
|
|
447
|
+
], { errorClass: DatabaseError })
|
|
448
|
+
|
|
449
|
+
// Combine concurrency control with errorClass
|
|
450
|
+
const [errors3] = await goTryAllRaw([
|
|
451
|
+
() => fetchUser(1),
|
|
452
|
+
() => fetchUser(2),
|
|
453
|
+
() => fetchUser(3),
|
|
454
|
+
], { concurrency: 2, errorClass: DatabaseError })
|
|
401
455
|
```
|
|
402
456
|
|
|
403
457
|
### `goTryOr<T>(value, defaultValue)`
|
|
@@ -453,6 +507,47 @@ console.log(err.name) // 'DatabaseError'
|
|
|
453
507
|
console.log(err.cause) // originalError
|
|
454
508
|
```
|
|
455
509
|
|
|
510
|
+
### `UnknownError`
|
|
511
|
+
|
|
512
|
+
A default tagged error class used by `goTryRaw` when no options are provided, or when `systemErrorClass` is not specified in the options object.
|
|
513
|
+
|
|
514
|
+
```ts
|
|
515
|
+
import { UnknownError, goTryRaw } from 'go-go-try'
|
|
516
|
+
|
|
517
|
+
// Errors are automatically wrapped in UnknownError
|
|
518
|
+
const [err, data] = goTryRaw(() => {
|
|
519
|
+
throw new Error('something went wrong')
|
|
520
|
+
})
|
|
521
|
+
|
|
522
|
+
if (err) {
|
|
523
|
+
console.log(err._tag) // 'UnknownError'
|
|
524
|
+
console.log(err.message) // 'something went wrong'
|
|
525
|
+
console.log(err.cause) // original Error
|
|
526
|
+
}
|
|
527
|
+
```
|
|
528
|
+
|
|
529
|
+
Since `UnknownError` is the default for `systemErrorClass`, you can distinguish between known tagged errors and unexpected system errors without specifying any options:
|
|
530
|
+
|
|
531
|
+
```ts
|
|
532
|
+
const DatabaseError = taggedError('DatabaseError')
|
|
533
|
+
|
|
534
|
+
function fetchData() {
|
|
535
|
+
// Operations that might throw DatabaseError should use errorClass to wrap them
|
|
536
|
+
const [err1, data1] = goTryRaw(() => queryDatabase(), { errorClass: DatabaseError })
|
|
537
|
+
|
|
538
|
+
// Other operations use the default behavior - non-tagged errors become UnknownError
|
|
539
|
+
const [err2, data2] = goTryRaw(() => parseData(data1))
|
|
540
|
+
// err2 is UnknownError | undefined
|
|
541
|
+
|
|
542
|
+
// Now you can distinguish between known and unknown error types
|
|
543
|
+
if (err1) {
|
|
544
|
+
console.log('Known DB error:', err1.message)
|
|
545
|
+
} else if (err2) {
|
|
546
|
+
console.log('Unexpected error:', err2.message)
|
|
547
|
+
}
|
|
548
|
+
}
|
|
549
|
+
```
|
|
550
|
+
|
|
456
551
|
### `TaggedUnion<T>`
|
|
457
552
|
|
|
458
553
|
Creates a union type from multiple tagged error classes.
|
|
@@ -487,11 +582,11 @@ When using `goTryRaw` with different error classes in the same function, TypeScr
|
|
|
487
582
|
// No explicit return type needed!
|
|
488
583
|
async function fetchUserData(id: string) {
|
|
489
584
|
// First operation might fail with DatabaseError
|
|
490
|
-
const [dbErr, user] = await goTryRaw(queryDb(id), DatabaseError)
|
|
585
|
+
const [dbErr, user] = await goTryRaw(queryDb(id), { errorClass: DatabaseError })
|
|
491
586
|
if (dbErr) return failure(dbErr) // returns Failure<DatabaseError>
|
|
492
587
|
|
|
493
588
|
// Second operation might fail with NetworkError
|
|
494
|
-
const [netErr, enriched] = await goTryRaw(enrichUser(user!), NetworkError)
|
|
589
|
+
const [netErr, enriched] = await goTryRaw(enrichUser(user!), { errorClass: NetworkError })
|
|
495
590
|
if (netErr) return failure(netErr) // returns Failure<NetworkError>
|
|
496
591
|
|
|
497
592
|
return success(enriched) // returns Success<User>
|
|
@@ -525,6 +620,18 @@ type Result<E, T> = Success<T> | Failure<E>
|
|
|
525
620
|
type TaggedInstance<T> = T extends ErrorConstructor<infer E> ? E : never
|
|
526
621
|
type TaggedUnion<T extends readonly ErrorConstructor<unknown>[]> =
|
|
527
622
|
{ [K in keyof T]: T[K] extends ErrorConstructor<infer E> ? E : never }[number]
|
|
623
|
+
|
|
624
|
+
// Options for goTryRaw (errorClass and systemErrorClass are mutually exclusive)
|
|
625
|
+
type GoTryRawOptions<E> =
|
|
626
|
+
| { errorClass: ErrorConstructor<E>; systemErrorClass?: never }
|
|
627
|
+
| { errorClass?: never; systemErrorClass: ErrorConstructor<E> }
|
|
628
|
+
| { errorClass?: never; systemErrorClass?: never }
|
|
629
|
+
|
|
630
|
+
// Options for goTryAllRaw (includes concurrency + error class options)
|
|
631
|
+
type GoTryAllRawOptions<E> =
|
|
632
|
+
| { concurrency?: number; errorClass: ErrorConstructor<E>; systemErrorClass?: never }
|
|
633
|
+
| { concurrency?: number; errorClass?: never; systemErrorClass: ErrorConstructor<E> }
|
|
634
|
+
| { concurrency?: number; errorClass?: never; systemErrorClass?: never }
|
|
528
635
|
```
|
|
529
636
|
|
|
530
637
|
## License
|
package/dist/index.cjs
CHANGED
|
@@ -50,16 +50,46 @@ function goTry(value) {
|
|
|
50
50
|
}
|
|
51
51
|
}
|
|
52
52
|
|
|
53
|
-
function
|
|
53
|
+
function taggedError(tag) {
|
|
54
|
+
return class TaggedErrorClass extends Error {
|
|
55
|
+
constructor(message, options) {
|
|
56
|
+
super(message);
|
|
57
|
+
this._tag = tag;
|
|
58
|
+
this.name = tag;
|
|
59
|
+
this.cause = options?.cause;
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const UnknownError = taggedError("UnknownError");
|
|
65
|
+
|
|
66
|
+
function isTaggedError$1(err) {
|
|
67
|
+
return isError(err) && "_tag" in err && typeof err._tag === "string";
|
|
68
|
+
}
|
|
69
|
+
function goTryRaw(value, options) {
|
|
70
|
+
const { errorClass, systemErrorClass } = options || {};
|
|
71
|
+
const actualSystemErrorClass = systemErrorClass ?? UnknownError;
|
|
54
72
|
const wrapError = (err) => {
|
|
55
|
-
if (
|
|
73
|
+
if (errorClass) {
|
|
74
|
+
if (err === void 0) {
|
|
75
|
+
return new errorClass("undefined");
|
|
76
|
+
}
|
|
77
|
+
if (isError(err)) {
|
|
78
|
+
return new errorClass(err.message, { cause: err });
|
|
79
|
+
}
|
|
80
|
+
return new errorClass(String(err));
|
|
81
|
+
}
|
|
82
|
+
if (actualSystemErrorClass) {
|
|
83
|
+
if (isTaggedError$1(err)) {
|
|
84
|
+
return err;
|
|
85
|
+
}
|
|
56
86
|
if (err === void 0) {
|
|
57
|
-
return new
|
|
87
|
+
return new actualSystemErrorClass("undefined");
|
|
58
88
|
}
|
|
59
89
|
if (isError(err)) {
|
|
60
|
-
return new
|
|
90
|
+
return new actualSystemErrorClass(err.message, { cause: err });
|
|
61
91
|
}
|
|
62
|
-
return new
|
|
92
|
+
return new actualSystemErrorClass(String(err));
|
|
63
93
|
}
|
|
64
94
|
if (err === void 0) {
|
|
65
95
|
return new Error("undefined");
|
|
@@ -89,6 +119,31 @@ function goTryOr(value, defaultValue) {
|
|
|
89
119
|
}
|
|
90
120
|
}
|
|
91
121
|
|
|
122
|
+
function isTaggedError(err) {
|
|
123
|
+
return isError(err) && "_tag" in err && typeof err._tag === "string";
|
|
124
|
+
}
|
|
125
|
+
function wrapError(err, errorClass, systemErrorClass) {
|
|
126
|
+
if (errorClass) {
|
|
127
|
+
if (err === void 0) {
|
|
128
|
+
return new errorClass("undefined");
|
|
129
|
+
}
|
|
130
|
+
if (isError(err)) {
|
|
131
|
+
return new errorClass(err.message, { cause: err });
|
|
132
|
+
}
|
|
133
|
+
return new errorClass(String(err));
|
|
134
|
+
}
|
|
135
|
+
const actualSystemErrorClass = systemErrorClass ?? UnknownError;
|
|
136
|
+
if (isTaggedError(err)) {
|
|
137
|
+
return err;
|
|
138
|
+
}
|
|
139
|
+
if (err === void 0) {
|
|
140
|
+
return new actualSystemErrorClass("undefined");
|
|
141
|
+
}
|
|
142
|
+
if (isError(err)) {
|
|
143
|
+
return new actualSystemErrorClass(err.message, { cause: err });
|
|
144
|
+
}
|
|
145
|
+
return new actualSystemErrorClass(String(err));
|
|
146
|
+
}
|
|
92
147
|
async function runWithConcurrency(items, concurrency) {
|
|
93
148
|
if (items.length === 0) {
|
|
94
149
|
return [];
|
|
@@ -136,6 +191,7 @@ async function goTryAll(items, options) {
|
|
|
136
191
|
return [errors, results];
|
|
137
192
|
}
|
|
138
193
|
async function goTryAllRaw(items, options) {
|
|
194
|
+
const { errorClass, systemErrorClass } = options || {};
|
|
139
195
|
const settled = await runWithConcurrency(items, options?.concurrency ?? 0);
|
|
140
196
|
const errors = [];
|
|
141
197
|
const results = [];
|
|
@@ -146,24 +202,13 @@ async function goTryAllRaw(items, options) {
|
|
|
146
202
|
results[i] = item.value;
|
|
147
203
|
} else {
|
|
148
204
|
const reason = item.reason;
|
|
149
|
-
errors[i] =
|
|
205
|
+
errors[i] = wrapError(reason, errorClass, systemErrorClass);
|
|
150
206
|
results[i] = void 0;
|
|
151
207
|
}
|
|
152
208
|
}
|
|
153
209
|
return [errors, results];
|
|
154
210
|
}
|
|
155
211
|
|
|
156
|
-
function taggedError(tag) {
|
|
157
|
-
return class TaggedErrorClass extends Error {
|
|
158
|
-
constructor(message, options) {
|
|
159
|
-
super(message);
|
|
160
|
-
this._tag = tag;
|
|
161
|
-
this.name = tag;
|
|
162
|
-
this.cause = options?.cause;
|
|
163
|
-
}
|
|
164
|
-
};
|
|
165
|
-
}
|
|
166
|
-
|
|
167
212
|
function assert(condition, errorOrClass, message) {
|
|
168
213
|
if (!condition) {
|
|
169
214
|
if (typeof errorOrClass === "string") {
|
|
@@ -176,6 +221,7 @@ function assert(condition, errorOrClass, message) {
|
|
|
176
221
|
}
|
|
177
222
|
}
|
|
178
223
|
|
|
224
|
+
exports.UnknownError = UnknownError;
|
|
179
225
|
exports.assert = assert;
|
|
180
226
|
exports.assertNever = assertNever;
|
|
181
227
|
exports.failure = failure;
|
package/dist/index.d.cts
CHANGED
|
@@ -28,6 +28,38 @@ interface GoTryAllOptions {
|
|
|
28
28
|
type ErrorConstructor<E> = new (message: string, options?: {
|
|
29
29
|
cause?: unknown;
|
|
30
30
|
}) => E;
|
|
31
|
+
/**
|
|
32
|
+
* Options for goTryRaw function.
|
|
33
|
+
* errorClass and systemErrorClass are mutually exclusive - you can only provide one.
|
|
34
|
+
*/
|
|
35
|
+
type GoTryRawOptions<E = Error> = {
|
|
36
|
+
errorClass: ErrorConstructor<E>;
|
|
37
|
+
systemErrorClass?: never;
|
|
38
|
+
} | {
|
|
39
|
+
errorClass?: never;
|
|
40
|
+
systemErrorClass: ErrorConstructor<E>;
|
|
41
|
+
} | {
|
|
42
|
+
errorClass?: never;
|
|
43
|
+
systemErrorClass?: never;
|
|
44
|
+
};
|
|
45
|
+
/**
|
|
46
|
+
* Options for goTryAllRaw function.
|
|
47
|
+
* Includes concurrency control and error class options.
|
|
48
|
+
* errorClass and systemErrorClass are mutually exclusive.
|
|
49
|
+
*/
|
|
50
|
+
type GoTryAllRawOptions<E = Error> = {
|
|
51
|
+
concurrency?: number;
|
|
52
|
+
errorClass: ErrorConstructor<E>;
|
|
53
|
+
systemErrorClass?: never;
|
|
54
|
+
} | {
|
|
55
|
+
concurrency?: number;
|
|
56
|
+
errorClass?: never;
|
|
57
|
+
systemErrorClass: ErrorConstructor<E>;
|
|
58
|
+
} | {
|
|
59
|
+
concurrency?: number;
|
|
60
|
+
errorClass?: never;
|
|
61
|
+
systemErrorClass?: never;
|
|
62
|
+
};
|
|
31
63
|
/**
|
|
32
64
|
* Creates a union type from multiple tagged error classes.
|
|
33
65
|
*
|
|
@@ -71,14 +103,44 @@ declare function goTry<T>(promise: Promise<T>): Promise<Result<string, T>>;
|
|
|
71
103
|
declare function goTry<T>(fn: () => T): Result<string, T>;
|
|
72
104
|
declare function goTry<T>(value: T): Result<string, T>;
|
|
73
105
|
|
|
106
|
+
/**
|
|
107
|
+
* Default system error class for errors that aren't already wrapped in a tagged error class.
|
|
108
|
+
*
|
|
109
|
+
* @example
|
|
110
|
+
* // By default, goTryRaw wraps unknown errors in UnknownError
|
|
111
|
+
* const [err, result] = goTryRaw(() => mightThrow())
|
|
112
|
+
* if (err) {
|
|
113
|
+
* console.log(err._tag) // 'UnknownError'
|
|
114
|
+
* }
|
|
115
|
+
*
|
|
116
|
+
* @example
|
|
117
|
+
* // Use a custom system error class
|
|
118
|
+
* const SystemError = taggedError('SystemError')
|
|
119
|
+
* const [err, result] = goTryRaw(() => mightThrow(), {
|
|
120
|
+
* systemErrorClass: SystemError
|
|
121
|
+
* })
|
|
122
|
+
*/
|
|
123
|
+
declare const UnknownError: {
|
|
124
|
+
new (message: string, options?: {
|
|
125
|
+
cause?: unknown;
|
|
126
|
+
} | undefined): {
|
|
127
|
+
readonly _tag: "UnknownError";
|
|
128
|
+
readonly cause?: unknown;
|
|
129
|
+
name: string;
|
|
130
|
+
message: string;
|
|
131
|
+
stack?: string;
|
|
132
|
+
};
|
|
133
|
+
isError(error: unknown): error is Error;
|
|
134
|
+
};
|
|
135
|
+
|
|
74
136
|
/**
|
|
75
137
|
* Executes a function, promise, or value and returns a Result type.
|
|
76
138
|
* If an error occurs, it returns a Failure with the raw error object.
|
|
77
139
|
*
|
|
78
140
|
* @template T The type of the successful result
|
|
79
|
-
* @template E The type of the error
|
|
141
|
+
* @template E The type of the error
|
|
80
142
|
* @param {T | Promise<T> | (() => T | Promise<T>)} value - The value, promise, or function to execute
|
|
81
|
-
* @param {
|
|
143
|
+
* @param {GoTryRawOptions<E>} [options] - Optional options object
|
|
82
144
|
* @returns {Result<E, T> | Promise<Result<E, T>>} A Result type or a Promise of a Result type
|
|
83
145
|
*
|
|
84
146
|
* @example
|
|
@@ -94,21 +156,26 @@ declare function goTry<T>(value: T): Result<string, T>;
|
|
|
94
156
|
* const [err, result] = await goTryRaw(fetch('https://api.example.com/data'));
|
|
95
157
|
*
|
|
96
158
|
* @example
|
|
97
|
-
* // With
|
|
159
|
+
* // With options object - wrap all errors
|
|
98
160
|
* const DatabaseError = taggedError('DatabaseError');
|
|
99
|
-
* const [err, result] = await goTryRaw(fetchData(), DatabaseError);
|
|
100
|
-
*
|
|
161
|
+
* const [err, result] = await goTryRaw(fetchData(), { errorClass: DatabaseError });
|
|
162
|
+
*
|
|
163
|
+
* @example
|
|
164
|
+
* // With options object - systemErrorClass only wraps non-tagged errors
|
|
165
|
+
* const [err, result] = await goTryRaw(fetchData(), { systemErrorClass: UnknownError });
|
|
166
|
+
* // Errors thrown as tagged errors pass through
|
|
167
|
+
* // Other errors are wrapped in UnknownError
|
|
101
168
|
*/
|
|
102
|
-
declare function goTryRaw<T
|
|
103
|
-
declare function goTryRaw<T, E =
|
|
104
|
-
declare function goTryRaw<T
|
|
105
|
-
declare function goTryRaw<T, E =
|
|
106
|
-
declare function goTryRaw<T
|
|
107
|
-
declare function goTryRaw<T, E =
|
|
108
|
-
declare function goTryRaw<T
|
|
109
|
-
declare function goTryRaw<T, E =
|
|
110
|
-
declare function goTryRaw<T
|
|
111
|
-
declare function goTryRaw<T, E =
|
|
169
|
+
declare function goTryRaw<T>(fn: () => never): Result<Error, never>;
|
|
170
|
+
declare function goTryRaw<T, E = InstanceType<typeof UnknownError>>(fn: () => never, options: GoTryRawOptions<E>): Result<E, never>;
|
|
171
|
+
declare function goTryRaw<T>(fn: () => Promise<T>): Promise<Result<Error, T>>;
|
|
172
|
+
declare function goTryRaw<T, E = InstanceType<typeof UnknownError>>(fn: () => Promise<T>, options: GoTryRawOptions<E>): Promise<Result<E, T>>;
|
|
173
|
+
declare function goTryRaw<T>(promise: Promise<T>): Promise<Result<Error, T>>;
|
|
174
|
+
declare function goTryRaw<T, E = InstanceType<typeof UnknownError>>(promise: Promise<T>, options: GoTryRawOptions<E>): Promise<Result<E, T>>;
|
|
175
|
+
declare function goTryRaw<T>(fn: () => T): Result<Error, T>;
|
|
176
|
+
declare function goTryRaw<T, E = InstanceType<typeof UnknownError>>(fn: () => T, options: GoTryRawOptions<E>): Result<E, T>;
|
|
177
|
+
declare function goTryRaw<T>(value: T): Result<Error, T>;
|
|
178
|
+
declare function goTryRaw<T, E = InstanceType<typeof UnknownError>>(value: T, options: GoTryRawOptions<E>): Result<E, T>;
|
|
112
179
|
|
|
113
180
|
/**
|
|
114
181
|
* Executes a function, promise, or value and returns a Result type with a fallback default.
|
|
@@ -178,18 +245,25 @@ declare function goTryAll<T extends readonly unknown[]>(items: {
|
|
|
178
245
|
}]>;
|
|
179
246
|
/**
|
|
180
247
|
* Like `goTryAll`, but returns raw Error objects instead of error messages.
|
|
248
|
+
* Non-tagged errors are wrapped in `UnknownError` by default (consistent with `goTryRaw`).
|
|
249
|
+
* Tagged errors pass through unchanged.
|
|
250
|
+
*
|
|
251
|
+
* Supports `errorClass` and `systemErrorClass` options (mutually exclusive):
|
|
252
|
+
* - `errorClass`: Wrap ALL errors in the specified class
|
|
253
|
+
* - `systemErrorClass`: Only wrap non-tagged errors (defaults to UnknownError)
|
|
181
254
|
*
|
|
182
255
|
* @template T The tuple type of all promise results
|
|
256
|
+
* @template E The type of the error
|
|
183
257
|
* @param {readonly [...{ [K in keyof T]: Promise<T[K]> | (() => Promise<T[K]>) }]} items - Array of promises or factories
|
|
184
|
-
* @param {
|
|
185
|
-
* @returns {Promise<[{ [K in keyof T]:
|
|
258
|
+
* @param {GoTryAllRawOptions<E>} options - Optional configuration
|
|
259
|
+
* @returns {Promise<[{ [K in keyof T]: E | undefined }, { [K in keyof T]: T[K] | undefined }]>}
|
|
186
260
|
* A tuple where the first element is a tuple of Error objects (or undefined) and
|
|
187
261
|
* the second element is a tuple of results (or undefined), preserving input order
|
|
188
262
|
*/
|
|
189
|
-
declare function goTryAllRaw<T extends readonly unknown[]
|
|
263
|
+
declare function goTryAllRaw<T extends readonly unknown[], E = InstanceType<typeof UnknownError>>(items: {
|
|
190
264
|
[K in keyof T]: Promise<T[K]> | (() => Promise<T[K]>);
|
|
191
|
-
}, options?:
|
|
192
|
-
[K in keyof T]:
|
|
265
|
+
}, options?: GoTryAllRawOptions<E>): Promise<[{
|
|
266
|
+
[K in keyof T]: E | undefined;
|
|
193
267
|
}, {
|
|
194
268
|
[K in keyof T]: T[K] | undefined;
|
|
195
269
|
}]>;
|
|
@@ -301,6 +375,6 @@ declare function failure<E>(error: E): Failure<E>;
|
|
|
301
375
|
*/
|
|
302
376
|
declare function assertNever(value: never): never;
|
|
303
377
|
|
|
304
|
-
export { assert, assertNever, failure, goTry, goTryAll, goTryAllRaw, goTryOr, goTryRaw, isFailure, isSuccess, success, taggedError };
|
|
305
|
-
export type { ErrorConstructor, Failure, GoTryAllOptions, MaybePromise, Result, ResultWithDefault, Success, TaggedError, TaggedUnion };
|
|
378
|
+
export { UnknownError, assert, assertNever, failure, goTry, goTryAll, goTryAllRaw, goTryOr, goTryRaw, isFailure, isSuccess, success, taggedError };
|
|
379
|
+
export type { ErrorConstructor, Failure, GoTryAllOptions, GoTryAllRawOptions, GoTryRawOptions, MaybePromise, Result, ResultWithDefault, Success, TaggedError, TaggedUnion };
|
|
306
380
|
//# sourceMappingURL=index.d.cts.map
|
package/dist/index.d.cts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.cts","sources":["../src/types.ts","../src/goTry.ts","../src/goTryRaw.ts","../src/goTryOr.ts","../src/goTryAll.ts","../src/tagged-error.ts","../src/assert.ts","../src/result-helpers.ts"],"mappings":"AAAA;;;AAIM,KAAM,OAAO;AACb,KAAM,OAAO;AACb,KAAM,MAAM,SAAS,OAAO,MAAM,OAAO;AAEzC,KAAM,iBAAiB;AAEvB,KAAM,YAAY,UAAU,OAAO;AAEzC;;;;AAIM,UAAW,WAAW;;;;;AAMtB,UAAW,eAAe;;;;;;;AAQhC;;;AAGM,KAAM,gBAAgB;;;
|
|
1
|
+
{"version":3,"file":"index.d.cts","sources":["../src/types.ts","../src/goTry.ts","../src/unknown-error.ts","../src/goTryRaw.ts","../src/goTryOr.ts","../src/goTryAll.ts","../src/tagged-error.ts","../src/assert.ts","../src/result-helpers.ts"],"mappings":"AAAA;;;AAIM,KAAM,OAAO;AACb,KAAM,OAAO;AACb,KAAM,MAAM,SAAS,OAAO,MAAM,OAAO;AAEzC,KAAM,iBAAiB;AAEvB,KAAM,YAAY,UAAU,OAAO;AAEzC;;;;AAIM,UAAW,WAAW;;;;;AAMtB,UAAW,eAAe;;;;;;;AAQhC;;;AAGM,KAAM,gBAAgB;;;AAO5B;;;;AAIM,KAAM,eAAe,KAAK,KAAK;gBACnB,gBAAgB;;;;sBACU,gBAAgB;;;;;AAG5D;;;;;AAKM,KAAM,kBAAkB,KAAK,KAAK;;gBACA,gBAAgB;;;;;sBACU,gBAAgB;;;;;;AAGlF;;;;;;;;;;;;;AAaM,KAAM,WAAW,oBAAoB,gBAAgB;iCAC1B,gBAAgB;;;ACrEjD;;;;;;;;;;;;;;;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