yerror
It helps to know why you got an error.
Usage
First, require me where you could throw errors:
import YError from 'yerror';
Then, emit errors with a bonus: parameters!
function doSomething(pay, action) {
if(parseInt(pay, 10) !== pay) {
throw new YError('E_BAD_PAY', pay, action);
}
}
doSomething('nuts', 'code');
You don't have to use constant like error messages, we use this convention
mainly for i18n reasons.
Also, you could want to wrap errors and keep a valuable stack trace:
function doSomethingAsync(pay, action) {
return new Promise(function(resolve, reject) {
try {
doSomething(pay, action);
resolve();
} catch(err) {
reject(YError.bump(err));
}
});
}
doSomethingAsync('nuts', 'code')
.catch(function(err) {
console.log(err.stack);
});
API
YError ⇐ Error
An YError class able to contain some params and
print better stack traces
Kind: global class
Extends: Error
new YError([errorCode], [...params])
Creates a new YError with an error code
and some params as debug values.
Param | Type | Default | Description |
---|
[errorCode] | string | "'E_UNEXPECTED'" | The error code corresponding to the actual error |
[...params] | any | | Some additional debugging values |
YError.wrap(err, [errorCode], [...params]) ⇒ YError
Wraps any error and output a YError with an error
code and some params as debug values.
Kind: static method of YError
Returns: YError
- The wrapped error
Param | Type | Default | Description |
---|
err | Error | | The error to wrap |
[errorCode] | string | "'E_UNEXPECTED'" | The error code corresponding to the actual error |
[...params] | any | | Some additional debugging values |
YError.cast(err, [errorCode], [...params]) ⇒ YError
Return a YError as is or wraps any other error and output
a YError with a code and some params as debug values.
Kind: static method of YError
Returns: YError
- The wrapped error
Param | Type | Default | Description |
---|
err | Error | | The error to cast |
[errorCode] | string | "'E_UNEXPECTED'" | The error code corresponding to the actual error |
[...params] | any | | Some additional debugging values |
YError.bump(err, [errorCode], [...params]) ⇒ YError
Same than YError.wrap()
but preserves the code
and the debug values of the error if it is
already an instance of the YError constructor.
Kind: static method of YError
Returns: YError
- The wrapped error
Param | Type | Default | Description |
---|
err | Error | | The error to bump |
[errorCode] | string | "'E_UNEXPECTED'" | The error code corresponding to the actual error |
[...params] | any | | Some additional debugging values |
Authors
License
MIT