effect-errors 1.2.24 → 1.3.0
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 +154 -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 +89 -0
- package/examples/parallel-errors.d.ts +4 -0
- package/examples/parallel-errors.js +27 -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/extract-error-details.d.ts +7 -0
- package/logic/errors/extract-error-details.js +70 -0
- package/logic/errors/parse-error.js +4 -3
- package/package.json +9 -9
- package/pretty-print.js +33 -23
- 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 +8 -0
- package/types/pretty-error.type.d.ts +4 -9
- 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 { Cause } from 'effect/Cause';
|
|
2
|
+
import { 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) {
|
|
18
|
+
var current = span;
|
|
19
|
+
while (current && 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,154 @@
|
|
|
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
|
+
(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).toBeUndefined();
|
|
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[0].name).toBe('fetchUser');
|
|
65
|
+
(0, vitest_1.expect)(spans[0].attributes).toHaveAttributes([
|
|
66
|
+
{
|
|
67
|
+
key: 'userId',
|
|
68
|
+
value: '123',
|
|
69
|
+
},
|
|
70
|
+
]);
|
|
71
|
+
(0, vitest_1.expect)(spans[1].attributes).toHaveAttributes([]);
|
|
72
|
+
(0, vitest_1.expect)(stack).not.toHaveLength(0);
|
|
73
|
+
return [2 /*return*/];
|
|
74
|
+
}
|
|
75
|
+
});
|
|
76
|
+
}); });
|
|
77
|
+
(0, vitest_1.it)('should capture parallel errors', function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
78
|
+
var cause, result, firstError, secondError, thirdError;
|
|
79
|
+
return __generator(this, function (_a) {
|
|
80
|
+
switch (_a.label) {
|
|
81
|
+
case 0: return [4 /*yield*/, (0, effect_cause_1.effectCause)(parallel_errors_1.withParallelErrorsTask)];
|
|
82
|
+
case 1:
|
|
83
|
+
cause = _a.sent();
|
|
84
|
+
result = (0, capture_errors_1.captureErrors)(cause);
|
|
85
|
+
(0, vitest_1.expect)(result.interrupted).toBe(false);
|
|
86
|
+
(0, vitest_1.expect)(result.errors).toHaveLength(3);
|
|
87
|
+
firstError = result.errors[0];
|
|
88
|
+
(0, vitest_1.expect)(firstError.errorType).toBe('UserNotFound');
|
|
89
|
+
(0, vitest_1.expect)(firstError.isPlainString).toBeUndefined();
|
|
90
|
+
(0, vitest_1.expect)(firstError.message).toStrictEqual('Oh no, this user does no exist!');
|
|
91
|
+
(0, vitest_1.expect)(firstError.spans).toHaveLength(3);
|
|
92
|
+
(0, vitest_1.expect)(firstError.spans[0].name).toBe('readUser');
|
|
93
|
+
(0, vitest_1.expect)(firstError.spans[0].attributes).toHaveAttributes([
|
|
94
|
+
{
|
|
95
|
+
key: 'name',
|
|
96
|
+
value: 'yolo',
|
|
97
|
+
},
|
|
98
|
+
]);
|
|
99
|
+
(0, vitest_1.expect)(firstError.spans[1].name).toBe('parallelGet');
|
|
100
|
+
(0, vitest_1.expect)(firstError.spans[1].attributes).toHaveAttributes([
|
|
101
|
+
{
|
|
102
|
+
key: 'names',
|
|
103
|
+
value: ['yolo', 'bro', 'cool'],
|
|
104
|
+
},
|
|
105
|
+
]);
|
|
106
|
+
(0, vitest_1.expect)(firstError.spans[2].name).toBe('withParallelErrorsTask');
|
|
107
|
+
(0, vitest_1.expect)(firstError.spans[2].attributes).toHaveAttributes([]);
|
|
108
|
+
secondError = result.errors[1];
|
|
109
|
+
(0, vitest_1.expect)(secondError.errorType).toBe('UserNotFound');
|
|
110
|
+
(0, vitest_1.expect)(secondError.isPlainString).toBeUndefined();
|
|
111
|
+
(0, vitest_1.expect)(secondError.message).toStrictEqual('Oh no, this user does no exist!');
|
|
112
|
+
(0, vitest_1.expect)(secondError.spans).toHaveLength(3);
|
|
113
|
+
(0, vitest_1.expect)(secondError.spans[0].name).toBe('readUser');
|
|
114
|
+
(0, vitest_1.expect)(secondError.spans[0].attributes).toHaveAttributes([
|
|
115
|
+
{
|
|
116
|
+
key: 'name',
|
|
117
|
+
value: 'bro',
|
|
118
|
+
},
|
|
119
|
+
]);
|
|
120
|
+
(0, vitest_1.expect)(secondError.spans[1].name).toBe('parallelGet');
|
|
121
|
+
(0, vitest_1.expect)(secondError.spans[1].attributes).toHaveAttributes([
|
|
122
|
+
{
|
|
123
|
+
key: 'names',
|
|
124
|
+
value: ['yolo', 'bro', 'cool'],
|
|
125
|
+
},
|
|
126
|
+
]);
|
|
127
|
+
(0, vitest_1.expect)(secondError.spans[2].name).toBe('withParallelErrorsTask');
|
|
128
|
+
(0, vitest_1.expect)(secondError.spans[2].attributes).toHaveAttributes([]);
|
|
129
|
+
thirdError = result.errors[2];
|
|
130
|
+
(0, vitest_1.expect)(thirdError.errorType).toBe('UserNotFound');
|
|
131
|
+
(0, vitest_1.expect)(thirdError.isPlainString).toBeUndefined();
|
|
132
|
+
(0, vitest_1.expect)(thirdError.message).toStrictEqual('Oh no, this user does no exist!');
|
|
133
|
+
(0, vitest_1.expect)(thirdError.spans).toHaveLength(3);
|
|
134
|
+
(0, vitest_1.expect)(thirdError.spans[0].name).toBe('readUser');
|
|
135
|
+
(0, vitest_1.expect)(thirdError.spans[0].attributes).toHaveAttributes([
|
|
136
|
+
{
|
|
137
|
+
key: 'name',
|
|
138
|
+
value: 'cool',
|
|
139
|
+
},
|
|
140
|
+
]);
|
|
141
|
+
(0, vitest_1.expect)(thirdError.spans[1].name).toBe('parallelGet');
|
|
142
|
+
(0, vitest_1.expect)(thirdError.spans[1].attributes).toHaveAttributes([
|
|
143
|
+
{
|
|
144
|
+
key: 'names',
|
|
145
|
+
value: ['yolo', 'bro', 'cool'],
|
|
146
|
+
},
|
|
147
|
+
]);
|
|
148
|
+
(0, vitest_1.expect)(thirdError.spans[2].name).toBe('withParallelErrorsTask');
|
|
149
|
+
(0, vitest_1.expect)(thirdError.spans[2].attributes).toHaveAttributes([]);
|
|
150
|
+
return [2 /*return*/];
|
|
151
|
+
}
|
|
152
|
+
});
|
|
153
|
+
}); });
|
|
154
|
+
});
|
|
@@ -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,89 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
3
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
4
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
5
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
6
|
+
function step(op) {
|
|
7
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
8
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
9
|
+
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;
|
|
10
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
11
|
+
switch (op[0]) {
|
|
12
|
+
case 0: case 1: t = op; break;
|
|
13
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
14
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
15
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
16
|
+
default:
|
|
17
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
18
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
19
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
20
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
21
|
+
if (t[2]) _.ops.pop();
|
|
22
|
+
_.trys.pop(); continue;
|
|
23
|
+
}
|
|
24
|
+
op = body.call(thisArg, _);
|
|
25
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
26
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
var __values = (this && this.__values) || function(o) {
|
|
30
|
+
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
|
|
31
|
+
if (m) return m.call(o);
|
|
32
|
+
if (o && typeof o.length === "number") return {
|
|
33
|
+
next: function () {
|
|
34
|
+
if (o && i >= o.length) o = void 0;
|
|
35
|
+
return { value: o && o[i++], done: !o };
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
|
|
39
|
+
};
|
|
40
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
41
|
+
exports.fromPromiseTask = void 0;
|
|
42
|
+
var effect_1 = require("effect");
|
|
43
|
+
var fs_extra_1 = require("fs-extra");
|
|
44
|
+
var fetch_error_1 = require("./errors/fetch-error");
|
|
45
|
+
var file_error_1 = require("./errors/file-error");
|
|
46
|
+
var filename_effect_1 = require("./util/filename.effect");
|
|
47
|
+
var readUser = effect_1.Effect.withSpan('readUser')(effect_1.Effect.tryPromise({
|
|
48
|
+
try: function () { return (0, fs_extra_1.readJson)('./src/examples/data/user.json'); },
|
|
49
|
+
catch: function (e) { return new file_error_1.FileError({ cause: e }); },
|
|
50
|
+
}));
|
|
51
|
+
var fetchTask = function (userId) {
|
|
52
|
+
return effect_1.Effect.withSpan('fetchUser', {
|
|
53
|
+
attributes: {
|
|
54
|
+
userId: userId,
|
|
55
|
+
},
|
|
56
|
+
})(effect_1.Effect.tryPromise({
|
|
57
|
+
try: function () { return fetch("https://yolo-bro-oh-no.org/users/".concat(userId)); },
|
|
58
|
+
catch: function (e) {
|
|
59
|
+
return new fetch_error_1.FetchError({
|
|
60
|
+
cause: e,
|
|
61
|
+
});
|
|
62
|
+
},
|
|
63
|
+
}));
|
|
64
|
+
};
|
|
65
|
+
var unwrapResponseTask = function (response) {
|
|
66
|
+
return effect_1.Effect.withSpan('unwrapFetchUserResponse')(effect_1.Effect.tryPromise({
|
|
67
|
+
try: function () { return response.json(); },
|
|
68
|
+
catch: function (e) { return new fetch_error_1.FetchError({ cause: e }); },
|
|
69
|
+
}));
|
|
70
|
+
};
|
|
71
|
+
exports.fromPromiseTask = effect_1.Effect.withSpan('fromPromiseTask')(effect_1.Effect.gen(function (_) {
|
|
72
|
+
var id, response;
|
|
73
|
+
return __generator(this, function (_a) {
|
|
74
|
+
switch (_a.label) {
|
|
75
|
+
case 0: return [5 /*yield**/, __values(_((0, filename_effect_1.filename)(__filename)))];
|
|
76
|
+
case 1:
|
|
77
|
+
_a.sent();
|
|
78
|
+
return [5 /*yield**/, __values(_(readUser))];
|
|
79
|
+
case 2:
|
|
80
|
+
id = (_a.sent()).id;
|
|
81
|
+
return [5 /*yield**/, __values(_(fetchTask(id)))];
|
|
82
|
+
case 3:
|
|
83
|
+
response = _a.sent();
|
|
84
|
+
return [5 /*yield**/, __values(_(unwrapResponseTask(response)))];
|
|
85
|
+
case 4: return [2 /*return*/, _a.sent()];
|
|
86
|
+
}
|
|
87
|
+
});
|
|
88
|
+
}));
|
|
89
|
+
exports.default = exports.fromPromiseTask;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.withParallelErrorsTask = void 0;
|
|
4
|
+
var effect_1 = require("effect");
|
|
5
|
+
var user_not_found_error_1 = require("./errors/user-not-found.error");
|
|
6
|
+
var filename_effect_1 = require("./util/filename.effect");
|
|
7
|
+
var readUser = function (name) {
|
|
8
|
+
return effect_1.Effect.withSpan('readUser', {
|
|
9
|
+
attributes: {
|
|
10
|
+
name: name,
|
|
11
|
+
},
|
|
12
|
+
})(effect_1.Effect.tryPromise({
|
|
13
|
+
try: function () { return Promise.reject('Oh no, this user does no exist!'); },
|
|
14
|
+
catch: function (e) { return new user_not_found_error_1.UserNotFoundError({ cause: e }); },
|
|
15
|
+
}));
|
|
16
|
+
};
|
|
17
|
+
var parallelGet = function (names) {
|
|
18
|
+
return effect_1.Effect.withSpan('parallelGet', {
|
|
19
|
+
attributes: {
|
|
20
|
+
names: names,
|
|
21
|
+
},
|
|
22
|
+
})(effect_1.Effect.all(names.map(readUser), {
|
|
23
|
+
concurrency: 'unbounded',
|
|
24
|
+
}));
|
|
25
|
+
};
|
|
26
|
+
exports.withParallelErrorsTask = effect_1.Effect.withSpan('withParallelErrorsTask')(effect_1.Effect.all([(0, filename_effect_1.filename)(__filename), parallelGet(['yolo', 'bro', 'cool'])]));
|
|
27
|
+
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);
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __read = (this && this.__read) || function (o, n) {
|
|
3
|
+
var m = typeof Symbol === "function" && o[Symbol.iterator];
|
|
4
|
+
if (!m) return o;
|
|
5
|
+
var i = m.call(o), r, ar = [], e;
|
|
6
|
+
try {
|
|
7
|
+
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
|
|
8
|
+
}
|
|
9
|
+
catch (error) { e = { error: error }; }
|
|
10
|
+
finally {
|
|
11
|
+
try {
|
|
12
|
+
if (r && !r.done && (m = i["return"])) m.call(i);
|
|
13
|
+
}
|
|
14
|
+
finally { if (e) throw e.error; }
|
|
15
|
+
}
|
|
16
|
+
return ar;
|
|
17
|
+
};
|
|
18
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
19
|
+
exports.extractErrorDetails = void 0;
|
|
20
|
+
var Function_1 = require("effect/Function");
|
|
21
|
+
var Predicate_1 = require("effect/Predicate");
|
|
22
|
+
var extractErrorDetails = function (error) {
|
|
23
|
+
if (typeof error === 'string') {
|
|
24
|
+
return {
|
|
25
|
+
isPlainString: true,
|
|
26
|
+
message: error,
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
// TaggedError with cause
|
|
30
|
+
if (error instanceof Error &&
|
|
31
|
+
(0, Predicate_1.hasProperty)(error, 'cause') &&
|
|
32
|
+
(0, Predicate_1.hasProperty)(error, '_tag')) {
|
|
33
|
+
return {
|
|
34
|
+
type: error._tag,
|
|
35
|
+
message: error.cause,
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
// TaggedError with error ctor
|
|
39
|
+
if (error instanceof Error && (0, Predicate_1.hasProperty)(error, 'error')) {
|
|
40
|
+
return {
|
|
41
|
+
type: error.name,
|
|
42
|
+
message: error.error,
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
// Plain objects with tag attribute
|
|
46
|
+
if ((0, Predicate_1.hasProperty)(error, '_tag') && (0, Predicate_1.hasProperty)(error, 'message')) {
|
|
47
|
+
return {
|
|
48
|
+
type: error._tag,
|
|
49
|
+
message: error.message,
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
// Plain objects with toString impl
|
|
53
|
+
if ((0, Predicate_1.hasProperty)(error, 'toString') &&
|
|
54
|
+
(0, Function_1.isFunction)(error['toString']) &&
|
|
55
|
+
error['toString'] !== Object.prototype.toString &&
|
|
56
|
+
error['toString'] !== Array.prototype.toString) {
|
|
57
|
+
var message = error.toString();
|
|
58
|
+
var maybeWithUnderlyingType = message.split(': ');
|
|
59
|
+
if (maybeWithUnderlyingType.length > 1) {
|
|
60
|
+
var _a = __read(maybeWithUnderlyingType), type = _a[0], message_1 = _a.slice(1);
|
|
61
|
+
return {
|
|
62
|
+
type: type,
|
|
63
|
+
message: message_1,
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
return { message: message };
|
|
67
|
+
}
|
|
68
|
+
return { message: "Error: ".concat(JSON.stringify(error)) };
|
|
69
|
+
};
|
|
70
|
+
exports.extractErrorDetails = extractErrorDetails;
|
|
@@ -3,14 +3,15 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.parseError = void 0;
|
|
4
4
|
var Predicate_1 = require("effect/Predicate");
|
|
5
5
|
var pretty_error_type_1 = require("../../types/pretty-error.type");
|
|
6
|
-
var
|
|
6
|
+
var extract_error_details_1 = require("./extract-error-details");
|
|
7
7
|
var spanSymbol = Symbol.for('effect/SpanAnnotation');
|
|
8
8
|
var parseError = function (error) {
|
|
9
9
|
var _a;
|
|
10
10
|
var span = ((0, Predicate_1.hasProperty)(error, spanSymbol) && error[spanSymbol]);
|
|
11
|
+
var _b = (0, extract_error_details_1.extractErrorDetails)(error), message = _b.message, type = _b.type, isPlainString = _b.isPlainString;
|
|
11
12
|
if (error instanceof Error) {
|
|
12
|
-
return new pretty_error_type_1.PrettyError(
|
|
13
|
+
return new pretty_error_type_1.PrettyError(message, (_a = error.stack) === null || _a === void 0 ? void 0 : _a.split('\n').filter(function (el) { return /at (.*)/.exec(el); }).join('\r\n'), span, type);
|
|
13
14
|
}
|
|
14
|
-
return new pretty_error_type_1.PrettyError(
|
|
15
|
+
return new pretty_error_type_1.PrettyError(message, void 0, span, type, isPlainString);
|
|
15
16
|
};
|
|
16
17
|
exports.parseError = parseError;
|
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"repository": "https://github.com/jpb06/effect-errors.git",
|
|
3
3
|
"main": "index.js",
|
|
4
4
|
"name": "effect-errors",
|
|
5
|
-
"version": "1.
|
|
5
|
+
"version": "1.3.0",
|
|
6
6
|
"author": "jpb06 <jp.bois.06@outlook.fr>",
|
|
7
7
|
"description": "A POC for errors reporting in Effect",
|
|
8
8
|
"keywords": [],
|
|
@@ -27,17 +27,17 @@
|
|
|
27
27
|
"run-examples": "bun run ./src/examples/util/run-all"
|
|
28
28
|
},
|
|
29
29
|
"dependencies": {
|
|
30
|
-
"@effect/schema": "^0.64.
|
|
30
|
+
"@effect/schema": "^0.64.19",
|
|
31
31
|
"chalk": "<5",
|
|
32
|
-
"effect": "^2.4.
|
|
32
|
+
"effect": "^2.4.18"
|
|
33
33
|
},
|
|
34
34
|
"devDependencies": {
|
|
35
35
|
"@stylistic/eslint-plugin": "^1.7.0",
|
|
36
|
-
"@types/eslint": "^8.56.
|
|
36
|
+
"@types/eslint": "^8.56.7",
|
|
37
37
|
"@types/fs-extra": "^11.0.4",
|
|
38
|
-
"@types/node": "^20.
|
|
39
|
-
"@typescript-eslint/eslint-plugin": "^7.
|
|
40
|
-
"@typescript-eslint/parser": "^7.
|
|
38
|
+
"@types/node": "^20.12.5",
|
|
39
|
+
"@typescript-eslint/eslint-plugin": "^7.5.0",
|
|
40
|
+
"@typescript-eslint/parser": "^7.5.0",
|
|
41
41
|
"@vitest/coverage-v8": "^1.4.0",
|
|
42
42
|
"copyfiles": "^2.4.1",
|
|
43
43
|
"del-cli": "^5.1.0",
|
|
@@ -47,11 +47,11 @@
|
|
|
47
47
|
"eslint-plugin-import": "^2.29.1",
|
|
48
48
|
"eslint-plugin-markdown": "^4.0.1",
|
|
49
49
|
"eslint-plugin-prettier": "^5.1.3",
|
|
50
|
-
"eslint-plugin-vitest": "^0.
|
|
50
|
+
"eslint-plugin-vitest": "^0.4.1",
|
|
51
51
|
"fs-extra": "^11.2.0",
|
|
52
52
|
"prettier": "^3.2.5",
|
|
53
53
|
"readme-package-icons": "^1.1.14",
|
|
54
|
-
"typescript": "^5.4.
|
|
54
|
+
"typescript": "^5.4.4",
|
|
55
55
|
"vitest": "^1.4.0",
|
|
56
56
|
"vitest-mock-extended": "^1.3.1"
|
|
57
57
|
}
|
package/pretty-print.js
CHANGED
|
@@ -23,7 +23,17 @@ var prettyPrint = function (cause, _a) {
|
|
|
23
23
|
console.error("\r\n\uD83E\uDEE0 ".concat(chalk_1.default.bold.yellowBright.underline("".concat(failures.length, " error").concat(failures.length > 1 ? 's' : '', " occurred\r\n"))));
|
|
24
24
|
return failures
|
|
25
25
|
.map(function (_a, failuresIndex) {
|
|
26
|
-
var
|
|
26
|
+
var errorType = _a.errorType, errorMessage = _a.message, stack = _a.stack, span = _a.span, isPlainString = _a.isPlainString;
|
|
27
|
+
var message = '💥 ' +
|
|
28
|
+
(failures.length > 1
|
|
29
|
+
? chalk_1.default.bgRed.whiteBright(" #".concat(failuresIndex + 1, " -"))
|
|
30
|
+
: '') +
|
|
31
|
+
chalk_1.default.bgRed.whiteBright(" ".concat(errorType !== null && errorType !== void 0 ? errorType : 'Unknown error', " ")) +
|
|
32
|
+
chalk_1.default.bold.whiteBright(" \u2022 ".concat(errorMessage)) +
|
|
33
|
+
'\r\n';
|
|
34
|
+
if (isPlainString === true) {
|
|
35
|
+
message += "\r\n".concat(chalk_1.default.gray('ℹ️ You used a plain string to represent a failure in the error channel (E). You should consider using tagged objects (with a _tag field), or yieldable errors such as Data.TaggedError and Schema.TaggedError for better handling experience.'));
|
|
36
|
+
}
|
|
27
37
|
if (span) {
|
|
28
38
|
var current = span;
|
|
29
39
|
var spans_1 = [];
|
|
@@ -31,32 +41,32 @@ var prettyPrint = function (cause, _a) {
|
|
|
31
41
|
spans_1.push(current);
|
|
32
42
|
current = effect_1.Option.getOrUndefined(current.parent);
|
|
33
43
|
}
|
|
34
|
-
message
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
(0, get_span_attributes_1.getSpanAttributes)(attributes, isLastEntry));
|
|
54
|
-
})
|
|
55
|
-
.join('');
|
|
44
|
+
message += spans_1
|
|
45
|
+
.toReversed()
|
|
46
|
+
.map(function (_a, index) {
|
|
47
|
+
var name = _a.name, attributes = _a.attributes, status = _a.status;
|
|
48
|
+
var isFirstEntry = index === 0;
|
|
49
|
+
var isLastEntry = index === spans_1.length - 1;
|
|
50
|
+
var filePath = " at ".concat(stripCwd ? (0, strip_cwd_path_1.stripCwdPath)(name) : name);
|
|
51
|
+
return chalk_1.default.whiteBright((isFirstEntry ? "\r\n".concat(chalk_1.default.gray('◯')) : '') +
|
|
52
|
+
'\r\n' +
|
|
53
|
+
(0, spans_stack_trailing_char_1.spanStackTrailingChar)(isLastEntry) +
|
|
54
|
+
chalk_1.default.gray('─') +
|
|
55
|
+
filePath +
|
|
56
|
+
(0, get_span_duration_1.getSpanDuration)(status, isLastEntry) +
|
|
57
|
+
(0, get_span_attributes_1.getSpanAttributes)(attributes, isLastEntry));
|
|
58
|
+
})
|
|
59
|
+
.join('');
|
|
60
|
+
}
|
|
61
|
+
else if (!isPlainString) {
|
|
62
|
+
message += "\r\n".concat(chalk_1.default.gray('ℹ️ Consider using spans to improve errors reporting.\r\n'));
|
|
56
63
|
}
|
|
57
64
|
if (stack) {
|
|
58
65
|
message += "\r\n".concat(span ? '\r\n' : '', "\uD83D\uDEA8 Stacktrace\r\n").concat(chalk_1.default.red((0, filter_stack_1.filterStack)(stack, stripCwd === true)));
|
|
59
66
|
}
|
|
67
|
+
else if (!isPlainString) {
|
|
68
|
+
message += "\r\n\r\n".concat(chalk_1.default.gray('ℹ️ Consider using a yieldable error such as Data.TaggedError and Schema.TaggedError to get a stacktrace.'));
|
|
69
|
+
}
|
|
60
70
|
return message + '\r\n';
|
|
61
71
|
})
|
|
62
72
|
.join('\r\n');
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __assign = (this && this.__assign) || function () {
|
|
3
|
+
__assign = Object.assign || function(t) {
|
|
4
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
5
|
+
s = arguments[i];
|
|
6
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
7
|
+
t[p] = s[p];
|
|
8
|
+
}
|
|
9
|
+
return t;
|
|
10
|
+
};
|
|
11
|
+
return __assign.apply(this, arguments);
|
|
12
|
+
};
|
|
13
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
14
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
15
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
16
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
17
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
18
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
19
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
20
|
+
});
|
|
21
|
+
};
|
|
22
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
23
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
24
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
25
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
26
|
+
function step(op) {
|
|
27
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
28
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
29
|
+
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;
|
|
30
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
31
|
+
switch (op[0]) {
|
|
32
|
+
case 0: case 1: t = op; break;
|
|
33
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
34
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
35
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
36
|
+
default:
|
|
37
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
38
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
39
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
40
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
41
|
+
if (t[2]) _.ops.pop();
|
|
42
|
+
_.trys.pop(); continue;
|
|
43
|
+
}
|
|
44
|
+
op = body.call(thisArg, _);
|
|
45
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
46
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
50
|
+
exports.mockConsole = void 0;
|
|
51
|
+
var vitest_1 = require("vitest");
|
|
52
|
+
var mockConsole = function (args) { return __awaiter(void 0, void 0, void 0, function () {
|
|
53
|
+
var _a, _b;
|
|
54
|
+
return __generator(this, function (_c) {
|
|
55
|
+
switch (_c.label) {
|
|
56
|
+
case 0:
|
|
57
|
+
_a = global;
|
|
58
|
+
_b = [{}];
|
|
59
|
+
return [4 /*yield*/, vitest_1.vi.importActual('node:console')];
|
|
60
|
+
case 1:
|
|
61
|
+
_a.console = __assign.apply(void 0, [__assign.apply(void 0, _b.concat([(_c.sent())])), args]);
|
|
62
|
+
return [2 /*return*/];
|
|
63
|
+
}
|
|
64
|
+
});
|
|
65
|
+
}); };
|
|
66
|
+
exports.mockConsole = mockConsole;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.effectCause = void 0;
|
|
4
|
+
var effect_1 = require("effect");
|
|
5
|
+
var effectCause = function (effect) {
|
|
6
|
+
return effect_1.Effect.runPromise((0, effect_1.pipe)(effect, effect_1.Effect.catchAllCause(function (e) { return effect_1.Effect.fail(e); }), effect_1.Effect.flip));
|
|
7
|
+
};
|
|
8
|
+
exports.effectCause = effectCause;
|
|
@@ -1,14 +1,9 @@
|
|
|
1
1
|
import { Span } from 'effect/Tracer';
|
|
2
|
-
interface Error {
|
|
3
|
-
message: string;
|
|
4
|
-
stack?: string;
|
|
5
|
-
span?: Span;
|
|
6
|
-
}
|
|
7
2
|
export declare class PrettyError {
|
|
8
|
-
readonly message:
|
|
3
|
+
readonly message: unknown;
|
|
9
4
|
readonly stack: string | undefined;
|
|
10
5
|
readonly span: Span | undefined;
|
|
11
|
-
|
|
12
|
-
|
|
6
|
+
readonly errorType?: unknown;
|
|
7
|
+
readonly isPlainString?: boolean | undefined;
|
|
8
|
+
constructor(message: unknown, stack: string | undefined, span: Span | undefined, errorType?: unknown, isPlainString?: boolean | undefined);
|
|
13
9
|
}
|
|
14
|
-
export {};
|
|
@@ -2,21 +2,13 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.PrettyError = void 0;
|
|
4
4
|
var PrettyError = /** @class */ (function () {
|
|
5
|
-
function PrettyError(message, stack, span) {
|
|
5
|
+
function PrettyError(message, stack, span, errorType, isPlainString) {
|
|
6
6
|
this.message = message;
|
|
7
7
|
this.stack = stack;
|
|
8
8
|
this.span = span;
|
|
9
|
+
this.errorType = errorType;
|
|
10
|
+
this.isPlainString = isPlainString;
|
|
9
11
|
}
|
|
10
|
-
PrettyError.prototype.toJSON = function () {
|
|
11
|
-
var out = { message: this.message };
|
|
12
|
-
if (this.stack) {
|
|
13
|
-
out.stack = this.stack;
|
|
14
|
-
}
|
|
15
|
-
if (this.span) {
|
|
16
|
-
out.span = this.span;
|
|
17
|
-
}
|
|
18
|
-
return out;
|
|
19
|
-
};
|
|
20
12
|
return PrettyError;
|
|
21
13
|
}());
|
|
22
14
|
exports.PrettyError = PrettyError;
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare const prettyErrorMessage: (u: unknown) => string;
|
|
@@ -1,59 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __read = (this && this.__read) || function (o, n) {
|
|
3
|
-
var m = typeof Symbol === "function" && o[Symbol.iterator];
|
|
4
|
-
if (!m) return o;
|
|
5
|
-
var i = m.call(o), r, ar = [], e;
|
|
6
|
-
try {
|
|
7
|
-
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
|
|
8
|
-
}
|
|
9
|
-
catch (error) { e = { error: error }; }
|
|
10
|
-
finally {
|
|
11
|
-
try {
|
|
12
|
-
if (r && !r.done && (m = i["return"])) m.call(i);
|
|
13
|
-
}
|
|
14
|
-
finally { if (e) throw e.error; }
|
|
15
|
-
}
|
|
16
|
-
return ar;
|
|
17
|
-
};
|
|
18
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
19
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
20
|
-
};
|
|
21
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
22
|
-
exports.prettyErrorMessage = void 0;
|
|
23
|
-
var chalk_1 = __importDefault(require("chalk"));
|
|
24
|
-
var Function_1 = require("effect/Function");
|
|
25
|
-
var Predicate_1 = require("effect/Predicate");
|
|
26
|
-
var redBackground = function (text) { return chalk_1.default.bgRed(" ".concat(text, " ")); };
|
|
27
|
-
var whiteBright = function (text) { return chalk_1.default.bold.whiteBright("\u2022 ".concat(text)); };
|
|
28
|
-
var prettyErrorMessage = function (u) {
|
|
29
|
-
if (typeof u === 'string') {
|
|
30
|
-
return "".concat(u, "\r\n\r\n\u2139\uFE0F ").concat(chalk_1.default.gray('You used a plain string to represent a failure in the error channel (E). You should consider using tagged objects (with a _tag field), or yieldable errors such as Data.TaggedError and Schema.TaggedError for better handling experience.'));
|
|
31
|
-
}
|
|
32
|
-
// TaggedError with cause
|
|
33
|
-
if (u instanceof Error && (0, Predicate_1.hasProperty)(u, 'cause') && (0, Predicate_1.hasProperty)(u, '_tag')) {
|
|
34
|
-
return "".concat(redBackground(u._tag), " ").concat(whiteBright(u.cause), "\r\n");
|
|
35
|
-
}
|
|
36
|
-
// TaggedError with error ctor
|
|
37
|
-
if (u instanceof Error && (0, Predicate_1.hasProperty)(u, 'error')) {
|
|
38
|
-
return "".concat(redBackground(u.name), " ").concat(whiteBright(u.error), "\r\n");
|
|
39
|
-
}
|
|
40
|
-
// Plain objects with tag attribute
|
|
41
|
-
if ((0, Predicate_1.hasProperty)(u, '_tag') && (0, Predicate_1.hasProperty)(u, 'message')) {
|
|
42
|
-
return "".concat(redBackground(u._tag), " ").concat(whiteBright(u.message), "\r\n");
|
|
43
|
-
}
|
|
44
|
-
// Plain objects with toString impl
|
|
45
|
-
if ((0, Predicate_1.hasProperty)(u, 'toString') &&
|
|
46
|
-
(0, Function_1.isFunction)(u['toString']) &&
|
|
47
|
-
u['toString'] !== Object.prototype.toString &&
|
|
48
|
-
u['toString'] !== Array.prototype.toString) {
|
|
49
|
-
var message = u['toString']();
|
|
50
|
-
var maybeWithUnderlyingType = message.split(': ');
|
|
51
|
-
if (maybeWithUnderlyingType.length > 1) {
|
|
52
|
-
var _a = __read(maybeWithUnderlyingType), type = _a[0], message_1 = _a.slice(1);
|
|
53
|
-
return "".concat(redBackground(type), " ").concat(whiteBright(message_1));
|
|
54
|
-
}
|
|
55
|
-
return "".concat(message);
|
|
56
|
-
}
|
|
57
|
-
return "Error: ".concat(JSON.stringify(u));
|
|
58
|
-
};
|
|
59
|
-
exports.prettyErrorMessage = prettyErrorMessage;
|