try-catch-util 1.0.7 → 1.0.9
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 +7 -27
- package/dist/cjs/index.cjs.map +2 -2
- package/dist/esm/index.js.map +2 -2
- package/dist/index.d.ts +1 -3
- package/package.json +3 -2
package/Readme.md
CHANGED
|
@@ -28,12 +28,8 @@ const { result, error } = await tryCatch<TodosResponse>(async () => {
|
|
|
28
28
|
return response.json() as Promise<TodosResponse>;
|
|
29
29
|
});
|
|
30
30
|
|
|
31
|
-
if (error)
|
|
32
|
-
|
|
33
|
-
return;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
console.log(result.todos);
|
|
31
|
+
if (error) console.error(error.message);
|
|
32
|
+
else console.log(result.todos);
|
|
37
33
|
```
|
|
38
34
|
|
|
39
35
|
`error` is typed as `Error`, so `error.message` needs no cast.
|
|
@@ -43,18 +39,8 @@ console.log(result.todos);
|
|
|
43
39
|
```ts
|
|
44
40
|
import { tryCatch } from 'try-catch-util';
|
|
45
41
|
|
|
46
|
-
type TodosResponse = {
|
|
47
|
-
todos: Array<{ id: number; todo: string; completed: boolean; userId: number }>;
|
|
48
|
-
total: number;
|
|
49
|
-
skip: number;
|
|
50
|
-
limit: number;
|
|
51
|
-
};
|
|
52
|
-
|
|
53
42
|
class TodosApiError extends Error {
|
|
54
|
-
constructor(
|
|
55
|
-
message: string,
|
|
56
|
-
public readonly status: number,
|
|
57
|
-
) {
|
|
43
|
+
constructor(message: string, public readonly status: number) {
|
|
58
44
|
super(message);
|
|
59
45
|
this.name = 'TodosApiError';
|
|
60
46
|
}
|
|
@@ -66,12 +52,8 @@ const { result, error } = await tryCatch<TodosResponse, TodosApiError>(async ()
|
|
|
66
52
|
return response.json() as Promise<TodosResponse>;
|
|
67
53
|
});
|
|
68
54
|
|
|
69
|
-
if (error)
|
|
70
|
-
|
|
71
|
-
return;
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
console.log(result.todos.map((todo) => todo.todo));
|
|
55
|
+
if (error) console.error(error.status, error.message);
|
|
56
|
+
else console.log(result.todos.map((todo) => todo.todo));
|
|
75
57
|
```
|
|
76
58
|
|
|
77
59
|
`error` is typed as `TodosApiError`, so `error.status` needs no cast.
|
|
@@ -93,12 +75,10 @@ import type { Result } from 'try-catch-util';
|
|
|
93
75
|
## API
|
|
94
76
|
|
|
95
77
|
```ts
|
|
96
|
-
function tryCatch<T, E extends Error = Error>(
|
|
97
|
-
input: () => Promise<T> | T,
|
|
98
|
-
): Promise<Result<T, E>>;
|
|
78
|
+
function tryCatch<T, E extends Error = Error>(input: () => Promise<T> | T): Promise<Result<T, E>>;
|
|
99
79
|
```
|
|
100
80
|
|
|
101
|
-
If a function throws or rejects with a string, `tryCatch` converts it into an `Error` instance. Other thrown values are preserved as-is.
|
|
81
|
+
> Note: If a function throws or rejects with a string, `tryCatch` converts it into an `Error` instance. Other thrown values are preserved as-is.
|
|
102
82
|
|
|
103
83
|
## License
|
|
104
84
|
|
package/dist/cjs/index.cjs.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/index.ts"],
|
|
4
|
-
"sourcesContent": ["export type Success<T> = { result: T; error?: never };\nexport type Failure<E> = { result?: never; error: E };\nexport type Result<T, E extends Error = Error> = Success<T> | Failure<E>;\n\n/**\n * Executes a synchronous or asynchronous function and returns a typed result object.\n
|
|
5
|
-
"mappings": "4dAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,cAAAE,IAAA,eAAAC,EAAAH,
|
|
4
|
+
"sourcesContent": ["export type Success<T> = { result: T; error?: never };\nexport type Failure<E> = { result?: never; error: E };\nexport type Result<T, E extends Error = Error> = Success<T> | Failure<E>;\n\n/**\n * Executes a synchronous or asynchronous function and returns a typed result object.\n * Successful executions resolve with `{ result }`, while thrown errors resolve with\n * `{ error }`. If a string is thrown, it is converted into an `Error` instance.\n * @param input - Function to execute safely without messy try/catch blocks.\n * @returns A promise containing either the function result or the captured error.\n */\nexport async function tryCatch<T, E extends Error = Error>(input: () => Promise<T> | T): Promise<Result<T, E>> {\n try {\n const result = await input();\n return { result };\n } catch (e) {\n const error = typeof e === 'string' ? (new Error(String(e)) as E) : (e as E);\n return { error };\n }\n}\n"],
|
|
5
|
+
"mappings": "4dAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,cAAAE,IAAA,eAAAC,EAAAH,GAWA,eAAsBI,EAAqCC,EAAoD,CAC7G,GAAI,CAEF,MAAO,CAAE,OADM,MAAMA,EAAM,CACX,CAClB,OAASC,EAAG,CAEV,MAAO,CAAE,MADK,OAAOA,GAAM,SAAY,IAAI,MAAM,OAAOA,CAAC,CAAC,EAAWA,CACtD,CACjB,CACF,CARsBC,EAAAH,EAAA",
|
|
6
6
|
"names": ["index_exports", "__export", "tryCatch", "__toCommonJS", "tryCatch", "input", "e", "__name"]
|
|
7
7
|
}
|
package/dist/esm/index.js.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/index.ts"],
|
|
4
|
-
"sourcesContent": ["export type Success<T> = { result: T; error?: never };\nexport type Failure<E> = { result?: never; error: E };\nexport type Result<T, E extends Error = Error> = Success<T> | Failure<E>;\n\n/**\n * Executes a synchronous or asynchronous function and returns a typed result object.\n
|
|
5
|
-
"mappings": "+
|
|
4
|
+
"sourcesContent": ["export type Success<T> = { result: T; error?: never };\nexport type Failure<E> = { result?: never; error: E };\nexport type Result<T, E extends Error = Error> = Success<T> | Failure<E>;\n\n/**\n * Executes a synchronous or asynchronous function and returns a typed result object.\n * Successful executions resolve with `{ result }`, while thrown errors resolve with\n * `{ error }`. If a string is thrown, it is converted into an `Error` instance.\n * @param input - Function to execute safely without messy try/catch blocks.\n * @returns A promise containing either the function result or the captured error.\n */\nexport async function tryCatch<T, E extends Error = Error>(input: () => Promise<T> | T): Promise<Result<T, E>> {\n try {\n const result = await input();\n return { result };\n } catch (e) {\n const error = typeof e === 'string' ? (new Error(String(e)) as E) : (e as E);\n return { error };\n }\n}\n"],
|
|
5
|
+
"mappings": "+EAWA,eAAsBA,EAAqCC,EAAoD,CAC7G,GAAI,CAEF,MAAO,CAAE,OADM,MAAMA,EAAM,CACX,CAClB,OAASC,EAAG,CAEV,MAAO,CAAE,MADK,OAAOA,GAAM,SAAY,IAAI,MAAM,OAAOA,CAAC,CAAC,EAAWA,CACtD,CACjB,CACF,CARsBC,EAAAH,EAAA",
|
|
6
6
|
"names": ["tryCatch", "input", "e", "__name"]
|
|
7
7
|
}
|
package/dist/index.d.ts
CHANGED
|
@@ -9,11 +9,9 @@ export type Failure<E> = {
|
|
|
9
9
|
export type Result<T, E extends Error = Error> = Success<T> | Failure<E>;
|
|
10
10
|
/**
|
|
11
11
|
* Executes a synchronous or asynchronous function and returns a typed result object.
|
|
12
|
-
*
|
|
13
12
|
* Successful executions resolve with `{ result }`, while thrown errors resolve with
|
|
14
13
|
* `{ error }`. If a string is thrown, it is converted into an `Error` instance.
|
|
15
|
-
*
|
|
16
|
-
* @param input - Function to execute safely.
|
|
14
|
+
* @param input - Function to execute safely without messy try/catch blocks.
|
|
17
15
|
* @returns A promise containing either the function result or the captured error.
|
|
18
16
|
*/
|
|
19
17
|
export declare function tryCatch<T, E extends Error = Error>(input: () => Promise<T> | T): Promise<Result<T, E>>;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "try-catch-util",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.9",
|
|
4
4
|
"description": "A simple util to wrap sync/async functions and handle errors type-safely.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"try catch",
|
|
@@ -35,7 +35,8 @@
|
|
|
35
35
|
"check": "biome check .",
|
|
36
36
|
"test": "jest",
|
|
37
37
|
"build": "tsc --build --noEmit && tsx esbuild.config.ts",
|
|
38
|
-
"prepublishOnly": "npm run build && npm test"
|
|
38
|
+
"prepublishOnly": "npm run build && npm test",
|
|
39
|
+
"patch": "npm version patch -m \"chore(release): %s [skip ci]\""
|
|
39
40
|
},
|
|
40
41
|
"devDependencies": {
|
|
41
42
|
"@biomejs/biome": "^2.4.16",
|