neverpanic 0.0.1 → 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/dist/index.js +61 -0
- package/package.json +4 -2
- package/index.ts +0 -29
- package/tsconfig.json +0 -29
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/dist/index.js
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
12
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
|
|
13
|
+
return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
14
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
15
|
+
function step(op) {
|
|
16
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
17
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
18
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
19
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
20
|
+
switch (op[0]) {
|
|
21
|
+
case 0: case 1: t = op; break;
|
|
22
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
23
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
24
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
25
|
+
default:
|
|
26
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
27
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
28
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
29
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
30
|
+
if (t[2]) _.ops.pop();
|
|
31
|
+
_.trys.pop(); continue;
|
|
32
|
+
}
|
|
33
|
+
op = body.call(thisArg, _);
|
|
34
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
35
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
|
+
exports.o = void 0;
|
|
40
|
+
function safeReturn(cb, eh) {
|
|
41
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
42
|
+
var e_1;
|
|
43
|
+
var _a;
|
|
44
|
+
return __generator(this, function (_b) {
|
|
45
|
+
switch (_b.label) {
|
|
46
|
+
case 0:
|
|
47
|
+
_b.trys.push([0, 2, , 3]);
|
|
48
|
+
return [4 /*yield*/, cb()];
|
|
49
|
+
case 1: return [2 /*return*/, _b.sent()];
|
|
50
|
+
case 2:
|
|
51
|
+
e_1 = _b.sent();
|
|
52
|
+
return [2 /*return*/, {
|
|
53
|
+
success: false,
|
|
54
|
+
error: (_a = eh === null || eh === void 0 ? void 0 : eh(e_1)) !== null && _a !== void 0 ? _a : null,
|
|
55
|
+
}];
|
|
56
|
+
case 3: return [2 /*return*/];
|
|
57
|
+
}
|
|
58
|
+
});
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
exports.o = { safeReturn: safeReturn };
|
package/package.json
CHANGED
|
@@ -1,11 +1,13 @@
|
|
|
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": "
|
|
7
|
+
"build": "tsc",
|
|
8
|
+
"test": "bun test"
|
|
8
9
|
},
|
|
10
|
+
"files": ["dist"],
|
|
9
11
|
"exports": {
|
|
10
12
|
".": "./dist/index.js"
|
|
11
13
|
},
|
package/index.ts
DELETED
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
type Result<T, E = unknown> =
|
|
2
|
-
| {
|
|
3
|
-
success: true;
|
|
4
|
-
data: T;
|
|
5
|
-
}
|
|
6
|
-
| { success: false; error: E };
|
|
7
|
-
|
|
8
|
-
async function safeReturn<T, E>(
|
|
9
|
-
cb: () => Promise<Result<T, E>> | Result<T, E>,
|
|
10
|
-
): Promise<Result<T, E | null>>;
|
|
11
|
-
async function safeReturn<T, E, H>(
|
|
12
|
-
cb: () => Promise<Result<T, E>> | Result<T, E>,
|
|
13
|
-
eh: (err: unknown) => H,
|
|
14
|
-
): Promise<Result<T, E | H>>;
|
|
15
|
-
async function safeReturn<T, E, H>(
|
|
16
|
-
cb: () => Promise<Result<T, E>> | Result<T, E>,
|
|
17
|
-
eh?: (err: unknown) => H,
|
|
18
|
-
): Promise<Result<T, E | H | null>> {
|
|
19
|
-
try {
|
|
20
|
-
return await cb();
|
|
21
|
-
} catch (e) {
|
|
22
|
-
return {
|
|
23
|
-
success: false,
|
|
24
|
-
error: eh?.(e) ?? null,
|
|
25
|
-
};
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
export const o = { safeReturn };
|
package/tsconfig.json
DELETED
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"compilerOptions": {
|
|
3
|
-
// Environment setup & latest features
|
|
4
|
-
"lib": ["ESNext"],
|
|
5
|
-
"target": "ESNext",
|
|
6
|
-
"module": "Preserve",
|
|
7
|
-
"moduleDetection": "force",
|
|
8
|
-
"jsx": "react-jsx",
|
|
9
|
-
"allowJs": true,
|
|
10
|
-
|
|
11
|
-
// Bundler mode
|
|
12
|
-
"moduleResolution": "bundler",
|
|
13
|
-
"allowImportingTsExtensions": true,
|
|
14
|
-
"verbatimModuleSyntax": true,
|
|
15
|
-
"noEmit": true,
|
|
16
|
-
|
|
17
|
-
// Best practices
|
|
18
|
-
"strict": true,
|
|
19
|
-
"skipLibCheck": true,
|
|
20
|
-
"noFallthroughCasesInSwitch": true,
|
|
21
|
-
"noUncheckedIndexedAccess": true,
|
|
22
|
-
"noImplicitOverride": true,
|
|
23
|
-
|
|
24
|
-
// Some stricter flags (disabled by default)
|
|
25
|
-
"noUnusedLocals": false,
|
|
26
|
-
"noUnusedParameters": false,
|
|
27
|
-
"noPropertyAccessFromIndexSignature": false
|
|
28
|
-
}
|
|
29
|
-
}
|