effect-errors 1.0.8 → 1.1.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 +59 -0
- package/logic/pretty-error-message.js +8 -13
- package/package.json +3 -3
- package/runners/run-promise.d.ts +1 -1
- package/runners/run-sync.d.ts +1 -1
package/README.md
CHANGED
|
@@ -44,6 +44,65 @@ 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
|
+
|
|
98
|
+
### 🔶 Plain object
|
|
99
|
+
|
|
100
|
+
Alternativly, you _can_ use a plain object with a `_tag` and `message` attribute:
|
|
101
|
+
|
|
102
|
+
```typescript
|
|
103
|
+
Effect.fail({ _tag: 'SucksToBeMe', message: 'Yeah...' });
|
|
104
|
+
```
|
|
105
|
+
|
|
47
106
|
## ⚡ examples
|
|
48
107
|
|
|
49
108
|
I wrote some examples for fun and giggles. You can run them using:
|
|
@@ -23,18 +23,21 @@ 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
|
-
return "\uD83D\uDCA5 ".concat(u);
|
|
28
|
+
return "\uD83D\uDCA5 ".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 the _tag) field, or yieldable errors such as Data.TaggedError and Schema.TaggedError for better handling experience.'));
|
|
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");
|
|
30
33
|
}
|
|
31
34
|
// TaggedError with error ctor
|
|
32
35
|
if (u instanceof Error && (0, Predicate_1.hasProperty)(u, 'error')) {
|
|
33
36
|
return "\uD83D\uDCA5 ".concat(chalk_1.default.bgRed(" ".concat(u.name, " ")), " ").concat(chalk_1.default.bold.whiteBright("\u2022 ".concat(u.error)), "\r\n");
|
|
34
37
|
}
|
|
35
|
-
//
|
|
36
|
-
if (u
|
|
37
|
-
return "\uD83D\uDCA5 ".concat(chalk_1.default.bgRed(" ".concat(u.
|
|
38
|
+
// Plain objects with tag attribute
|
|
39
|
+
if ((0, Predicate_1.hasProperty)(u, '_tag') && (0, Predicate_1.hasProperty)(u, 'message')) {
|
|
40
|
+
return "\uD83D\uDCA5 ".concat(chalk_1.default.bgRed(" ".concat(u._tag, " ")), " ").concat(chalk_1.default.bold.whiteBright("\u2022 ".concat(u.message)), "\r\n");
|
|
38
41
|
}
|
|
39
42
|
if ((0, Predicate_1.hasProperty)(u, 'toString') &&
|
|
40
43
|
(0, Function_1.isFunction)(u['toString']) &&
|
|
@@ -48,14 +51,6 @@ var prettyErrorMessage = function (u) {
|
|
|
48
51
|
}
|
|
49
52
|
return "\uD83D\uDCA5 ".concat(message);
|
|
50
53
|
}
|
|
51
|
-
if ((0, Predicate_1.hasProperty)(u, '_tag') && (0, Predicate_1.hasProperty)(u, 'message')) {
|
|
52
|
-
var message = u.message
|
|
53
|
-
? chalk_1.default
|
|
54
|
-
.hex('#c25c30')(u.message)
|
|
55
|
-
.replace(cwdRegex, '.')
|
|
56
|
-
: undefined;
|
|
57
|
-
return "\uD83D\uDCA5 ".concat(chalk_1.default.bgRed(" ".concat(u._tag, " ")), " ").concat(message ? chalk_1.default.bold.whiteBright("\u2022 ".concat(message)) : '', "\r\n");
|
|
58
|
-
}
|
|
59
54
|
return "Error: ".concat(JSON.stringify(u));
|
|
60
55
|
};
|
|
61
56
|
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.1.0",
|
|
6
6
|
"author": "jpb06 <jp.bois.06@outlook.fr>",
|
|
7
7
|
"description": "A POC for errors reporting in Effect",
|
|
8
8
|
"keywords": [],
|
|
@@ -31,7 +31,7 @@
|
|
|
31
31
|
"@stylistic/eslint-plugin": "^1.6.3",
|
|
32
32
|
"@types/eslint": "^8.56.5",
|
|
33
33
|
"@types/fs-extra": "^11.0.4",
|
|
34
|
-
"@types/node": "^20.11.
|
|
34
|
+
"@types/node": "^20.11.25",
|
|
35
35
|
"@typescript-eslint/eslint-plugin": "^7.1.1",
|
|
36
36
|
"@typescript-eslint/parser": "^7.1.1",
|
|
37
37
|
"copyfiles": "^2.4.1",
|
|
@@ -45,6 +45,6 @@
|
|
|
45
45
|
"fs-extra": "^11.2.0",
|
|
46
46
|
"prettier": "^3.2.5",
|
|
47
47
|
"readme-package-icons": "^1.1.14",
|
|
48
|
-
"typescript": "^5.
|
|
48
|
+
"typescript": "^5.4.2"
|
|
49
49
|
}
|
|
50
50
|
}
|
package/runners/run-promise.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
import { Effect } from 'effect';
|
|
2
|
-
export declare const runPromise: <A, E>(effect: Effect.Effect<A, E
|
|
2
|
+
export declare const runPromise: <A, E>(effect: Effect.Effect<A, E>) => Promise<A>;
|
package/runners/run-sync.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
import { Effect } from 'effect';
|
|
2
|
-
export declare const runSync: <A, E>(effect: Effect.Effect<A, E
|
|
2
|
+
export declare const runSync: <A, E>(effect: Effect.Effect<A, E>) => A;
|