try-catch-util 1.0.0 → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/Readme.md +105 -0
- package/package.json +1 -1
package/Readme.md
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
# try-catch-util
|
|
2
|
+
|
|
3
|
+
A tiny TypeScript utility for typed `try/catch` results.
|
|
4
|
+
|
|
5
|
+
`tryCatch` returns a destructurable `{ result, error }` object. The main benefit: `error` is typed as `Error` by default, or as the custom error generic you provide, so you do not have to cast caught values before using `message` or custom fields.
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```sh
|
|
10
|
+
npm install try-catch-util
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Example 1: Default Error
|
|
14
|
+
|
|
15
|
+
```ts
|
|
16
|
+
import { tryCatch } from 'try-catch-util';
|
|
17
|
+
|
|
18
|
+
type TodosResponse = {
|
|
19
|
+
todos: Array<{ id: number; todo: string; completed: boolean; userId: number }>;
|
|
20
|
+
total: number;
|
|
21
|
+
skip: number;
|
|
22
|
+
limit: number;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
const { result, error } = await tryCatch<TodosResponse>(async () => {
|
|
26
|
+
const response = await fetch('https://dummyjson.com/todos');
|
|
27
|
+
if (!response.ok) throw new Error(`Failed to load todos: ${response.status}`);
|
|
28
|
+
return response.json() as Promise<TodosResponse>;
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
if (error) {
|
|
32
|
+
console.error(error.message);
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
console.log(result.todos);
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
`error` is typed as `Error`, so `error.message` needs no cast.
|
|
40
|
+
|
|
41
|
+
## Example 2: Custom Error Generic
|
|
42
|
+
|
|
43
|
+
```ts
|
|
44
|
+
import { tryCatch } from 'try-catch-util';
|
|
45
|
+
|
|
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
|
+
class TodosApiError extends Error {
|
|
54
|
+
constructor(
|
|
55
|
+
message: string,
|
|
56
|
+
public readonly status: number,
|
|
57
|
+
) {
|
|
58
|
+
super(message);
|
|
59
|
+
this.name = 'TodosApiError';
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const { result, error } = await tryCatch<TodosResponse, TodosApiError>(async () => {
|
|
64
|
+
const response = await fetch('https://dummyjson.com/todos');
|
|
65
|
+
if (!response.ok) throw new TodosApiError('Unable to load todos', response.status);
|
|
66
|
+
return response.json() as Promise<TodosResponse>;
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
if (error) {
|
|
70
|
+
console.error(error.status, error.message);
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
console.log(result.todos.map((todo) => todo.todo));
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
`error` is typed as `TodosApiError`, so `error.status` needs no cast.
|
|
78
|
+
|
|
79
|
+
## TypeScript Result Type
|
|
80
|
+
|
|
81
|
+
```ts
|
|
82
|
+
type Success<T> = { result: T; error?: never };
|
|
83
|
+
type Failure<E> = { result?: never; error: E };
|
|
84
|
+
type Result<T, E extends Error = Error> = Success<T> | Failure<E>;
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
You can import the types when you want to describe helper return values.
|
|
88
|
+
|
|
89
|
+
```ts
|
|
90
|
+
import type { Result } from 'try-catch-util';
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
## API
|
|
94
|
+
|
|
95
|
+
```ts
|
|
96
|
+
function tryCatch<T, E extends Error = Error>(
|
|
97
|
+
input: () => Promise<T> | T,
|
|
98
|
+
): Promise<Result<T, E>>;
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
If a function throws or rejects with a string, `tryCatch` converts it into an `Error` instance. Other thrown values are preserved as-is.
|
|
102
|
+
|
|
103
|
+
## License
|
|
104
|
+
|
|
105
|
+
MIT
|