effect-errors 1.2.24 → 1.3.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 +44 -0
- package/capture-errors.d.ts +19 -0
- package/capture-errors.js +42 -0
- package/caputre-errors.test.d.ts +1 -0
- package/caputre-errors.test.js +156 -0
- package/examples/errors/fetch-error.d.ts +9 -0
- package/examples/errors/fetch-error.js +27 -0
- package/examples/errors/file-error.d.ts +9 -0
- package/examples/errors/file-error.js +27 -0
- package/examples/errors/user-not-found.error.d.ts +9 -0
- package/examples/errors/user-not-found.error.js +27 -0
- package/examples/from-promise.d.ts +5 -0
- package/examples/from-promise.js +113 -0
- package/examples/parallel-errors.d.ts +4 -0
- package/examples/parallel-errors.js +69 -0
- package/examples/util/filename.effect.d.ts +2 -0
- package/examples/util/filename.effect.js +11 -0
- package/index.d.ts +1 -0
- package/index.js +1 -0
- package/logic/errors/capture-errors-from-cause.d.ts +2 -2
- package/logic/errors/capture-errors-from-cause.js +1 -1
- package/logic/errors/extract-error-details.d.ts +7 -0
- package/logic/errors/extract-error-details.js +74 -0
- package/logic/errors/parse-error.js +7 -4
- package/logic/spans/get-span-duration.d.ts +1 -1
- package/logic/strip-cwd-path.js +1 -1
- package/package.json +21 -13
- package/pretty-print.d.ts +2 -2
- package/pretty-print.js +38 -27
- package/runners/run-promise.d.ts +1 -1
- package/runners/run-promise.js +81 -9
- package/runners/run-sync.d.ts +1 -1
- package/runners/run-sync.js +1 -1
- package/tests/mocks/console.mock.d.ts +9 -0
- package/tests/mocks/console.mock.js +66 -0
- package/tests/runners/effect-cause.d.ts +2 -0
- package/tests/runners/effect-cause.js +49 -0
- package/types/pretty-error.type.d.ts +5 -10
- package/types/pretty-error.type.js +3 -11
- package/logic/errors/pretty-error-message.d.ts +0 -1
- package/logic/errors/pretty-error-message.js +0 -59
package/README.md
CHANGED
|
@@ -137,6 +137,50 @@ Alternatively, you _can_ use a plain object with a `_tag` and `message` attribut
|
|
|
137
137
|
Effect.fail({ _tag: 'SucksToBeMe', message: 'Yeah...' });
|
|
138
138
|
```
|
|
139
139
|
|
|
140
|
+
## ⚡ Capturing errors data
|
|
141
|
+
|
|
142
|
+
You might want to apply your own logic to reported errors data; for example if you want to display errors in html. You can do so using `captureErrors`. The function has the following signature:
|
|
143
|
+
|
|
144
|
+
```typescript
|
|
145
|
+
export interface ErrorSpan {
|
|
146
|
+
name: string;
|
|
147
|
+
attributes: ReadonlyMap<string, unknown>;
|
|
148
|
+
status: SpanStatus;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
export interface ErrorData {
|
|
152
|
+
errorType: unknown;
|
|
153
|
+
message: unknown;
|
|
154
|
+
stack?: string;
|
|
155
|
+
spans?: ErrorSpan[];
|
|
156
|
+
isPlainString: boolean;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
export interface CapturedErrors {
|
|
160
|
+
interrupted: boolean;
|
|
161
|
+
errors: ErrorData[];
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
type captureErrorsFunction: <E>(cause: Cause<E>) => CapturedErrors
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
You can use `captureErrors` like so:
|
|
168
|
+
|
|
169
|
+
```typescript
|
|
170
|
+
import { captureErrors } from 'effect-errors';
|
|
171
|
+
|
|
172
|
+
await Effect.runPromise(
|
|
173
|
+
pipe(
|
|
174
|
+
effect,
|
|
175
|
+
Effect.catchAll((e) => {
|
|
176
|
+
const data = captureErrors(e);
|
|
177
|
+
|
|
178
|
+
// ...
|
|
179
|
+
}),
|
|
180
|
+
),
|
|
181
|
+
);
|
|
182
|
+
```
|
|
183
|
+
|
|
140
184
|
## ⚡ examples
|
|
141
185
|
|
|
142
186
|
I wrote some examples for fun and giggles. You can run them using:
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { type Cause } from 'effect/Cause';
|
|
2
|
+
import { type SpanStatus } from 'effect/Tracer';
|
|
3
|
+
export interface ErrorSpan {
|
|
4
|
+
name: string;
|
|
5
|
+
attributes: ReadonlyMap<string, unknown>;
|
|
6
|
+
status: SpanStatus;
|
|
7
|
+
}
|
|
8
|
+
export interface ErrorData {
|
|
9
|
+
errorType: unknown;
|
|
10
|
+
message: unknown;
|
|
11
|
+
stack?: string;
|
|
12
|
+
spans?: ErrorSpan[];
|
|
13
|
+
isPlainString: boolean;
|
|
14
|
+
}
|
|
15
|
+
export interface CapturedErrors {
|
|
16
|
+
interrupted: boolean;
|
|
17
|
+
errors: ErrorData[];
|
|
18
|
+
}
|
|
19
|
+
export declare const captureErrors: <E>(cause: Cause<E>) => CapturedErrors;
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.captureErrors = void 0;
|
|
4
|
+
var effect_1 = require("effect");
|
|
5
|
+
var Cause_1 = require("effect/Cause");
|
|
6
|
+
var capture_errors_from_cause_1 = require("./logic/errors/capture-errors-from-cause");
|
|
7
|
+
var captureErrors = function (cause) {
|
|
8
|
+
if ((0, Cause_1.isInterruptedOnly)(cause)) {
|
|
9
|
+
return {
|
|
10
|
+
interrupted: true,
|
|
11
|
+
errors: [],
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
var errors = (0, capture_errors_from_cause_1.captureErrorsFrom)(cause).map(function (_a) {
|
|
15
|
+
var message = _a.message, stack = _a.stack, span = _a.span, errorType = _a.errorType, isPlainString = _a.isPlainString;
|
|
16
|
+
var spans = [];
|
|
17
|
+
if (span !== undefined) {
|
|
18
|
+
var current = span;
|
|
19
|
+
while (current !== undefined && current._tag === 'Span') {
|
|
20
|
+
var name_1 = current.name, attributes = current.attributes, status_1 = current.status;
|
|
21
|
+
spans.push({
|
|
22
|
+
name: name_1,
|
|
23
|
+
attributes: attributes,
|
|
24
|
+
status: status_1,
|
|
25
|
+
});
|
|
26
|
+
current = effect_1.Option.getOrUndefined(current.parent);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
return {
|
|
30
|
+
errorType: errorType,
|
|
31
|
+
message: message,
|
|
32
|
+
stack: stack,
|
|
33
|
+
spans: spans,
|
|
34
|
+
isPlainString: isPlainString,
|
|
35
|
+
};
|
|
36
|
+
});
|
|
37
|
+
return {
|
|
38
|
+
interrupted: false,
|
|
39
|
+
errors: errors,
|
|
40
|
+
};
|
|
41
|
+
};
|
|
42
|
+
exports.captureErrors = captureErrors;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,156 @@
|
|
|
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;
|
|
13
|
+
return g = { next: verb(0), "throw": verb(1), "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
|
+
var vitest_1 = require("vitest");
|
|
40
|
+
var capture_errors_1 = require("./capture-errors");
|
|
41
|
+
var from_promise_1 = require("./examples/from-promise");
|
|
42
|
+
var parallel_errors_1 = require("./examples/parallel-errors");
|
|
43
|
+
var console_mock_1 = require("./tests/mocks/console.mock");
|
|
44
|
+
var effect_cause_1 = require("./tests/runners/effect-cause");
|
|
45
|
+
void (0, console_mock_1.mockConsole)({
|
|
46
|
+
info: vitest_1.vi.fn(),
|
|
47
|
+
});
|
|
48
|
+
(0, vitest_1.describe)('captureErrors function', function () {
|
|
49
|
+
(0, vitest_1.it)('should capture errors from promises', function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
50
|
+
var cause, result, _a, errorType, isPlainString, message, spans, stack;
|
|
51
|
+
return __generator(this, function (_b) {
|
|
52
|
+
switch (_b.label) {
|
|
53
|
+
case 0: return [4 /*yield*/, (0, effect_cause_1.effectCause)(from_promise_1.fromPromiseTask)];
|
|
54
|
+
case 1:
|
|
55
|
+
cause = _b.sent();
|
|
56
|
+
result = (0, capture_errors_1.captureErrors)(cause);
|
|
57
|
+
(0, vitest_1.expect)(result.interrupted).toBe(false);
|
|
58
|
+
(0, vitest_1.expect)(result.errors).toHaveLength(1);
|
|
59
|
+
_a = result.errors[0], errorType = _a.errorType, isPlainString = _a.isPlainString, message = _a.message, spans = _a.spans, stack = _a.stack;
|
|
60
|
+
(0, vitest_1.expect)(errorType).toBe('FetchError');
|
|
61
|
+
(0, vitest_1.expect)(isPlainString).toBe(false);
|
|
62
|
+
(0, vitest_1.expect)(message.toString()).toStrictEqual('TypeError: fetch failed');
|
|
63
|
+
(0, vitest_1.expect)(spans).toHaveLength(2);
|
|
64
|
+
(0, vitest_1.expect)(spans === null || spans === void 0 ? void 0 : spans[0].name).toBe('fetchUser');
|
|
65
|
+
(0, vitest_1.expect)(spans === null || spans === void 0 ? void 0 : spans[0].attributes).toHaveAttributes([
|
|
66
|
+
{
|
|
67
|
+
key: 'userId',
|
|
68
|
+
value: '123',
|
|
69
|
+
},
|
|
70
|
+
]);
|
|
71
|
+
(0, vitest_1.expect)(spans === null || spans === void 0 ? void 0 : spans[1].attributes).toHaveAttributes([]);
|
|
72
|
+
(0, vitest_1.expect)(stack).not.toHaveLength(0);
|
|
73
|
+
return [2 /*return*/];
|
|
74
|
+
}
|
|
75
|
+
});
|
|
76
|
+
}); });
|
|
77
|
+
// eslint-disable-next-line complexity
|
|
78
|
+
(0, vitest_1.it)('should capture parallel errors', function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
79
|
+
var cause, result, firstError, secondError, thirdError;
|
|
80
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t;
|
|
81
|
+
return __generator(this, function (_u) {
|
|
82
|
+
switch (_u.label) {
|
|
83
|
+
case 0: return [4 /*yield*/, (0, effect_cause_1.effectCause)(parallel_errors_1.withParallelErrorsTask)];
|
|
84
|
+
case 1:
|
|
85
|
+
cause = _u.sent();
|
|
86
|
+
result = (0, capture_errors_1.captureErrors)(cause);
|
|
87
|
+
(0, vitest_1.expect)(result.interrupted).toBe(false);
|
|
88
|
+
(0, vitest_1.expect)(result.errors).toHaveLength(3);
|
|
89
|
+
firstError = result.errors[0];
|
|
90
|
+
(0, vitest_1.expect)(firstError.errorType).toBe('UserNotFound');
|
|
91
|
+
(0, vitest_1.expect)(firstError.isPlainString).toBe(false);
|
|
92
|
+
(0, vitest_1.expect)(firstError.message).toStrictEqual('Oh no, this user does no exist!');
|
|
93
|
+
(0, vitest_1.expect)(firstError.spans).toHaveLength(3);
|
|
94
|
+
(0, vitest_1.expect)((_a = firstError.spans) === null || _a === void 0 ? void 0 : _a[0].name).toBe('readUser');
|
|
95
|
+
(0, vitest_1.expect)((_b = firstError.spans) === null || _b === void 0 ? void 0 : _b[0].attributes).toHaveAttributes([
|
|
96
|
+
{
|
|
97
|
+
key: 'name',
|
|
98
|
+
value: 'yolo',
|
|
99
|
+
},
|
|
100
|
+
]);
|
|
101
|
+
(0, vitest_1.expect)((_c = firstError.spans) === null || _c === void 0 ? void 0 : _c[1].name).toBe('parallelGet');
|
|
102
|
+
(0, vitest_1.expect)((_d = firstError.spans) === null || _d === void 0 ? void 0 : _d[1].attributes).toHaveAttributes([
|
|
103
|
+
{
|
|
104
|
+
key: 'names',
|
|
105
|
+
value: ['yolo', 'bro', 'cool'],
|
|
106
|
+
},
|
|
107
|
+
]);
|
|
108
|
+
(0, vitest_1.expect)((_e = firstError.spans) === null || _e === void 0 ? void 0 : _e[2].name).toBe('withParallelErrorsTask');
|
|
109
|
+
(0, vitest_1.expect)((_f = firstError.spans) === null || _f === void 0 ? void 0 : _f[2].attributes).toHaveAttributes([]);
|
|
110
|
+
secondError = result.errors[1];
|
|
111
|
+
(0, vitest_1.expect)(secondError.errorType).toBe('UserNotFound');
|
|
112
|
+
(0, vitest_1.expect)(secondError.isPlainString).toBe(false);
|
|
113
|
+
(0, vitest_1.expect)(secondError.message).toStrictEqual('Oh no, this user does no exist!');
|
|
114
|
+
(0, vitest_1.expect)(secondError.spans).toHaveLength(3);
|
|
115
|
+
(0, vitest_1.expect)((_g = secondError.spans) === null || _g === void 0 ? void 0 : _g[0].name).toBe('readUser');
|
|
116
|
+
(0, vitest_1.expect)((_h = secondError.spans) === null || _h === void 0 ? void 0 : _h[0].attributes).toHaveAttributes([
|
|
117
|
+
{
|
|
118
|
+
key: 'name',
|
|
119
|
+
value: 'bro',
|
|
120
|
+
},
|
|
121
|
+
]);
|
|
122
|
+
(0, vitest_1.expect)((_j = secondError.spans) === null || _j === void 0 ? void 0 : _j[1].name).toBe('parallelGet');
|
|
123
|
+
(0, vitest_1.expect)((_k = secondError.spans) === null || _k === void 0 ? void 0 : _k[1].attributes).toHaveAttributes([
|
|
124
|
+
{
|
|
125
|
+
key: 'names',
|
|
126
|
+
value: ['yolo', 'bro', 'cool'],
|
|
127
|
+
},
|
|
128
|
+
]);
|
|
129
|
+
(0, vitest_1.expect)((_l = secondError.spans) === null || _l === void 0 ? void 0 : _l[2].name).toBe('withParallelErrorsTask');
|
|
130
|
+
(0, vitest_1.expect)((_m = secondError.spans) === null || _m === void 0 ? void 0 : _m[2].attributes).toHaveAttributes([]);
|
|
131
|
+
thirdError = result.errors[2];
|
|
132
|
+
(0, vitest_1.expect)(thirdError.errorType).toBe('UserNotFound');
|
|
133
|
+
(0, vitest_1.expect)(thirdError.isPlainString).toBe(false);
|
|
134
|
+
(0, vitest_1.expect)(thirdError.message).toStrictEqual('Oh no, this user does no exist!');
|
|
135
|
+
(0, vitest_1.expect)(thirdError.spans).toHaveLength(3);
|
|
136
|
+
(0, vitest_1.expect)((_o = thirdError.spans) === null || _o === void 0 ? void 0 : _o[0].name).toBe('readUser');
|
|
137
|
+
(0, vitest_1.expect)((_p = thirdError.spans) === null || _p === void 0 ? void 0 : _p[0].attributes).toHaveAttributes([
|
|
138
|
+
{
|
|
139
|
+
key: 'name',
|
|
140
|
+
value: 'cool',
|
|
141
|
+
},
|
|
142
|
+
]);
|
|
143
|
+
(0, vitest_1.expect)((_q = thirdError.spans) === null || _q === void 0 ? void 0 : _q[1].name).toBe('parallelGet');
|
|
144
|
+
(0, vitest_1.expect)((_r = thirdError.spans) === null || _r === void 0 ? void 0 : _r[1].attributes).toHaveAttributes([
|
|
145
|
+
{
|
|
146
|
+
key: 'names',
|
|
147
|
+
value: ['yolo', 'bro', 'cool'],
|
|
148
|
+
},
|
|
149
|
+
]);
|
|
150
|
+
(0, vitest_1.expect)((_s = thirdError.spans) === null || _s === void 0 ? void 0 : _s[2].name).toBe('withParallelErrorsTask');
|
|
151
|
+
(0, vitest_1.expect)((_t = thirdError.spans) === null || _t === void 0 ? void 0 : _t[2].attributes).toHaveAttributes([]);
|
|
152
|
+
return [2 /*return*/];
|
|
153
|
+
}
|
|
154
|
+
});
|
|
155
|
+
}); });
|
|
156
|
+
});
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
declare const FetchError_base: new <A extends Record<string, any> = {}>(args: import("effect/Types").Equals<A, {}> extends true ? void : { readonly [P in keyof A as P extends "_tag" ? never : P]: A[P]; }) => import("effect/Cause").YieldableError & {
|
|
2
|
+
readonly _tag: "FetchError";
|
|
3
|
+
} & Readonly<A>;
|
|
4
|
+
export declare class FetchError extends FetchError_base<{
|
|
5
|
+
cause?: unknown;
|
|
6
|
+
message?: string;
|
|
7
|
+
}> {
|
|
8
|
+
}
|
|
9
|
+
export {};
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __extends = (this && this.__extends) || (function () {
|
|
3
|
+
var extendStatics = function (d, b) {
|
|
4
|
+
extendStatics = Object.setPrototypeOf ||
|
|
5
|
+
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
|
6
|
+
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
|
7
|
+
return extendStatics(d, b);
|
|
8
|
+
};
|
|
9
|
+
return function (d, b) {
|
|
10
|
+
if (typeof b !== "function" && b !== null)
|
|
11
|
+
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
|
12
|
+
extendStatics(d, b);
|
|
13
|
+
function __() { this.constructor = d; }
|
|
14
|
+
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
15
|
+
};
|
|
16
|
+
})();
|
|
17
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
|
+
exports.FetchError = void 0;
|
|
19
|
+
var Data_1 = require("effect/Data");
|
|
20
|
+
var FetchError = /** @class */ (function (_super) {
|
|
21
|
+
__extends(FetchError, _super);
|
|
22
|
+
function FetchError() {
|
|
23
|
+
return _super !== null && _super.apply(this, arguments) || this;
|
|
24
|
+
}
|
|
25
|
+
return FetchError;
|
|
26
|
+
}((0, Data_1.TaggedError)('FetchError')));
|
|
27
|
+
exports.FetchError = FetchError;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
declare const FileError_base: new <A extends Record<string, any> = {}>(args: import("effect/Types").Equals<A, {}> extends true ? void : { readonly [P in keyof A as P extends "_tag" ? never : P]: A[P]; }) => import("effect/Cause").YieldableError & {
|
|
2
|
+
readonly _tag: "FileError";
|
|
3
|
+
} & Readonly<A>;
|
|
4
|
+
export declare class FileError extends FileError_base<{
|
|
5
|
+
cause?: unknown;
|
|
6
|
+
message?: string;
|
|
7
|
+
}> {
|
|
8
|
+
}
|
|
9
|
+
export {};
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __extends = (this && this.__extends) || (function () {
|
|
3
|
+
var extendStatics = function (d, b) {
|
|
4
|
+
extendStatics = Object.setPrototypeOf ||
|
|
5
|
+
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
|
6
|
+
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
|
7
|
+
return extendStatics(d, b);
|
|
8
|
+
};
|
|
9
|
+
return function (d, b) {
|
|
10
|
+
if (typeof b !== "function" && b !== null)
|
|
11
|
+
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
|
12
|
+
extendStatics(d, b);
|
|
13
|
+
function __() { this.constructor = d; }
|
|
14
|
+
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
15
|
+
};
|
|
16
|
+
})();
|
|
17
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
|
+
exports.FileError = void 0;
|
|
19
|
+
var Data_1 = require("effect/Data");
|
|
20
|
+
var FileError = /** @class */ (function (_super) {
|
|
21
|
+
__extends(FileError, _super);
|
|
22
|
+
function FileError() {
|
|
23
|
+
return _super !== null && _super.apply(this, arguments) || this;
|
|
24
|
+
}
|
|
25
|
+
return FileError;
|
|
26
|
+
}((0, Data_1.TaggedError)('FileError')));
|
|
27
|
+
exports.FileError = FileError;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
declare const UserNotFoundError_base: new <A extends Record<string, any> = {}>(args: import("effect/Types").Equals<A, {}> extends true ? void : { readonly [P in keyof A as P extends "_tag" ? never : P]: A[P]; }) => import("effect/Cause").YieldableError & {
|
|
2
|
+
readonly _tag: "UserNotFound";
|
|
3
|
+
} & Readonly<A>;
|
|
4
|
+
export declare class UserNotFoundError extends UserNotFoundError_base<{
|
|
5
|
+
cause?: unknown;
|
|
6
|
+
message?: string;
|
|
7
|
+
}> {
|
|
8
|
+
}
|
|
9
|
+
export {};
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __extends = (this && this.__extends) || (function () {
|
|
3
|
+
var extendStatics = function (d, b) {
|
|
4
|
+
extendStatics = Object.setPrototypeOf ||
|
|
5
|
+
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
|
6
|
+
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
|
7
|
+
return extendStatics(d, b);
|
|
8
|
+
};
|
|
9
|
+
return function (d, b) {
|
|
10
|
+
if (typeof b !== "function" && b !== null)
|
|
11
|
+
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
|
12
|
+
extendStatics(d, b);
|
|
13
|
+
function __() { this.constructor = d; }
|
|
14
|
+
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
15
|
+
};
|
|
16
|
+
})();
|
|
17
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
|
+
exports.UserNotFoundError = void 0;
|
|
19
|
+
var Data_1 = require("effect/Data");
|
|
20
|
+
var UserNotFoundError = /** @class */ (function (_super) {
|
|
21
|
+
__extends(UserNotFoundError, _super);
|
|
22
|
+
function UserNotFoundError() {
|
|
23
|
+
return _super !== null && _super.apply(this, arguments) || this;
|
|
24
|
+
}
|
|
25
|
+
return UserNotFoundError;
|
|
26
|
+
}((0, Data_1.TaggedError)('UserNotFound')));
|
|
27
|
+
exports.UserNotFoundError = UserNotFoundError;
|
|
@@ -0,0 +1,113 @@
|
|
|
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;
|
|
13
|
+
return g = { next: verb(0), "throw": verb(1), "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
|
+
var __values = (this && this.__values) || function(o) {
|
|
39
|
+
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
|
|
40
|
+
if (m) return m.call(o);
|
|
41
|
+
if (o && typeof o.length === "number") return {
|
|
42
|
+
next: function () {
|
|
43
|
+
if (o && i >= o.length) o = void 0;
|
|
44
|
+
return { value: o && o[i++], done: !o };
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
|
|
48
|
+
};
|
|
49
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
50
|
+
exports.fromPromiseTask = void 0;
|
|
51
|
+
var effect_1 = require("effect");
|
|
52
|
+
var fs_extra_1 = require("fs-extra");
|
|
53
|
+
var fetch_error_1 = require("./errors/fetch-error");
|
|
54
|
+
var file_error_1 = require("./errors/file-error");
|
|
55
|
+
var filename_effect_1 = require("./util/filename.effect");
|
|
56
|
+
var readUser = effect_1.Effect.withSpan('readUser')(effect_1.Effect.tryPromise({
|
|
57
|
+
try: function () { return __awaiter(void 0, void 0, void 0, function () { return __generator(this, function (_a) {
|
|
58
|
+
switch (_a.label) {
|
|
59
|
+
case 0: return [4 /*yield*/, (0, fs_extra_1.readJson)('./src/examples/data/user.json')];
|
|
60
|
+
case 1: return [2 /*return*/, _a.sent()];
|
|
61
|
+
}
|
|
62
|
+
}); }); },
|
|
63
|
+
catch: function (e) { return new file_error_1.FileError({ cause: e }); },
|
|
64
|
+
}));
|
|
65
|
+
var fetchTask = function (userId) {
|
|
66
|
+
return effect_1.Effect.withSpan('fetchUser', {
|
|
67
|
+
attributes: {
|
|
68
|
+
userId: userId,
|
|
69
|
+
},
|
|
70
|
+
})(effect_1.Effect.tryPromise({
|
|
71
|
+
try: function () { return __awaiter(void 0, void 0, void 0, function () { return __generator(this, function (_a) {
|
|
72
|
+
switch (_a.label) {
|
|
73
|
+
case 0: return [4 /*yield*/, fetch("https://yolo-bro-oh-no.org/users/".concat(userId))];
|
|
74
|
+
case 1: return [2 /*return*/, _a.sent()];
|
|
75
|
+
}
|
|
76
|
+
}); }); },
|
|
77
|
+
catch: function (e) {
|
|
78
|
+
return new fetch_error_1.FetchError({
|
|
79
|
+
cause: e,
|
|
80
|
+
});
|
|
81
|
+
},
|
|
82
|
+
}));
|
|
83
|
+
};
|
|
84
|
+
var unwrapResponseTask = function (response) {
|
|
85
|
+
return effect_1.Effect.withSpan('unwrapFetchUserResponse')(effect_1.Effect.tryPromise({
|
|
86
|
+
try: function () { return __awaiter(void 0, void 0, void 0, function () { return __generator(this, function (_a) {
|
|
87
|
+
switch (_a.label) {
|
|
88
|
+
case 0: return [4 /*yield*/, response.json()];
|
|
89
|
+
case 1: return [2 /*return*/, _a.sent()];
|
|
90
|
+
}
|
|
91
|
+
}); }); },
|
|
92
|
+
catch: function (e) { return new fetch_error_1.FetchError({ cause: e }); },
|
|
93
|
+
}));
|
|
94
|
+
};
|
|
95
|
+
exports.fromPromiseTask = effect_1.Effect.withSpan('fromPromiseTask')(effect_1.Effect.gen(function (_) {
|
|
96
|
+
var id, response;
|
|
97
|
+
return __generator(this, function (_a) {
|
|
98
|
+
switch (_a.label) {
|
|
99
|
+
case 0: return [5 /*yield**/, __values(_((0, filename_effect_1.filename)(__filename)))];
|
|
100
|
+
case 1:
|
|
101
|
+
_a.sent();
|
|
102
|
+
return [5 /*yield**/, __values(_(readUser))];
|
|
103
|
+
case 2:
|
|
104
|
+
id = (_a.sent()).id;
|
|
105
|
+
return [5 /*yield**/, __values(_(fetchTask(id)))];
|
|
106
|
+
case 3:
|
|
107
|
+
response = _a.sent();
|
|
108
|
+
return [5 /*yield**/, __values(_(unwrapResponseTask(response)))];
|
|
109
|
+
case 4: return [2 /*return*/, _a.sent()];
|
|
110
|
+
}
|
|
111
|
+
});
|
|
112
|
+
}));
|
|
113
|
+
exports.default = exports.fromPromiseTask;
|
|
@@ -0,0 +1,69 @@
|
|
|
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;
|
|
13
|
+
return g = { next: verb(0), "throw": verb(1), "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.withParallelErrorsTask = void 0;
|
|
40
|
+
var effect_1 = require("effect");
|
|
41
|
+
var user_not_found_error_1 = require("./errors/user-not-found.error");
|
|
42
|
+
var filename_effect_1 = require("./util/filename.effect");
|
|
43
|
+
var readUser = function (name) {
|
|
44
|
+
return effect_1.Effect.withSpan('readUser', {
|
|
45
|
+
attributes: {
|
|
46
|
+
name: name,
|
|
47
|
+
},
|
|
48
|
+
})(effect_1.Effect.tryPromise({
|
|
49
|
+
// eslint-disable-next-line prefer-promise-reject-errors
|
|
50
|
+
try: function () { return __awaiter(void 0, void 0, void 0, function () { return __generator(this, function (_a) {
|
|
51
|
+
switch (_a.label) {
|
|
52
|
+
case 0: return [4 /*yield*/, Promise.reject('Oh no, this user does no exist!')];
|
|
53
|
+
case 1: return [2 /*return*/, _a.sent()];
|
|
54
|
+
}
|
|
55
|
+
}); }); },
|
|
56
|
+
catch: function (e) { return new user_not_found_error_1.UserNotFoundError({ cause: e }); },
|
|
57
|
+
}));
|
|
58
|
+
};
|
|
59
|
+
var parallelGet = function (names) {
|
|
60
|
+
return effect_1.Effect.withSpan('parallelGet', {
|
|
61
|
+
attributes: {
|
|
62
|
+
names: names,
|
|
63
|
+
},
|
|
64
|
+
})(effect_1.Effect.all(names.map(readUser), {
|
|
65
|
+
concurrency: 'unbounded',
|
|
66
|
+
}));
|
|
67
|
+
};
|
|
68
|
+
exports.withParallelErrorsTask = effect_1.Effect.withSpan('withParallelErrorsTask')(effect_1.Effect.all([(0, filename_effect_1.filename)(__filename), parallelGet(['yolo', 'bro', 'cool'])]));
|
|
69
|
+
exports.default = exports.withParallelErrorsTask;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.filename = void 0;
|
|
4
|
+
var effect_1 = require("effect");
|
|
5
|
+
var cwdRegex = new RegExp(process.cwd(), 'g');
|
|
6
|
+
var filename = function (path) {
|
|
7
|
+
return effect_1.Effect.sync(function () {
|
|
8
|
+
console.info("\r\n\uD83D\uDCC1 ".concat(path.replace(cwdRegex, '.')));
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
exports.filename = filename;
|
package/index.d.ts
CHANGED
package/index.js
CHANGED
|
@@ -18,3 +18,4 @@ __exportStar(require("./pretty-print"), exports);
|
|
|
18
18
|
__exportStar(require("./runners/run-promise"), exports);
|
|
19
19
|
__exportStar(require("./runners/run-sync"), exports);
|
|
20
20
|
__exportStar(require("./types/pretty-print-options.type"), exports);
|
|
21
|
+
__exportStar(require("./capture-errors"), exports);
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import { Cause } from 'effect/Cause';
|
|
2
|
-
import { PrettyError } from '../../types/pretty-error.type';
|
|
1
|
+
import { type Cause } from 'effect/Cause';
|
|
2
|
+
import { type PrettyError } from '../../types/pretty-error.type';
|
|
3
3
|
export declare const captureErrorsFrom: <E>(cause: Cause<E>) => readonly PrettyError[];
|
|
@@ -29,7 +29,7 @@ exports.captureErrorsFrom = void 0;
|
|
|
29
29
|
var Cause_1 = require("effect/Cause");
|
|
30
30
|
var parse_error_1 = require("./parse-error");
|
|
31
31
|
var captureErrorsFrom = function (cause) {
|
|
32
|
-
return (0, Cause_1.reduceWithContext)(cause,
|
|
32
|
+
return (0, Cause_1.reduceWithContext)(cause, undefined, {
|
|
33
33
|
emptyCase: function () { return []; },
|
|
34
34
|
dieCase: function (_, unknownError) { return [(0, parse_error_1.parseError)(unknownError)]; },
|
|
35
35
|
failCase: function (_, error) { return [(0, parse_error_1.parseError)(error)]; },
|