unknown-error 0.1.0 → 0.1.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 +16 -4
- package/WHY-UNKNOWN-ERROR.md +2 -2
- package/package.json +7 -7
package/README.md
CHANGED
|
@@ -14,7 +14,7 @@ Turn any caught `unknown` into a real Error without throwing, even for hostile v
|
|
|
14
14
|
- **Whole error trees**: Walks `cause` chains and `AggregateError` entries without recursion, so circular or 100,000-deep chains end instead of overflowing the stack.
|
|
15
15
|
- **Doesn't modify your errors**: Errors are returned as they are, never patched in place.
|
|
16
16
|
|
|
17
|
-
For why it exists and the design decisions behind it, see [WHY-UNKNOWN-ERROR.md](https://github.com/JacobLey/leyman/blob/main/tools/unknown-error/WHY-UNKNOWN-ERROR.md).
|
|
17
|
+
Guides and the full reference are on the [docs site](https://jacobley.github.io/leyman/packages/unknown-error/). For why it exists and the design decisions behind it, see [WHY-UNKNOWN-ERROR.md](https://github.com/JacobLey/leyman/blob/main/tools/unknown-error/WHY-UNKNOWN-ERROR.md).
|
|
18
18
|
|
|
19
19
|
## Contents
|
|
20
20
|
- [Install](#install)
|
|
@@ -86,7 +86,19 @@ toError('boom'); // NonError: boom
|
|
|
86
86
|
toError({ count: 1n }); // NonError: {"count":"1n"}
|
|
87
87
|
```
|
|
88
88
|
|
|
89
|
-
An error-like object without a `stack` gets a stack pointing at the `toError` call, since the original location is unknown.
|
|
89
|
+
An error-like object without a `stack` gets a stack pointing at the `toError` call, since the original location is unknown. Properties the new Error already has, own or inherited, are not copied, so a JSON-parsed `"__proto__"` key can't replace its prototype.
|
|
90
|
+
|
|
91
|
+
A [`NonError`](#nonerror) message describes the value as follows:
|
|
92
|
+
|
|
93
|
+
| Value | Message |
|
|
94
|
+
|-------|---------|
|
|
95
|
+
| String | The string itself |
|
|
96
|
+
| `null`, `undefined`, number, boolean, symbol | `String(value)`, like `'null'` or `'Symbol(id)'` |
|
|
97
|
+
| BigInt | `'10n'` |
|
|
98
|
+
| Function | `'[Function name]'`, never its source |
|
|
99
|
+
| Object or array | JSON, with circular references as `"[Circular]"` and BigInts as `"10n"` |
|
|
100
|
+
|
|
101
|
+
When an object's JSON is empty (`toJSON` returns `undefined`) or throws, the message falls back to `String(value)`, then to the object's tag (`[object Object]`), and finally to `[unprintable value]`.
|
|
90
102
|
|
|
91
103
|
### `getErrorMessage(value)`
|
|
92
104
|
|
|
@@ -135,14 +147,14 @@ Only real Errors match. To match error-like objects too, convert them first: `ha
|
|
|
135
147
|
|
|
136
148
|
Visits `value`, its `cause` chain and the `errors` of any `AggregateError`, depth first: an error, then its cause, then its `errors` in order.
|
|
137
149
|
|
|
138
|
-
Every value is converted with `toError`, so a cause that isn't an Error still appears. A value already visited is skipped, so circular chains end.
|
|
150
|
+
Every value is converted with `toError`, so a cause that isn't an Error still appears. A value already visited is skipped, so circular chains end. Primitives have no identity, so the same string can be visited more than once.
|
|
139
151
|
|
|
140
152
|
| Parameter | Type | Default | Description |
|
|
141
153
|
|-----------|------|---------|-------------|
|
|
142
154
|
| `value` | `unknown` | — | A caught value. |
|
|
143
155
|
| `options.maxDepth` | `number` | `32` | Deepest level to visit. The root is level `0`. Pass `Infinity` for no limit. |
|
|
144
156
|
|
|
145
|
-
**Returns** `Generator<ErrorNode>`.
|
|
157
|
+
**Returns** `Generator<ErrorNode, void, undefined>`. The option and node types are exported as `WalkOptions`, `ErrorNode` and `ErrorRelation`.
|
|
146
158
|
|
|
147
159
|
#### `ErrorNode`
|
|
148
160
|
|
package/WHY-UNKNOWN-ERROR.md
CHANGED
|
@@ -6,14 +6,14 @@
|
|
|
6
6
|
|
|
7
7
|
## The Problem
|
|
8
8
|
|
|
9
|
-
In TypeScript's `strict` mode, a `catch` variable is `unknown`,
|
|
9
|
+
In TypeScript's `strict` mode, a `catch` variable is `unknown`, because JavaScript can throw anything. To log it, report it or wrap it, you need an Error, so nearly every codebase ends up with something like this:
|
|
10
10
|
|
|
11
11
|
```ts
|
|
12
12
|
const toError = (value: unknown): Error =>
|
|
13
13
|
value instanceof Error ? value : new Error(String(value));
|
|
14
14
|
```
|
|
15
15
|
|
|
16
|
-
It handles the cases that come to mind first: an Error, or a thrown string. It
|
|
16
|
+
It handles the cases that come to mind first: an Error, or a thrown string. It fails on anything else, and throws inside the `catch` block that was meant to handle the error.
|
|
17
17
|
|
|
18
18
|
### Errors from another realm aren't `instanceof Error`
|
|
19
19
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "unknown-error",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.1",
|
|
4
4
|
"description": "Turn any caught `unknown` into a real Error without throwing, even for hostile values, other realms and deep or circular cause chains",
|
|
5
5
|
"engines": {
|
|
6
6
|
"node": ">=24"
|
|
@@ -13,7 +13,7 @@
|
|
|
13
13
|
"url": "git+https://github.com/JacobLey/leyman.git",
|
|
14
14
|
"directory": "tools/unknown-error"
|
|
15
15
|
},
|
|
16
|
-
"homepage": "https://github.
|
|
16
|
+
"homepage": "https://jacobley.github.io/leyman/packages/unknown-error/",
|
|
17
17
|
"bugs": {
|
|
18
18
|
"url": "https://github.com/JacobLey/leyman/issues"
|
|
19
19
|
},
|
|
@@ -49,16 +49,16 @@
|
|
|
49
49
|
"@leyman/eslint-config": "^0.0.1",
|
|
50
50
|
"@leyman/expect": "^0.0.1",
|
|
51
51
|
"@swc/cli": "^0.8.1",
|
|
52
|
-
"@swc/core": "^1.16.
|
|
52
|
+
"@swc/core": "^1.16.18",
|
|
53
53
|
"@types/mocha": "^10.0.10",
|
|
54
54
|
"@types/node": "^24.19.0",
|
|
55
55
|
"@typescript/native": "npm:typescript@^7.0.2",
|
|
56
56
|
"c8": "^12.0.0",
|
|
57
|
-
"eslint": "^10.
|
|
57
|
+
"eslint": "^10.13.0",
|
|
58
58
|
"expect-type": "^1.4.0",
|
|
59
|
-
"mocha": "^12.0.
|
|
60
|
-
"mocha-chain": "^1.0.
|
|
61
|
-
"npm-barrelify": "npm:barrelify@^
|
|
59
|
+
"mocha": "^12.0.3",
|
|
60
|
+
"mocha-chain": "^1.0.2",
|
|
61
|
+
"npm-barrelify": "npm:barrelify@^2.2.1",
|
|
62
62
|
"typescript": "npm:@typescript/typescript6@^6.0.2"
|
|
63
63
|
}
|
|
64
64
|
}
|