try-catch-util 1.0.8 → 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.
Files changed (2) hide show
  1. package/Readme.md +7 -27
  2. package/package.json +1 -1
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
- console.error(error.message);
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
- console.error(error.status, error.message);
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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "try-catch-util",
3
- "version": "1.0.8",
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",