chubbyts-throwable-to-error
Description
Converts any thrown value into an Error.
In JavaScript, throw accepts anything: strings, numbers, plain objects, null, undefined, … That is why TypeScript
types a catch variable as unknown. throwableToError normalizes that value into a real Error, so the rest of your
code (logging, error responses, cause chains) can rely on message and name being present.
Why use it
- Type safe: turns
unknownintoErrorwithout unsafeas Errorcasts. - Never throws: circular references, throwing
toJSON/toString/ getters and unserializable values are handled and reported as[unserializable]instead of crashing your error handler. - Bounded output: messages longer than 2048 characters are truncated (suffix
...), so a huge thrown object can't flood your logs. - Keeps real errors untouched: an
Error(or subclass) is returned as is, the same instance with its original stack. - Tiny: a single function, no dependencies.
Requirements
- node: >= 22
Installation
Through NPM as @chubbyts/chubbyts-throwable-to-error.
npm i @chubbyts/chubbyts-throwable-to-error@^2.4.1
Usage
import { throwableToError } from '@chubbyts/chubbyts-throwable-to-error/throwable-to-error';
try {
throw { key: 'value' };
} catch (e) {
const error = throwableToError(e);
error.name; // 'object'
error.message; // '{"key":"value"}'
}
Behavior
| Thrown value | name | message |
|---|---|---|
new Error('example') | unchanged (same instance) | unchanged (same instance) |
'example' | 'string' | 'example' |
42 | 'number' | '42' |
true | 'boolean' | 'true' |
undefined | 'undefined' | 'undefined' |
null | 'null' | 'null' |
Symbol('example') | 'symbol' | 'Symbol(example)' |
() => null | 'function' | '() => null' |
{ key: 'value' } | 'object' | '{"key":"value"}' |
['example'] | 'object' | '["example"]' |
| object with circular reference | 'object' | '[unserializable]' |
- Objects (and arrays) are serialized with
JSON.stringify, all other values withString. - The
nameis thetypeofof the thrown value ('null'fornull). - The
stackof a converted error isundefined: it would only point tothrowableToErroritself, not to the place where the value was thrown.
Copyright
2026 Dominik Zogg