@zokugun/xtry 0.2.0 → 0.3.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/README.md +4 -2
- package/lib/index.d.ts +1 -0
- package/lib/index.js +1 -0
- package/lib/stringify-error.d.ts +1 -0
- package/lib/stringify-error.js +18 -0
- package/lib/try.d.ts +2 -2
- package/lib/try.js +14 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -130,8 +130,10 @@ export function err<E>(error: E): Failure<E>;
|
|
|
130
130
|
### Try helpers
|
|
131
131
|
|
|
132
132
|
```typescript
|
|
133
|
-
export function xtry<T, E>(func: () => Exclude<T, Promise<unknown>>, handler?: (error:
|
|
134
|
-
export function xatry<T, E>(func: (() => Exclude<T, Promise<unknown>>) | Promise<Exclude<T, Promise<unknown>>>, handler?: (error:
|
|
133
|
+
export function xtry<T, E>(func: () => Exclude<T, Promise<unknown>>, handler?: (error: unknown) => void | E): Result<T, E>;
|
|
134
|
+
export function xatry<T, E>(func: (() => Exclude<T, Promise<unknown>>) | Promise<Exclude<T, Promise<unknown>>>, handler?: (error: unknown) => void | E): Promise<Result<T, E>>;
|
|
135
|
+
|
|
136
|
+
export function stringifyError(error: unknown): string;
|
|
135
137
|
```
|
|
136
138
|
|
|
137
139
|
Both helpers:
|
package/lib/index.d.ts
CHANGED
package/lib/index.js
CHANGED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function stringifyError(error: unknown): string;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export function stringifyError(error) {
|
|
2
|
+
if (typeof error === 'string') {
|
|
3
|
+
return error;
|
|
4
|
+
}
|
|
5
|
+
if (error instanceof Error) {
|
|
6
|
+
return error.message ?? String(error);
|
|
7
|
+
}
|
|
8
|
+
try {
|
|
9
|
+
const json = JSON.stringify(error);
|
|
10
|
+
if (typeof json === 'string') {
|
|
11
|
+
return json;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
catch {
|
|
15
|
+
// fallthrough
|
|
16
|
+
}
|
|
17
|
+
return String(error);
|
|
18
|
+
}
|
package/lib/try.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
import { type Result } from './result.js';
|
|
2
|
-
export declare function xtry<T, E>(func: () => Exclude<T, Promise<unknown>>, handler?: (
|
|
3
|
-
export declare function xatry<T, E>(func: (() => Exclude<T, Promise<unknown>>) | Promise<Exclude<T, Promise<unknown>>>, handler?: (
|
|
2
|
+
export declare function xtry<T, E = unknown>(func: () => Exclude<T, Promise<unknown>>, handler?: (error: unknown) => E | undefined): E extends void ? Result<T, unknown> : Result<T, E>;
|
|
3
|
+
export declare function xatry<T, E = unknown>(func: (() => Exclude<T, Promise<unknown>>) | Promise<Exclude<T, Promise<unknown>>>, handler?: (error: unknown) => E | undefined): Promise<E extends void ? Result<T, unknown> : Result<T, E>>;
|
package/lib/try.js
CHANGED
|
@@ -2,24 +2,36 @@ import { err, ok } from './result.js';
|
|
|
2
2
|
export function xtry(func, handler) {
|
|
3
3
|
try {
|
|
4
4
|
const value = func();
|
|
5
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
|
|
5
6
|
return ok(value);
|
|
6
7
|
}
|
|
7
8
|
catch (error) {
|
|
8
9
|
if (handler) {
|
|
9
|
-
handler(error);
|
|
10
|
+
const newError = handler(error);
|
|
11
|
+
if (newError !== undefined) {
|
|
12
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
|
|
13
|
+
return err(newError);
|
|
14
|
+
}
|
|
10
15
|
}
|
|
16
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
|
|
11
17
|
return err(error);
|
|
12
18
|
}
|
|
13
19
|
}
|
|
14
20
|
export async function xatry(func, handler) {
|
|
15
21
|
try {
|
|
16
22
|
const value = await (func instanceof Promise ? func : Promise.resolve().then(func));
|
|
23
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
|
|
17
24
|
return ok(value);
|
|
18
25
|
}
|
|
19
26
|
catch (error) {
|
|
20
27
|
if (handler) {
|
|
21
|
-
handler(error);
|
|
28
|
+
const newError = handler(error);
|
|
29
|
+
if (newError !== undefined) {
|
|
30
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
|
|
31
|
+
return err(newError);
|
|
32
|
+
}
|
|
22
33
|
}
|
|
34
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
|
|
23
35
|
return err(error);
|
|
24
36
|
}
|
|
25
37
|
}
|