It helps to know why you got an error.
npm install yerror[//]: # ( )
[//]: # (This file is automatically generated by a metapak)
[//]: # (module. Do not change it except between the)
[//]: # (content:start/end flags, your changes would)
[//]: # (be overridden.)
[//]: # ( )


[//]: # (::contents:start)
First, require me where you could throw errors:
``js`
import YError from 'yerror';
Then, emit errors with a bonus: parameters!
`js
function doSomething(pay, action) {
if(parseInt(pay, 10) !== pay) {
throw new YError('E_BAD_PAY', pay, action);
}
}
doSomething('nuts', 'code');
// YError: E_BAD_PAY (nuts, code)
// at doSomething (/home/nfroidure/nfroidure/yerror/test.js:5:11)
// at Object.
// (...)
`
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:
`js
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);
});
// YError: E_BAD_PAY (nuts, code)
// at doSomething (/home/nfroidure/nfroidure/yerror/test.js:5:11)
// (...)
// YError: E_BAD_TRANSACTION (pay)
// at Function.YError.wrap (/home/nfroidure/nfroidure/yerror/src/index.js:41:12)
// at /home/nfroidure/nfroidure/yerror/test.js:16:21
// at doSomethingAsync (/home/nfroidure/nfroidure/yerror/test.js:11:11)
// (...)
`
[//]: # (::contents:end)
ErrorA YError class able to contain some params and
print better stack traces
stringAllow to print a stack from anything (especially catched
errors that may or may not contain errors 🤷).
ErrorKind: global class
Extends: Error
* YError ⇐ Error
* [.wrap(err, [errorCode], [...params])](#YError.wrap) ⇒ YError
* [.cast(err, [errorCode], [...params])](#YError.cast) ⇒ YError
* [.bump(err, [errorCode], [...params])](#YError.bump) ⇒ YError
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] | YErrorParams | | Some additional debugging 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] | YErrorParams | | Some additional debugging values |
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] | YErrorParams | | Some additional debugging values |
stringKind: global function
Returns: string - The stack trace if any
| Param | Type | Description |
| --- | --- | --- |
| err | Error | The error to print |