neverpanic 0.0.2 → 0.0.3
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 +51 -2
- package/package.json +4 -3
- package/dist/index.d.ts +0 -13
package/README.md
CHANGED
|
@@ -1,5 +1,54 @@
|
|
|
1
1
|
# neverpanic
|
|
2
2
|
|
|
3
|
-
This is the next generation of error handling in JavaScript. How many times have
|
|
3
|
+
This is the next generation of error handling in JavaScript. How many times have
|
|
4
|
+
you gotten out of bed, ready to get on with your day, only to find that you have
|
|
5
|
+
1243 Sentry alerts because you forgot to gracefuly handle an exception in your
|
|
6
|
+
NodeJS backend. The truth is, these are not exceptions, these are panics. Panics
|
|
7
|
+
not just in your code, but also to your mental health.
|
|
4
8
|
|
|
5
|
-
Try neverpanic, and live a zen life.
|
|
9
|
+
Try neverpanic, and live a zen life.
|
|
10
|
+
|
|
11
|
+
## Examples
|
|
12
|
+
|
|
13
|
+
### safeFn
|
|
14
|
+
|
|
15
|
+
Create a safe function from an unsafe one:
|
|
16
|
+
|
|
17
|
+
- Ensures that only a `Result` can be returned
|
|
18
|
+
- Catches and returns any unexpected errors as a `Result`
|
|
19
|
+
|
|
20
|
+
```ts
|
|
21
|
+
const getUser = n.safeFn(
|
|
22
|
+
async (id: string) => {
|
|
23
|
+
const res = await fetch(`https://example.com/users/${id}`);
|
|
24
|
+
if (!res.ok) return { success: false, error: "FAILED_TO_FETCH" };
|
|
25
|
+
|
|
26
|
+
return { success: true, data: await res.json() };
|
|
27
|
+
},
|
|
28
|
+
(err) => "FAILED_TO_GET_USER",
|
|
29
|
+
);
|
|
30
|
+
|
|
31
|
+
const getUserResult = await getUser("some-user-id");
|
|
32
|
+
if (!getUserResult.success) {
|
|
33
|
+
console.error(getUserResult.error);
|
|
34
|
+
} else {
|
|
35
|
+
console.log(getUserResult.data);
|
|
36
|
+
}
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
### fromUnsafe
|
|
40
|
+
|
|
41
|
+
Runs the provided callback function, catching any thrown errors and returning a
|
|
42
|
+
`Result`
|
|
43
|
+
|
|
44
|
+
```ts
|
|
45
|
+
const user = await n.fromUnsafe(
|
|
46
|
+
() => db.findUser("some-user-id"),
|
|
47
|
+
(err) => "FAILED_T0_FIND_USER",
|
|
48
|
+
);
|
|
49
|
+
if (!user.success) {
|
|
50
|
+
console.error(user.error);
|
|
51
|
+
} else {
|
|
52
|
+
console.log(user.data);
|
|
53
|
+
}
|
|
54
|
+
```
|
package/package.json
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "neverpanic",
|
|
3
3
|
"module": "index.ts",
|
|
4
|
-
"version": "0.0.
|
|
4
|
+
"version": "0.0.3",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"scripts": {
|
|
7
|
-
"build": "tsc
|
|
7
|
+
"build": "tsc",
|
|
8
|
+
"test": "bun test"
|
|
8
9
|
},
|
|
9
10
|
"files": ["dist"],
|
|
10
11
|
"exports": {
|
|
@@ -14,6 +15,6 @@
|
|
|
14
15
|
"@types/bun": "latest"
|
|
15
16
|
},
|
|
16
17
|
"peerDependencies": {
|
|
17
|
-
"
|
|
18
|
+
"typescript": "5"
|
|
18
19
|
}
|
|
19
20
|
}
|
package/dist/index.d.ts
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
type Result<T, E = unknown> = {
|
|
2
|
-
success: true;
|
|
3
|
-
data: T;
|
|
4
|
-
} | {
|
|
5
|
-
success: false;
|
|
6
|
-
error: E;
|
|
7
|
-
};
|
|
8
|
-
declare function safeReturn<T, E>(cb: () => Promise<Result<T, E>> | Result<T, E>): Promise<Result<T, E | null>>;
|
|
9
|
-
declare function safeReturn<T, E, H>(cb: () => Promise<Result<T, E>> | Result<T, E>, eh: (err: unknown) => H): Promise<Result<T, E | H>>;
|
|
10
|
-
export declare const o: {
|
|
11
|
-
safeReturn: typeof safeReturn;
|
|
12
|
-
};
|
|
13
|
-
export {};
|