effect-errors 1.0.8 → 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 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,19 +23,18 @@ 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
+ }
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
- // TaggedError with cause
36
- if (u instanceof Error && (0, Predicate_1.hasProperty)(u, 'cause')) {
37
- return "\uD83D\uDCA5 ".concat(chalk_1.default.bgRed(" ".concat(u.name, " ")), " ").concat(chalk_1.default.bold.whiteBright("\u2022 ".concat(u.cause)), "\r\n");
38
- }
39
38
  if ((0, Predicate_1.hasProperty)(u, 'toString') &&
40
39
  (0, Function_1.isFunction)(u['toString']) &&
41
40
  u['toString'] !== Object.prototype.toString &&
@@ -48,14 +47,6 @@ var prettyErrorMessage = function (u) {
48
47
  }
49
48
  return "\uD83D\uDCA5 ".concat(message);
50
49
  }
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
50
  return "Error: ".concat(JSON.stringify(u));
60
51
  };
61
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.8",
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": [],