ensure-error
TypeScript icon, indicating that this package has built-in type declarations

4.0.0 • Public • Published

ensure-error

Ensures a value is a valid error by making it one if not

Pass it any value and you're are guaranteed to get back an Error with name, message, and stack properties.

Can be useful when you don't control all the places an error can be thrown or rejected. A user could for example throw a string or an error without a stack property.

Install

$ npm install ensure-error

Usage

import ensureError from 'ensure-error';

const error = new TypeError('🦄');
error.name = '';

console.log(error.name);
//=> ''

console.log(ensureError(error).name);
//=> 'TypeError'
import ensureError from 'ensure-error';

console.log(ensureError(10));
//=> [NonError: 10]

API

ensureError(input)

If input is an Error, any missing Error properties will be added. If it's not an Error, input is converted to an Error.

input

Type: unknown

/ensure-error/

    Package Sidebar

    Install

    npm i ensure-error

    Weekly Downloads

    15,143

    Version

    4.0.0

    License

    MIT

    Unpacked Size

    4.74 kB

    Total Files

    5

    Last publish

    Collaborators

    • sindresorhus