A lightweight utility that converts any error-like value into a safe, human-readable string.
Designed for robust error handling in logs, CLI output, or user-facing messages.
Handles any input type (unknown) gracefully
Extracts message from Error objects
Falls back to toString() where available
Provides meaningful defaults for non-standard values
Fully typed with TypeScript
Zero dependencies
Install @simbo/stringify-error from the npm registry:
npm i [-D] @simbo/stringify-error
For a complete API reference, see the documentation.
Use stringifyError in your error handling:
import { stringifyError } from '@simbo/stringify-error';
try {
// Some things that may throw
} catch (error: unknown) {
console.error(`Failed: ${stringifyError(error)}`);
}
The following table is a simplified view of how stringifyError tests the input
and creates respective output:
Input (error: unknown) |
Output (string) |
|---|---|
Object with a message string |
error.message |
Object with a toString method |
error.toString() |
| Other object | `Unknown Error (${JSON.stringify(error)})` |
| Non-string primitive (number, etc.) | `Unknown Error (${String(error)})` |
| Empty string | `Unknown Error ("")` |
| Non-empty string | error |