Package Exports
- err-object
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (err-object) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
err-object
Custom error object.
Tired writing code like this:
const error = new SomeError('message')
error.code = 'SOME_ERROR'There are tremendous modules about custom errors in the NPM, but NONE of those could be usable.
Install
$ npm install err-objectUsage
import err from 'err-object'
const message = 'message'
err(message)
// Error
// - message
err({
message,
name: 'ImplementError',
code: 'ERR_IMPL'
})
// Error
// - message
// - name: 'ImplementError'
// - code: 'ERR_IMPL'
err(message, TypeError)
// TypeError
// - messageCreates error templates to manage multiple error types
import err, {Errors} from 'err-object'
import util from 'util'
const {
E,
error
} = new Errors()
// Error code, and message
E('ERR_NO_PERMISSION', 'you do not have permission to do this')
// Error code
E('ERR_INVALID_TYPE', {
// Custom error type
ctor: TypeError,
// Message template which will be formatted by `util.format`
message: 'number expected but got %s'
})
const factory = (code, preset, ...args) => {
const {
ctor = Error,
message: messageTemplate,
...others
} = preset
const message = util.format(messageTemplate, ...args)
return err({
...others,
code,
message
}, Error)
}
E('ERR_INVALID_TYPE_2', {
// Custom error type
ctor: TypeError,
// Message template which will be formatted by `util.format`
message: 'number expected but got %s'
// We can define our custom error factory,
// and the default error factory is:
}, factory)
error('ERR_NO_PERMISSION')
// Error
// - code: 'ERR_NO_PERMISSION'
// - message: 'you do not have permission to do this'
error('ERR_INVALID_TYPE', 'string')
// TypeError
// - code: 'ERR_INVALID_TYPE'
// - message: 'number expected but got string'
error('ERR_INVALID_TYPE_2', '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()`
new Errors({
factory
})err(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)will create the error object if the givencodeis not defined.
- factory
error.E(code, preset, factory)
Define an error preset.
- code
stringdefine the error code - preset
?Object- ctor
?Error=Errorthe constructor of the error - template
?stringthe 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
Returns this
error.error(code, ...args)
- code
- args
Array<any>which will be passed and spreaded intofactoryafter thecodeand thepresetparameters.
Returns Error And if a given code is not defined by error.E(), the return value will be notDefined(code, ...args)
License
MIT