@chubbyts/chubbyts-throwable-to-error 2.4.0 → 2.4.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 +43 -5
- package/dist/throwable-to-error.js +2 -0
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -19,18 +19,32 @@
|
|
|
19
19
|
|
|
20
20
|
## Description
|
|
21
21
|
|
|
22
|
-
|
|
22
|
+
Converts any thrown value into an `Error`.
|
|
23
|
+
|
|
24
|
+
In JavaScript, `throw` accepts anything: strings, numbers, plain objects, `null`, `undefined`, … That is why TypeScript
|
|
25
|
+
types a `catch` variable as `unknown`. `throwableToError` normalizes that value into a real `Error`, so the rest of your
|
|
26
|
+
code (logging, error responses, `cause` chains) can rely on `message` and `name` being present.
|
|
27
|
+
|
|
28
|
+
### Why use it
|
|
29
|
+
|
|
30
|
+
* **Type safe**: turns `unknown` into `Error` without unsafe `as Error` casts.
|
|
31
|
+
* **Never throws**: circular references, throwing `toJSON` / `toString` / getters and unserializable values are handled
|
|
32
|
+
and reported as `[unserializable]` instead of crashing your error handler.
|
|
33
|
+
* **Bounded output**: messages longer than 2048 characters are truncated (suffix `...`), so a huge thrown object can't
|
|
34
|
+
flood your logs.
|
|
35
|
+
* **Keeps real errors untouched**: an `Error` (or subclass) is returned as is, the same instance with its original stack.
|
|
36
|
+
* **Tiny**: a single function, no dependencies.
|
|
23
37
|
|
|
24
38
|
## Requirements
|
|
25
39
|
|
|
26
|
-
* node: 22
|
|
40
|
+
* node: >= 22
|
|
27
41
|
|
|
28
42
|
## Installation
|
|
29
43
|
|
|
30
44
|
Through [NPM](https://www.npmjs.com) as [@chubbyts/chubbyts-throwable-to-error][1].
|
|
31
45
|
|
|
32
|
-
```
|
|
33
|
-
npm i @chubbyts/chubbyts-throwable-to-error@^2.4.
|
|
46
|
+
```sh
|
|
47
|
+
npm i @chubbyts/chubbyts-throwable-to-error@^2.4.1
|
|
34
48
|
```
|
|
35
49
|
|
|
36
50
|
## Usage
|
|
@@ -39,12 +53,36 @@ npm i @chubbyts/chubbyts-throwable-to-error@^2.4.0
|
|
|
39
53
|
import { throwableToError } from '@chubbyts/chubbyts-throwable-to-error/throwable-to-error';
|
|
40
54
|
|
|
41
55
|
try {
|
|
42
|
-
|
|
56
|
+
throw { key: 'value' };
|
|
43
57
|
} catch (e) {
|
|
44
58
|
const error = throwableToError(e);
|
|
59
|
+
|
|
60
|
+
error.name; // 'object'
|
|
61
|
+
error.message; // '{"key":"value"}'
|
|
45
62
|
}
|
|
46
63
|
```
|
|
47
64
|
|
|
65
|
+
## Behavior
|
|
66
|
+
|
|
67
|
+
| Thrown value | `name` | `message` |
|
|
68
|
+
| ------------------------------ | ------------------------- | ------------------------- |
|
|
69
|
+
| `new Error('example')` | unchanged (same instance) | unchanged (same instance) |
|
|
70
|
+
| `'example'` | `'string'` | `'example'` |
|
|
71
|
+
| `42` | `'number'` | `'42'` |
|
|
72
|
+
| `true` | `'boolean'` | `'true'` |
|
|
73
|
+
| `undefined` | `'undefined'` | `'undefined'` |
|
|
74
|
+
| `null` | `'null'` | `'null'` |
|
|
75
|
+
| `Symbol('example')` | `'symbol'` | `'Symbol(example)'` |
|
|
76
|
+
| `() => null` | `'function'` | `'() => null'` |
|
|
77
|
+
| `{ key: 'value' }` | `'object'` | `'{"key":"value"}'` |
|
|
78
|
+
| `['example']` | `'object'` | `'["example"]'` |
|
|
79
|
+
| object with circular reference | `'object'` | `'[unserializable]'` |
|
|
80
|
+
|
|
81
|
+
* Objects (and arrays) are serialized with `JSON.stringify`, all other values with `String`.
|
|
82
|
+
* The `name` is the `typeof` of the thrown value (`'null'` for `null`).
|
|
83
|
+
* The `stack` of a converted error is `undefined`: it would only point to `throwableToError` itself, not to the place
|
|
84
|
+
where the value was thrown.
|
|
85
|
+
|
|
48
86
|
## Copyright
|
|
49
87
|
|
|
50
88
|
2026 Dominik Zogg
|
|
@@ -7,6 +7,8 @@ const throwableToMessage = (e) => {
|
|
|
7
7
|
if (typeof e === 'object') {
|
|
8
8
|
return JSON.stringify(e) ?? '[unserializable]';
|
|
9
9
|
}
|
|
10
|
+
// Objects (incl. null) are handled above, so only primitives and functions are left,
|
|
11
|
+
// none of which stringify to '[object Object]'. TypeScript can't narrow `unknown` here.
|
|
10
12
|
return String(e);
|
|
11
13
|
}
|
|
12
14
|
catch {
|
package/package.json
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@chubbyts/chubbyts-throwable-to-error",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "2.4.
|
|
5
|
-
"description": "
|
|
4
|
+
"version": "2.4.1",
|
|
5
|
+
"description": "Converts any thrown value into an Error.",
|
|
6
6
|
"keywords": [
|
|
7
7
|
"chubbyts",
|
|
8
8
|
"throwable",
|