Converts a value into an Error of the specified type.
Requires Node.js 6.0.0 or above.
npm i errate
The module exports a single function.
- Optional:
e
(string, Error object, boolean, or null): The error message, or an existing Error object. Ifnull
or a boolean, the returned Error will have no message. - Optional:
Cls
(Error class): The desired class of the returned Error (such asTypeError
orRangeError
). 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. ife
istrue
or “falsey”). -
forceClass
(boolean): If set totrue
, instances of otherError
classes will be converted to instances ofCls
. If set tofalse
,Cls
will only be used to wrap string errors. Defaults totrue
.
-
An Error of the specified class.
const errate = require('errate')
const err = errate('Message')
err instanceof Error // true
err.message // 'Message'
const errate = require('errate')
const err = errate()
err instanceof Error // true
err.message // ''
const errate = require('errate')
const err = errate('Message', TypeError)
err instanceof TypeError // true
err.message // 'Message'
const errate = require('errate')
const err = errate(TypeError)
err instanceof TypeError // true
err.message // ''
const errate = require('errate')
const err = errate(new TypeError('Message'), RangeError)
err instanceof RangeError // true
err.message // 'Message'