1.3.0 • Published 6 years ago
errate v1.3.0
errate
Converts a value into an Error of the specified type.
Installation
Requires Node.js 6.0.0 or above.
npm i errateAPI
The module exports a single function.
Parameters
- Optional:
e(string, Error object, boolean, or null): The error message, or an existing Error object. Ifnullor a boolean, the returned Error will have no message. - Optional:
Cls(Error class): The desired class of the returned Error (such asTypeErrororRangeError). Defaults toError. - Optional: Object argument:
defaultMessage(string): The first argument that will be provided to the constructor if an Error object is being created (i.e. ifeistrueor “falsey”).forceClass(boolean): If set totrue, instances of otherErrorclasses will be converted to instances ofCls. If set tofalse,Clswill only be used to wrap string errors. Defaults totrue.
Return Value
An Error of the specified class.
Examples
Default Class With Message
const errate = require('errate')
const err = errate('Message')
err instanceof Error // true
err.message // 'Message'Default Class Without Message
const errate = require('errate')
const err = errate()
err instanceof Error // true
err.message // ''Error Subclass With Message
const errate = require('errate')
const err = errate('Message', TypeError)
err instanceof TypeError // true
err.message // 'Message'Error Subclass Without Message
const errate = require('errate')
const err = errate(TypeError)
err instanceof TypeError // true
err.message // ''Error Class Conversion
const errate = require('errate')
const err = errate(new TypeError('Message'), RangeError)
err instanceof RangeError // true
err.message // 'Message'