err-object
Custom error object.
- supports to define error codes and messages in advance before use.
- provides cleaned error stack and we can manage the stack sanitizer by using
error-stack
(since 5.1.0)
Why
Tired writing code like this:
const error = 'message'errorcode = 'SOME_ERROR'
There are tremendous modules about custom errors in the NPM, but NONE of those is usable.
Install
$ npm i err-object
Usage
const message = 'message' // Error// - message // Error// - message// - name: 'ImplementError'// - code: 'ERR_IMPL' // TypeError// - message
Creates error templates to manage multiple error types
We could use this to standardize the error objects of the whole project.
const E error i18n = // Error code, and message // Which is equivalent to: // Error code // The equivalent default factoryconst factory = { const ctor = Error message: messageTemplate ...others } = preset const message = util return } // Error// - code: 'ERR_NO_PERMISSION'// - message: 'you do not have permission to do this' // TypeError// - code: 'ERR_INVALID_TYPE'// - message: 'number expected but got string'// - args: ['string'] // The same return value of the last statement // The constructor `Errors` accepts a `options.factory` parameter,// to define the default error factory.// And so the following statement is equivalent to `new Errors()` factory const ZH_CN_MAP = 'number expected but got %s': '期望 number 类型但实际为 %s' // TypeError// - code: 'ERR_INVALID_TYPE'// - message: '期望 number 类型但实际为 string'// - args: ['string']
error(thing, ctor)
- thing
String|Object
- ctor
Class=Error
new Errors(options)
- options?
Object
- factory?
Function(code, preset, ...args)
the default error factory (the default value please see above) - notDefined?
Function(code, ...args)=exitOnNotDefined
will create the error object if the givencode
is not defined byerror.E
. Since5.0.0
, if the given error code is not defined byerror.E
, it will throw an error and exit the current process. prefix?Deprecated instring
4.4.0
- messagePrefix?
string
the message prefix for every error message. New in4.4.0
- codePrefix?
string
the code prefix. New in4.4.0
- filterStackSources?
Array<path>=[]
defines source paths to be filtered out from error stacks. New in5.1.0
- factory?
error.E(code, preset, factory)
error.E(code, template, ctor)
Define an error preset.
- code
string
define the error code - preset
?Object
- ctor
?Error=Error
the constructor of the error - template
?(string | Function(...args))
the message template which will be formatted byutil.format()
- other property/properties that you want to add to the error object.
- ctor
- factory
?Function({code, preset, args, _})
the error factory- _
?Function=(x=>x)
thei18nConverter
function which defaults to the function that just returns the argument.
- _
Returns this
error.TE(code, template)
new in
4.5.0
Define a TypeError, in favor of using new TypeError('should be ..., but got
something')
const error TE = throw // TypeError: options must be an object, but got `undefined`
error.i18n(i18nConverter)
- i18nConverter
Function(string): string
Specify the i18n mapping function which receives the message template and returns the converted message template.
Returns this
error.error(code, ...args)
Creates a standard error object by code.
- code
- args
Array<any>
which will be passed and spreaded intofactory
after thecode
and thepreset
parameters.
Returns Error
And if a given code
is not defined by error.E()
, the return value will be notDefined(code, ...args)
message prefix
const E error = messagePrefix: '[err-object] ' codePrefix: 'CORE_' const err = console// [err-object] this is a fatal error// - code: 'CORE_FATAL_ERROR'
options.filterStackSources
/path/to/a.js (before):
const error E = module
/path/to/b.js
const error = const err = console// Error: bar// at Object.<anonymous> (/path/to/a.js:3:1)// at error (/path/to/b.js:2:13)// ...
Let's take a look at the error stack above, the /path/to/a.js
line is actually useless.
Then how to get rid of the first stack trace line? We can use options.filterStackSources
/path/to/a.js (after):
const error E = filterStackSources: // Filter out the current source file __filename module
Then,
console// Error: bar// at error (/path/to/b.js:2:13)// ...
License
MIT