@simbo/stringify-error
v1.0.2
Published
A lightweight utility that converts any error-like value into a safe, human-readable string.
Maintainers
Readme
Stringify Error
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.
Features
Handles any input type (
unknown) gracefullyExtracts message from
ErrorobjectsFalls back to
toString()where availableProvides meaningful defaults for non-standard values
Fully typed with TypeScript
Zero dependencies
Installation
Install @simbo/stringify-error from the npm registry:
npm i [-D] @simbo/stringify-errorUsage
For a complete API reference, see the documentation.
Example
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)}`);
}Input and Output
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 |
