effect-errors 1.1.5 → 1.2.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 +8 -2
- package/index.d.ts +1 -0
- package/index.js +1 -0
- package/logic/filter-stack.d.ts +1 -1
- package/logic/filter-stack.js +4 -2
- package/logic/pretty-error-message.js +6 -6
- package/logic/strip-cwd-path.d.ts +1 -0
- package/logic/strip-cwd-path.js +6 -0
- package/package.json +1 -1
- package/pretty-print.d.ts +2 -1
- package/pretty-print.js +27 -18
- package/runners/run-promise.d.ts +2 -1
- package/runners/run-promise.js +4 -2
- package/runners/run-sync.d.ts +2 -1
- package/runners/run-sync.js +4 -2
- package/types/pretty-print-options.type.d.ts +4 -0
- package/types/pretty-print-options.type.js +6 -0
package/README.md
CHANGED
|
@@ -41,9 +41,15 @@ import { prettyPrint } from 'effect-errors';
|
|
|
41
41
|
Signature is the following:
|
|
42
42
|
|
|
43
43
|
```typescript
|
|
44
|
-
const prettyPrint: <E>(cause: Cause<E
|
|
44
|
+
const prettyPrint: <E>(cause: Cause<E>, options?: PrettyPrintOptions) => string;
|
|
45
45
|
```
|
|
46
46
|
|
|
47
|
+
`PrettyPrintOptions` allows you to tweak the following:
|
|
48
|
+
|
|
49
|
+
#### `stripCwd` - Whether spans and stacktrace should contain absolute or relative paths
|
|
50
|
+
|
|
51
|
+
> default: `false` (absolute paths)
|
|
52
|
+
|
|
47
53
|
## ⚡ How should I raise errors?
|
|
48
54
|
|
|
49
55
|
The best way is to use either `SchemaError` or `TaggedError`.
|
|
@@ -97,7 +103,7 @@ Effect.fail(new UserNotFoundError({ cause: "User does not exist" }));
|
|
|
97
103
|
|
|
98
104
|
### 🔶 Plain object
|
|
99
105
|
|
|
100
|
-
|
|
106
|
+
Alternatively, you _can_ use a plain object with a `_tag` and `message` attribute, but you won't get any stacktrace if you use this method:
|
|
101
107
|
|
|
102
108
|
```typescript
|
|
103
109
|
Effect.fail({ _tag: 'SucksToBeMe', message: 'Yeah...' });
|
package/index.d.ts
CHANGED
package/index.js
CHANGED
|
@@ -17,3 +17,4 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
17
17
|
__exportStar(require("./pretty-print"), exports);
|
|
18
18
|
__exportStar(require("./runners/run-promise"), exports);
|
|
19
19
|
__exportStar(require("./runners/run-sync"), exports);
|
|
20
|
+
__exportStar(require("./types/pretty-print-options.type"), exports);
|
package/logic/filter-stack.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export declare const filterStack: (stack: string) => string;
|
|
1
|
+
export declare const filterStack: (stack: string, stripCwd: boolean) => string;
|
package/logic/filter-stack.js
CHANGED
|
@@ -12,7 +12,8 @@ var __values = (this && this.__values) || function(o) {
|
|
|
12
12
|
};
|
|
13
13
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
14
14
|
exports.filterStack = void 0;
|
|
15
|
-
var
|
|
15
|
+
var strip_cwd_path_1 = require("./strip-cwd-path");
|
|
16
|
+
var filterStack = function (stack, stripCwd) {
|
|
16
17
|
var e_1, _a;
|
|
17
18
|
var lines = stack.split('\n');
|
|
18
19
|
var out = [];
|
|
@@ -32,6 +33,7 @@ var filterStack = function (stack) {
|
|
|
32
33
|
}
|
|
33
34
|
finally { if (e_1) throw e_1.error; }
|
|
34
35
|
}
|
|
35
|
-
|
|
36
|
+
var final = out.join('\n').replace(/ {4}at /g, '🭳 at ');
|
|
37
|
+
return stripCwd ? (0, strip_cwd_path_1.stripCwdPath)(final) : final;
|
|
36
38
|
};
|
|
37
39
|
exports.filterStack = filterStack;
|
|
@@ -25,19 +25,19 @@ var Function_1 = require("effect/Function");
|
|
|
25
25
|
var Predicate_1 = require("effect/Predicate");
|
|
26
26
|
var prettyErrorMessage = function (u) {
|
|
27
27
|
if (typeof u === 'string') {
|
|
28
|
-
return "
|
|
28
|
+
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.'));
|
|
29
29
|
}
|
|
30
30
|
// TaggedError with cause
|
|
31
31
|
if (u instanceof Error && (0, Predicate_1.hasProperty)(u, 'cause') && (0, Predicate_1.hasProperty)(u, '_tag')) {
|
|
32
|
-
return "
|
|
32
|
+
return "".concat(chalk_1.default.bgRed(" ".concat(u._tag, " ")), " ").concat(chalk_1.default.bold.whiteBright("\u2022 ".concat(u.cause)), "\r\n");
|
|
33
33
|
}
|
|
34
34
|
// TaggedError with error ctor
|
|
35
35
|
if (u instanceof Error && (0, Predicate_1.hasProperty)(u, 'error')) {
|
|
36
|
-
return "
|
|
36
|
+
return "".concat(chalk_1.default.bgRed(" ".concat(u.name, " ")), " ").concat(chalk_1.default.bold.whiteBright("\u2022 ".concat(u.error)), "\r\n");
|
|
37
37
|
}
|
|
38
38
|
// Plain objects with tag attribute
|
|
39
39
|
if ((0, Predicate_1.hasProperty)(u, '_tag') && (0, Predicate_1.hasProperty)(u, 'message')) {
|
|
40
|
-
return "
|
|
40
|
+
return "".concat(chalk_1.default.bgRed(" ".concat(u._tag, " ")), " ").concat(chalk_1.default.bold.whiteBright("\u2022 ".concat(u.message)), "\r\n");
|
|
41
41
|
}
|
|
42
42
|
if ((0, Predicate_1.hasProperty)(u, 'toString') &&
|
|
43
43
|
(0, Function_1.isFunction)(u['toString']) &&
|
|
@@ -47,9 +47,9 @@ var prettyErrorMessage = function (u) {
|
|
|
47
47
|
var maybeWithUnderlyingType = message.split(': ');
|
|
48
48
|
if (maybeWithUnderlyingType.length > 1) {
|
|
49
49
|
var _a = __read(maybeWithUnderlyingType), type = _a[0], message_1 = _a.slice(1);
|
|
50
|
-
return "
|
|
50
|
+
return "".concat(chalk_1.default.bgRed(" ".concat(type, " ")), " ").concat(chalk_1.default.bold.whiteBright("\u2022 ".concat(message_1)));
|
|
51
51
|
}
|
|
52
|
-
return "
|
|
52
|
+
return "".concat(message);
|
|
53
53
|
}
|
|
54
54
|
return "Error: ".concat(JSON.stringify(u));
|
|
55
55
|
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const stripCwdPath: (path: string) => string;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.stripCwdPath = void 0;
|
|
4
|
+
var cwdRegex = new RegExp(process.cwd(), 'g');
|
|
5
|
+
var stripCwdPath = function (path) { return path.replace(cwdRegex, '.'); };
|
|
6
|
+
exports.stripCwdPath = stripCwdPath;
|
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"repository": "https://github.com/jpb06/effect-errors.git",
|
|
3
3
|
"main": "dist/index.js",
|
|
4
4
|
"name": "effect-errors",
|
|
5
|
-
"version": "1.1
|
|
5
|
+
"version": "1.2.1",
|
|
6
6
|
"author": "jpb06 <jp.bois.06@outlook.fr>",
|
|
7
7
|
"description": "A POC for errors reporting in Effect",
|
|
8
8
|
"keywords": [],
|
package/pretty-print.d.ts
CHANGED
|
@@ -1,2 +1,3 @@
|
|
|
1
1
|
import { Cause } from 'effect/Cause';
|
|
2
|
-
|
|
2
|
+
import { PrettyPrintOptions } from './types/pretty-print-options.type';
|
|
3
|
+
export declare const prettyPrint: <E>(cause: Cause<E>, { stripCwd }?: PrettyPrintOptions) => string;
|
package/pretty-print.js
CHANGED
|
@@ -12,14 +12,17 @@ var get_span_attributes_1 = require("./logic/get-span-attributes");
|
|
|
12
12
|
var get_span_duration_1 = require("./logic/get-span-duration");
|
|
13
13
|
var pretty_errors_1 = require("./logic/pretty-errors");
|
|
14
14
|
var spans_stack_trailing_char_1 = require("./logic/spans-stack-trailing-char");
|
|
15
|
-
var
|
|
15
|
+
var strip_cwd_path_1 = require("./logic/strip-cwd-path");
|
|
16
|
+
var pretty_print_options_type_1 = require("./types/pretty-print-options.type");
|
|
17
|
+
var prettyPrint = function (cause, _a) {
|
|
18
|
+
var _b = _a === void 0 ? pretty_print_options_type_1.prettyPrintOptionsDefault : _a, stripCwd = _b.stripCwd;
|
|
16
19
|
if ((0, Cause_1.isInterruptedOnly)(cause)) {
|
|
17
20
|
return 'All fibers interrupted without errors.';
|
|
18
21
|
}
|
|
19
22
|
var failures = (0, pretty_errors_1.prettyErrors)(cause);
|
|
20
23
|
console.error("\r\n\uD83E\uDEE0 ".concat(chalk_1.default.bold.yellowBright.underline("".concat(failures.length, " error").concat(failures.length > 1 ? 's' : '', " occurred\n"))));
|
|
21
24
|
return failures
|
|
22
|
-
.map(function (_a) {
|
|
25
|
+
.map(function (_a, failuresIndex) {
|
|
23
26
|
var message = _a.message, stack = _a.stack, span = _a.span;
|
|
24
27
|
if (span) {
|
|
25
28
|
var current = span;
|
|
@@ -28,24 +31,30 @@ var prettyPrint = function (cause) {
|
|
|
28
31
|
spans_1.push(current);
|
|
29
32
|
current = effect_1.Option.getOrUndefined(current.parent);
|
|
30
33
|
}
|
|
31
|
-
message
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
34
|
+
message =
|
|
35
|
+
'💥 ' +
|
|
36
|
+
(failures.length > 1
|
|
37
|
+
? "".concat(chalk_1.default.bgRed.whiteBright(" #".concat(failuresIndex + 1, " -")))
|
|
38
|
+
: '') +
|
|
39
|
+
message +
|
|
40
|
+
spans_1
|
|
41
|
+
.map(function (_a, index) {
|
|
42
|
+
var name = _a.name, attributes = _a.attributes, status = _a.status;
|
|
43
|
+
var isFirstEntry = index === 0;
|
|
44
|
+
var isLastEntry = index === spans_1.length - 1;
|
|
45
|
+
var filePath = " at ".concat(stripCwd ? (0, strip_cwd_path_1.stripCwdPath)(name) : name);
|
|
46
|
+
return chalk_1.default.whiteBright((isFirstEntry ? "\r\n".concat(chalk_1.default.gray('◯')) : '') +
|
|
47
|
+
'\r\n' +
|
|
48
|
+
(0, spans_stack_trailing_char_1.spanStackTrailingChar)(isLastEntry) +
|
|
49
|
+
chalk_1.default.gray('─') +
|
|
50
|
+
filePath +
|
|
51
|
+
(0, get_span_duration_1.getSpanDuration)(status, isLastEntry) +
|
|
52
|
+
(0, get_span_attributes_1.getSpanAttributes)(attributes, isLastEntry));
|
|
53
|
+
})
|
|
54
|
+
.join('');
|
|
46
55
|
}
|
|
47
56
|
if (stack) {
|
|
48
|
-
message += "\r\n".concat(span ? '\r\n' : '', "\uD83D\uDEA8 Stacktrace\r\n").concat(chalk_1.default.red((0, filter_stack_1.filterStack)(stack
|
|
57
|
+
message += "\r\n".concat(span ? '\r\n' : '', "\uD83D\uDEA8 Stacktrace\r\n").concat(chalk_1.default.red((0, filter_stack_1.filterStack)(stack, stripCwd)));
|
|
49
58
|
}
|
|
50
59
|
return message + '\r\n';
|
|
51
60
|
})
|
package/runners/run-promise.d.ts
CHANGED
|
@@ -1,2 +1,3 @@
|
|
|
1
1
|
import { Effect } from 'effect';
|
|
2
|
-
|
|
2
|
+
import { PrettyPrintOptions } from '../types/pretty-print-options.type';
|
|
3
|
+
export declare const runPromise: <A, E>(effect: Effect.Effect<A, E>, options?: PrettyPrintOptions) => Promise<A>;
|
package/runners/run-promise.js
CHANGED
|
@@ -4,10 +4,12 @@ exports.runPromise = void 0;
|
|
|
4
4
|
var effect_1 = require("effect");
|
|
5
5
|
var __1 = require("..");
|
|
6
6
|
var pretty_print_enabled_1 = require("../config/pretty-print-enabled");
|
|
7
|
-
var
|
|
7
|
+
var pretty_print_options_type_1 = require("../types/pretty-print-options.type");
|
|
8
|
+
var runPromise = function (effect, options) {
|
|
9
|
+
if (options === void 0) { options = pretty_print_options_type_1.prettyPrintOptionsDefault; }
|
|
8
10
|
return effect_1.Effect.runPromise((0, effect_1.pipe)(effect, effect_1.Effect.sandbox, effect_1.Effect.catchAll(function (e) {
|
|
9
11
|
if (pretty_print_enabled_1.prettyPrintEnabled) {
|
|
10
|
-
console.error((0, __1.prettyPrint)(e));
|
|
12
|
+
console.error((0, __1.prettyPrint)(e, options));
|
|
11
13
|
return effect_1.Effect.fail('❌ runPromise failure');
|
|
12
14
|
}
|
|
13
15
|
return effect_1.Effect.fail(e);
|
package/runners/run-sync.d.ts
CHANGED
|
@@ -1,2 +1,3 @@
|
|
|
1
1
|
import { Effect } from 'effect';
|
|
2
|
-
|
|
2
|
+
import { PrettyPrintOptions } from '../types/pretty-print-options.type';
|
|
3
|
+
export declare const runSync: <A, E>(effect: Effect.Effect<A, E>, options?: PrettyPrintOptions) => A;
|
package/runners/run-sync.js
CHANGED
|
@@ -4,10 +4,12 @@ exports.runSync = void 0;
|
|
|
4
4
|
var effect_1 = require("effect");
|
|
5
5
|
var __1 = require("..");
|
|
6
6
|
var pretty_print_enabled_1 = require("../config/pretty-print-enabled");
|
|
7
|
-
var
|
|
7
|
+
var pretty_print_options_type_1 = require("../types/pretty-print-options.type");
|
|
8
|
+
var runSync = function (effect, options) {
|
|
9
|
+
if (options === void 0) { options = pretty_print_options_type_1.prettyPrintOptionsDefault; }
|
|
8
10
|
return effect_1.Effect.runSync((0, effect_1.pipe)(effect, effect_1.Effect.sandbox, effect_1.Effect.catchAll(function (e) {
|
|
9
11
|
if (pretty_print_enabled_1.prettyPrintEnabled) {
|
|
10
|
-
console.error((0, __1.prettyPrint)(e));
|
|
12
|
+
console.error((0, __1.prettyPrint)(e, options));
|
|
11
13
|
return effect_1.Effect.fail('❌ runSync failure');
|
|
12
14
|
}
|
|
13
15
|
return effect_1.Effect.fail(e);
|