effect-errors 1.0.7 → 1.0.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +51 -0
- package/logic/pretty-error-message.js +5 -9
- package/package.json +2 -1
package/README.md
CHANGED
|
@@ -44,6 +44,57 @@ Signature is the following:
|
|
|
44
44
|
const prettyPrint: <E>(cause: Cause<E>) => string;
|
|
45
45
|
```
|
|
46
46
|
|
|
47
|
+
## ⚡ How should I raise errors?
|
|
48
|
+
|
|
49
|
+
The best way is to use either `SchemaError` or `TaggedError`.
|
|
50
|
+
|
|
51
|
+
### 🔶 `SchemaError`
|
|
52
|
+
|
|
53
|
+
Declaring the error could look like this:
|
|
54
|
+
|
|
55
|
+
```typescript
|
|
56
|
+
import * as Schema from '@effect/schema/Schema';
|
|
57
|
+
|
|
58
|
+
export class FileNotFoundError extends Schema.TaggedError<SchemaError>()(
|
|
59
|
+
'FileNotFound',
|
|
60
|
+
{
|
|
61
|
+
cause: Schema.optional(Schema.unknown),
|
|
62
|
+
},
|
|
63
|
+
) {}
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
You would then raise a `FileNotFoundError` to the error channel like this:
|
|
67
|
+
|
|
68
|
+
```typescript
|
|
69
|
+
Effect.tryPromise({
|
|
70
|
+
try: () => ...,
|
|
71
|
+
catch: (e) => new FileNotFoundError({ cause: e }),
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
// or raising directly
|
|
75
|
+
Effect.fail(new FileNotFoundError({ cause: "Oh no!" }));
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
### 🔶 `TaggedError`
|
|
79
|
+
|
|
80
|
+
```typescript
|
|
81
|
+
export class UserNotFoundError extends TaggedError('UserNotFound')<{
|
|
82
|
+
cause?: unknown;
|
|
83
|
+
}> {}
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
You would then raise a `UserNotFoundError` to the error channel like this:
|
|
87
|
+
|
|
88
|
+
```typescript
|
|
89
|
+
Effect.tryPromise({
|
|
90
|
+
try: () => ...,
|
|
91
|
+
catch: (e) => new UserNotFoundError({ cause: e }),
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
// or raising directly
|
|
95
|
+
Effect.fail(new UserNotFoundError({ cause: "User does not exist" }));
|
|
96
|
+
```
|
|
97
|
+
|
|
47
98
|
## ⚡ examples
|
|
48
99
|
|
|
49
100
|
I wrote some examples for fun and giggles. You can run them using:
|
|
@@ -23,11 +23,15 @@ exports.prettyErrorMessage = void 0;
|
|
|
23
23
|
var chalk_1 = __importDefault(require("chalk"));
|
|
24
24
|
var Function_1 = require("effect/Function");
|
|
25
25
|
var Predicate_1 = require("effect/Predicate");
|
|
26
|
-
var cwdRegex = new RegExp("".concat(process.cwd()), 'g');
|
|
27
26
|
var prettyErrorMessage = function (u) {
|
|
28
27
|
if (typeof u === 'string') {
|
|
29
28
|
return "\uD83D\uDCA5 ".concat(u);
|
|
30
29
|
}
|
|
30
|
+
// TaggedError with cause
|
|
31
|
+
if (u instanceof Error && (0, Predicate_1.hasProperty)(u, 'cause') && (0, Predicate_1.hasProperty)(u, '_tag')) {
|
|
32
|
+
return "\uD83D\uDCA5 ".concat(chalk_1.default.bgRed(" ".concat(u._tag, " ")), " ").concat(chalk_1.default.bold.whiteBright("\u2022 ".concat(u.cause)), "\r\n");
|
|
33
|
+
}
|
|
34
|
+
// TaggedError with error ctor
|
|
31
35
|
if (u instanceof Error && (0, Predicate_1.hasProperty)(u, 'error')) {
|
|
32
36
|
return "\uD83D\uDCA5 ".concat(chalk_1.default.bgRed(" ".concat(u.name, " ")), " ").concat(chalk_1.default.bold.whiteBright("\u2022 ".concat(u.error)), "\r\n");
|
|
33
37
|
}
|
|
@@ -43,14 +47,6 @@ var prettyErrorMessage = function (u) {
|
|
|
43
47
|
}
|
|
44
48
|
return "\uD83D\uDCA5 ".concat(message);
|
|
45
49
|
}
|
|
46
|
-
if ((0, Predicate_1.hasProperty)(u, '_tag') && (0, Predicate_1.hasProperty)(u, 'message')) {
|
|
47
|
-
var message = u.message
|
|
48
|
-
? chalk_1.default
|
|
49
|
-
.hex('#c25c30')(u.message)
|
|
50
|
-
.replace(cwdRegex, '.')
|
|
51
|
-
: undefined;
|
|
52
|
-
return "\uD83D\uDCA5 ".concat(chalk_1.default.bgRed(" ".concat(u._tag, " ")), " ").concat(message ? chalk_1.default.bold.whiteBright("\u2022 ".concat(message)) : '', "\r\n");
|
|
53
|
-
}
|
|
54
50
|
return "Error: ".concat(JSON.stringify(u));
|
|
55
51
|
};
|
|
56
52
|
exports.prettyErrorMessage = prettyErrorMessage;
|
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.0.
|
|
5
|
+
"version": "1.0.9",
|
|
6
6
|
"author": "jpb06 <jp.bois.06@outlook.fr>",
|
|
7
7
|
"description": "A POC for errors reporting in Effect",
|
|
8
8
|
"keywords": [],
|
|
@@ -22,6 +22,7 @@
|
|
|
22
22
|
"run-examples": "for i in ./src/examples/*.ts; do bun run $i; done"
|
|
23
23
|
},
|
|
24
24
|
"dependencies": {
|
|
25
|
+
"@effect/schema": "^0.63.2",
|
|
25
26
|
"chalk": "^5.3.0",
|
|
26
27
|
"dotenv-flow": "^4.1.0",
|
|
27
28
|
"effect": "^2.4.1"
|