yerror 6.1.1 → 6.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +4 -0
- package/README.md +31 -0
- package/dist/index.js +15 -1
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +4 -0
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
- package/src/index.d.ts +2 -0
- package/src/index.js +18 -0
package/CHANGELOG.md
CHANGED
package/README.md
CHANGED
|
@@ -76,6 +76,24 @@ doSomethingAsync('nuts', 'code')
|
|
|
76
76
|
[//]: # (::contents:end)
|
|
77
77
|
|
|
78
78
|
# API
|
|
79
|
+
## Classes
|
|
80
|
+
|
|
81
|
+
<dl>
|
|
82
|
+
<dt><a href="#YError">YError</a> ⇐ <code>Error</code></dt>
|
|
83
|
+
<dd><p>An YError class able to contain some params and
|
|
84
|
+
print better stack traces</p>
|
|
85
|
+
</dd>
|
|
86
|
+
</dl>
|
|
87
|
+
|
|
88
|
+
## Functions
|
|
89
|
+
|
|
90
|
+
<dl>
|
|
91
|
+
<dt><a href="#printStackTrace">printStackTrace(err)</a> ⇒ <code>string</code></dt>
|
|
92
|
+
<dd><p>Allow to print a stack from anything (especially catched
|
|
93
|
+
errors that may or may not contain errors 🤷).</p>
|
|
94
|
+
</dd>
|
|
95
|
+
</dl>
|
|
96
|
+
|
|
79
97
|
<a name="YError"></a>
|
|
80
98
|
|
|
81
99
|
## YError ⇐ <code>Error</code>
|
|
@@ -149,6 +167,19 @@ Same than `YError.wrap()` but preserves the code
|
|
|
149
167
|
| [errorCode] | <code>string</code> | <code>"'E_UNEXPECTED'"</code> | The error code corresponding to the actual error |
|
|
150
168
|
| [...params] | <code>any</code> | | Some additional debugging values |
|
|
151
169
|
|
|
170
|
+
<a name="printStackTrace"></a>
|
|
171
|
+
|
|
172
|
+
## printStackTrace(err) ⇒ <code>string</code>
|
|
173
|
+
Allow to print a stack from anything (especially catched
|
|
174
|
+
errors that may or may not contain errors 🤷).
|
|
175
|
+
|
|
176
|
+
**Kind**: global function
|
|
177
|
+
**Returns**: <code>string</code> - The stack trace if any
|
|
178
|
+
|
|
179
|
+
| Param | Type | Description |
|
|
180
|
+
| --- | --- | --- |
|
|
181
|
+
| err | <code>Error</code> | The error to print |
|
|
182
|
+
|
|
152
183
|
|
|
153
184
|
# Authors
|
|
154
185
|
- [Nicolas Froidure (formerly at SimpliField)](http://insertafter.com/en/index.html)
|
package/dist/index.js
CHANGED
|
@@ -4,6 +4,7 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.default = exports.YError = void 0;
|
|
7
|
+
exports.printStackTrace = printStackTrace;
|
|
7
8
|
|
|
8
9
|
var _os = _interopRequireDefault(require("os"));
|
|
9
10
|
|
|
@@ -166,7 +167,20 @@ YError.bump = function yerrorBump(err, ...params) {
|
|
|
166
167
|
}
|
|
167
168
|
|
|
168
169
|
return YError.wrap.apply(YError, [err].concat(params));
|
|
169
|
-
};
|
|
170
|
+
};
|
|
171
|
+
/**
|
|
172
|
+
* Allow to print a stack from anything (especially catched
|
|
173
|
+
* errors that may or may not contain errors 🤷).
|
|
174
|
+
* @param {Error} err
|
|
175
|
+
* The error to print
|
|
176
|
+
* @return {string}
|
|
177
|
+
* The stack trace if any
|
|
178
|
+
*/
|
|
179
|
+
|
|
180
|
+
|
|
181
|
+
function printStackTrace(err) {
|
|
182
|
+
return typeof err === 'object' && typeof err.stack === 'function' ? err.stack() : `[no_stack_trace]: error is ${err != null && typeof err.toString === 'function' ? err.toString() : typeof err}`;
|
|
183
|
+
} // In order to keep compatibility through major versions
|
|
170
184
|
// we have to make kind of an cross major version instanceof
|
|
171
185
|
|
|
172
186
|
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":["YError","wrappedErrors","errorCode","params","Array","concat","code","name","toString","Error","captureStackTrace","constructor","length","stack","os","EOL","join","wrap","yerrorWrap","err","yError","wrappedErrorIsACode","_looksLikeAYErrorCode","message","push","cast","yerrorCast","_looksLikeAYError","apply","bump","yerrorBump","endsWith","str","test"],"sources":["../src/index.js"],"sourcesContent":["import os from 'os';\n\n/**\n * An YError class able to contain some params and\n * print better stack traces\n * @extends Error\n */\nclass YError extends Error {\n /**\n * Creates a new YError with an error code\n * and some params as debug values.\n * @param {string} [errorCode = 'E_UNEXPECTED']\n * The error code corresponding to the actual error\n * @param {...any} [params]\n * Some additional debugging values\n */\n constructor(wrappedErrors, errorCode, ...params) {\n // Detecting if wrappedErrors are passed\n if (!(wrappedErrors instanceof Array)) {\n params = ('undefined' === typeof errorCode ? [] : [errorCode]).concat(\n params,\n );\n errorCode = wrappedErrors;\n wrappedErrors = [];\n }\n\n // Call the parent constructor\n super(errorCode);\n\n // Filling error\n this.code = errorCode || 'E_UNEXPECTED';\n this.params = params;\n this.wrappedErrors = wrappedErrors;\n this.name = this.toString();\n\n if (Error.captureStackTrace) {\n Error.captureStackTrace(this, this.constructor);\n }\n }\n\n toString() {\n return (\n (this.wrappedErrors.length\n ? // eslint-disable-next-line\n this.wrappedErrors[this.wrappedErrors.length - 1].stack + os.EOL\n : '') +\n this.constructor.name +\n ': ' +\n this.code +\n ' (' +\n this.params.join(', ') +\n ')'\n );\n }\n}\n\n/**\n * Wraps any error and output a YError with an error\n * code and some params as debug values.\n * @param {Error} err\n * The error to wrap\n * @param {string} [errorCode = 'E_UNEXPECTED']\n * The error code corresponding to the actual error\n * @param {...any} [params]\n * Some additional debugging values\n * @return {YError}\n * The wrapped error\n */\nYError.wrap = function yerrorWrap(err, errorCode, ...params) {\n let yError = null;\n const wrappedErrorIsACode = _looksLikeAYErrorCode(err.message);\n const wrappedErrors = (err.wrappedErrors || []).concat(err);\n\n if (!errorCode) {\n if (wrappedErrorIsACode) {\n errorCode = err.message;\n } else {\n errorCode = 'E_UNEXPECTED';\n }\n }\n if (err.message && !wrappedErrorIsACode) {\n params.push(err.message);\n }\n yError = new YError(wrappedErrors, errorCode, ...params);\n return yError;\n};\n\n/**\n * Return a YError as is or wraps any other error and output\n * a YError with a code and some params as debug values.\n * @param {Error} err\n * The error to cast\n * @param {string} [errorCode = 'E_UNEXPECTED']\n * The error code corresponding to the actual error\n * @param {...any} [params]\n * Some additional debugging values\n * @return {YError}\n * The wrapped error\n */\nYError.cast = function yerrorCast(err, ...params) {\n if (_looksLikeAYError(err)) {\n return err;\n }\n return YError.wrap.apply(YError, [err].concat(params));\n};\n\n/**\n * Same than `YError.wrap()` but preserves the code\n * and the debug values of the error if it is\n * already an instance of the YError constructor.\n * @param {Error} err\n * The error to bump\n * @param {string} [errorCode = 'E_UNEXPECTED']\n * The error code corresponding to the actual error\n * @param {...any} [params]\n * Some additional debugging values\n * @return {YError}\n * The wrapped error\n */\nYError.bump = function yerrorBump(err, ...params) {\n if (_looksLikeAYError(err)) {\n return YError.wrap.apply(YError, [err, err.code].concat(err.params));\n }\n return YError.wrap.apply(YError, [err].concat(params));\n};\n\n// In order to keep compatibility through major versions\n// we have to make kind of an cross major version instanceof\nfunction _looksLikeAYError(err) {\n return (\n err instanceof YError ||\n (err.constructor &&\n err.constructor.name &&\n err.constructor.name.endsWith('Error') &&\n 'string' === typeof err.code &&\n _looksLikeAYErrorCode(err.code) &&\n err.params &&\n err.params instanceof Array)\n );\n}\n\nfunction _looksLikeAYErrorCode(str) {\n return /^([A-Z0-9_]+)$/.test(str);\n}\n\nexport default YError;\nexport { YError };\n"],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.js","names":["YError","wrappedErrors","errorCode","params","Array","concat","code","name","toString","Error","captureStackTrace","constructor","length","stack","os","EOL","join","wrap","yerrorWrap","err","yError","wrappedErrorIsACode","_looksLikeAYErrorCode","message","push","cast","yerrorCast","_looksLikeAYError","apply","bump","yerrorBump","printStackTrace","endsWith","str","test"],"sources":["../src/index.js"],"sourcesContent":["import os from 'os';\n\n/**\n * An YError class able to contain some params and\n * print better stack traces\n * @extends Error\n */\nclass YError extends Error {\n /**\n * Creates a new YError with an error code\n * and some params as debug values.\n * @param {string} [errorCode = 'E_UNEXPECTED']\n * The error code corresponding to the actual error\n * @param {...any} [params]\n * Some additional debugging values\n */\n constructor(wrappedErrors, errorCode, ...params) {\n // Detecting if wrappedErrors are passed\n if (!(wrappedErrors instanceof Array)) {\n params = ('undefined' === typeof errorCode ? [] : [errorCode]).concat(\n params,\n );\n errorCode = wrappedErrors;\n wrappedErrors = [];\n }\n\n // Call the parent constructor\n super(errorCode);\n\n // Filling error\n this.code = errorCode || 'E_UNEXPECTED';\n this.params = params;\n this.wrappedErrors = wrappedErrors;\n this.name = this.toString();\n\n if (Error.captureStackTrace) {\n Error.captureStackTrace(this, this.constructor);\n }\n }\n\n toString() {\n return (\n (this.wrappedErrors.length\n ? // eslint-disable-next-line\n this.wrappedErrors[this.wrappedErrors.length - 1].stack + os.EOL\n : '') +\n this.constructor.name +\n ': ' +\n this.code +\n ' (' +\n this.params.join(', ') +\n ')'\n );\n }\n}\n\n/**\n * Wraps any error and output a YError with an error\n * code and some params as debug values.\n * @param {Error} err\n * The error to wrap\n * @param {string} [errorCode = 'E_UNEXPECTED']\n * The error code corresponding to the actual error\n * @param {...any} [params]\n * Some additional debugging values\n * @return {YError}\n * The wrapped error\n */\nYError.wrap = function yerrorWrap(err, errorCode, ...params) {\n let yError = null;\n const wrappedErrorIsACode = _looksLikeAYErrorCode(err.message);\n const wrappedErrors = (err.wrappedErrors || []).concat(err);\n\n if (!errorCode) {\n if (wrappedErrorIsACode) {\n errorCode = err.message;\n } else {\n errorCode = 'E_UNEXPECTED';\n }\n }\n if (err.message && !wrappedErrorIsACode) {\n params.push(err.message);\n }\n yError = new YError(wrappedErrors, errorCode, ...params);\n return yError;\n};\n\n/**\n * Return a YError as is or wraps any other error and output\n * a YError with a code and some params as debug values.\n * @param {Error} err\n * The error to cast\n * @param {string} [errorCode = 'E_UNEXPECTED']\n * The error code corresponding to the actual error\n * @param {...any} [params]\n * Some additional debugging values\n * @return {YError}\n * The wrapped error\n */\nYError.cast = function yerrorCast(err, ...params) {\n if (_looksLikeAYError(err)) {\n return err;\n }\n return YError.wrap.apply(YError, [err].concat(params));\n};\n\n/**\n * Same than `YError.wrap()` but preserves the code\n * and the debug values of the error if it is\n * already an instance of the YError constructor.\n * @param {Error} err\n * The error to bump\n * @param {string} [errorCode = 'E_UNEXPECTED']\n * The error code corresponding to the actual error\n * @param {...any} [params]\n * Some additional debugging values\n * @return {YError}\n * The wrapped error\n */\nYError.bump = function yerrorBump(err, ...params) {\n if (_looksLikeAYError(err)) {\n return YError.wrap.apply(YError, [err, err.code].concat(err.params));\n }\n return YError.wrap.apply(YError, [err].concat(params));\n};\n\n/**\n * Allow to print a stack from anything (especially catched\n * errors that may or may not contain errors 🤷).\n * @param {Error} err\n * The error to print\n * @return {string}\n * The stack trace if any\n */\nexport function printStackTrace(err) {\n return typeof err === 'object' && typeof err.stack === 'function'\n ? err.stack()\n : `[no_stack_trace]: error is ${\n err != null && typeof err.toString === 'function'\n ? err.toString()\n : typeof err\n }`;\n}\n\n// In order to keep compatibility through major versions\n// we have to make kind of an cross major version instanceof\nfunction _looksLikeAYError(err) {\n return (\n err instanceof YError ||\n (err.constructor &&\n err.constructor.name &&\n err.constructor.name.endsWith('Error') &&\n 'string' === typeof err.code &&\n _looksLikeAYErrorCode(err.code) &&\n err.params &&\n err.params instanceof Array)\n );\n}\n\nfunction _looksLikeAYErrorCode(str) {\n return /^([A-Z0-9_]+)$/.test(str);\n}\n\nexport default YError;\nexport { YError };\n"],"mappings":";;;;;;;;AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAEA;AACA;AACA;AACA;AACA;IACMA,M;;;EACJ;AACF;AACA;AACA;AACA;AACA;AACA;AACA;EACE,gBAAYC,aAAZ,EAA2BC,SAA3B,EAAsC,GAAGC,MAAzC,EAAiD;IAAA;;IAAA;;IAC/C;IACA,IAAI,EAAEF,aAAa,YAAYG,KAA3B,CAAJ,EAAuC;MAC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MAAb,CAA1B,CAAP;AACD,CALD;AAOA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;;AACO,SAAS4B,eAAT,CAAyBZ,GAAzB,EAA8B;EACnC,OAAO,OAAOA,GAAP,KAAe,QAAf,IAA2B,OAAOA,GAAG,CAACN,KAAX,KAAqB,UAAhD,GACHM,GAAG,CAACN,KAAJ,EADG,GAEF,8BACCM,GAAG,IAAI,IAAP,IAAe,OAAOA,GAAG,CAACX,QAAX,KAAwB,UAAvC,GACIW,GAAG,CAACX,QAAJ,EADJ,GAEI,OAAOW,GACZ,EANL;AAOD,C,CAED;AACA;;;AACA,SAASQ,iBAAT,CAA2BR,GAA3B,EAAgC;EAC9B,OACEA,GAAG,YAAYnB,MAAf,IACCmB,GAAG,CAACR,WAAJ,IACCQ,GAAG,CAACR,WAAJ,CAAgBJ,IADjB,IAECY,GAAG,CAACR,WAAJ,CAAgBJ,IAAhB,CAAqByB,QAArB,CAA8B,OAA9B,CAFD,IAGC,aAAa,OAAOb,GAAG,CAACb,IAHzB,IAICgB,qBAAqB,CAACH,GAAG,CAACb,IAAL,CAJtB,IAKCa,GAAG,CAAChB,MALL,IAMCgB,GAAG,CAAChB,MAAJ,YAAsBC,KAR1B;AAUD;;AAED,SAASkB,qBAAT,CAA+BW,GAA/B,EAAoC;EAClC,OAAO,iBAAiBC,IAAjB,CAAsBD,GAAtB,CAAP;AACD;;eAEcjC,M"}
|
package/dist/index.mjs
CHANGED
|
@@ -100,6 +100,10 @@ YError.bump = function yerrorBump(err, ...params) {
|
|
|
100
100
|
return YError.wrap.apply(YError, [err].concat(params));
|
|
101
101
|
};
|
|
102
102
|
|
|
103
|
+
export function printStackTrace(err) {
|
|
104
|
+
return typeof err === 'object' && typeof err.stack === 'function' ? err.stack() : `[no_stack_trace]: error is ${err != null && typeof err.toString === 'function' ? err.toString() : typeof err}`;
|
|
105
|
+
}
|
|
106
|
+
|
|
103
107
|
function _looksLikeAYError(err) {
|
|
104
108
|
return err instanceof YError || err.constructor && err.constructor.name && err.constructor.name.endsWith('Error') && 'string' === typeof err.code && _looksLikeAYErrorCode(err.code) && err.params && err.params instanceof Array;
|
|
105
109
|
}
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.mjs","names":["os","YError","wrappedErrors","errorCode","params","Array","concat","code","name","toString","Error","captureStackTrace","constructor","length","stack","EOL","join","wrap","yerrorWrap","err","yError","wrappedErrorIsACode","_looksLikeAYErrorCode","message","push","cast","yerrorCast","_looksLikeAYError","apply","bump","yerrorBump","endsWith","str","test"],"sources":["../src/index.js"],"sourcesContent":["import os from 'os';\n\n/**\n * An YError class able to contain some params and\n * print better stack traces\n * @extends Error\n */\nclass YError extends Error {\n /**\n * Creates a new YError with an error code\n * and some params as debug values.\n * @param {string} [errorCode = 'E_UNEXPECTED']\n * The error code corresponding to the actual error\n * @param {...any} [params]\n * Some additional debugging values\n */\n constructor(wrappedErrors, errorCode, ...params) {\n // Detecting if wrappedErrors are passed\n if (!(wrappedErrors instanceof Array)) {\n params = ('undefined' === typeof errorCode ? [] : [errorCode]).concat(\n params,\n );\n errorCode = wrappedErrors;\n wrappedErrors = [];\n }\n\n // Call the parent constructor\n super(errorCode);\n\n // Filling error\n this.code = errorCode || 'E_UNEXPECTED';\n this.params = params;\n this.wrappedErrors = wrappedErrors;\n this.name = this.toString();\n\n if (Error.captureStackTrace) {\n Error.captureStackTrace(this, this.constructor);\n }\n }\n\n toString() {\n return (\n (this.wrappedErrors.length\n ? // eslint-disable-next-line\n this.wrappedErrors[this.wrappedErrors.length - 1].stack + os.EOL\n : '') +\n this.constructor.name +\n ': ' +\n this.code +\n ' (' +\n this.params.join(', ') +\n ')'\n );\n }\n}\n\n/**\n * Wraps any error and output a YError with an error\n * code and some params as debug values.\n * @param {Error} err\n * The error to wrap\n * @param {string} [errorCode = 'E_UNEXPECTED']\n * The error code corresponding to the actual error\n * @param {...any} [params]\n * Some additional debugging values\n * @return {YError}\n * The wrapped error\n */\nYError.wrap = function yerrorWrap(err, errorCode, ...params) {\n let yError = null;\n const wrappedErrorIsACode = _looksLikeAYErrorCode(err.message);\n const wrappedErrors = (err.wrappedErrors || []).concat(err);\n\n if (!errorCode) {\n if (wrappedErrorIsACode) {\n errorCode = err.message;\n } else {\n errorCode = 'E_UNEXPECTED';\n }\n }\n if (err.message && !wrappedErrorIsACode) {\n params.push(err.message);\n }\n yError = new YError(wrappedErrors, errorCode, ...params);\n return yError;\n};\n\n/**\n * Return a YError as is or wraps any other error and output\n * a YError with a code and some params as debug values.\n * @param {Error} err\n * The error to cast\n * @param {string} [errorCode = 'E_UNEXPECTED']\n * The error code corresponding to the actual error\n * @param {...any} [params]\n * Some additional debugging values\n * @return {YError}\n * The wrapped error\n */\nYError.cast = function yerrorCast(err, ...params) {\n if (_looksLikeAYError(err)) {\n return err;\n }\n return YError.wrap.apply(YError, [err].concat(params));\n};\n\n/**\n * Same than `YError.wrap()` but preserves the code\n * and the debug values of the error if it is\n * already an instance of the YError constructor.\n * @param {Error} err\n * The error to bump\n * @param {string} [errorCode = 'E_UNEXPECTED']\n * The error code corresponding to the actual error\n * @param {...any} [params]\n * Some additional debugging values\n * @return {YError}\n * The wrapped error\n */\nYError.bump = function yerrorBump(err, ...params) {\n if (_looksLikeAYError(err)) {\n return YError.wrap.apply(YError, [err, err.code].concat(err.params));\n }\n return YError.wrap.apply(YError, [err].concat(params));\n};\n\n// In order to keep compatibility through major versions\n// we have to make kind of an cross major version instanceof\nfunction _looksLikeAYError(err) {\n return (\n err instanceof YError ||\n (err.constructor &&\n err.constructor.name &&\n err.constructor.name.endsWith('Error') &&\n 'string' === typeof err.code &&\n _looksLikeAYErrorCode(err.code) &&\n err.params &&\n err.params instanceof Array)\n );\n}\n\nfunction _looksLikeAYErrorCode(str) {\n return /^([A-Z0-9_]+)$/.test(str);\n}\n\nexport default YError;\nexport { YError };\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;AAAA,OAAOA,EAAP,MA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index.mjs","names":["os","YError","wrappedErrors","errorCode","params","Array","concat","code","name","toString","Error","captureStackTrace","constructor","length","stack","EOL","join","wrap","yerrorWrap","err","yError","wrappedErrorIsACode","_looksLikeAYErrorCode","message","push","cast","yerrorCast","_looksLikeAYError","apply","bump","yerrorBump","printStackTrace","endsWith","str","test"],"sources":["../src/index.js"],"sourcesContent":["import os from 'os';\n\n/**\n * An YError class able to contain some params and\n * print better stack traces\n * @extends Error\n */\nclass YError extends Error {\n /**\n * Creates a new YError with an error code\n * and some params as debug values.\n * @param {string} [errorCode = 'E_UNEXPECTED']\n * The error code corresponding to the actual error\n * @param {...any} [params]\n * Some additional debugging values\n */\n constructor(wrappedErrors, errorCode, ...params) {\n // Detecting if wrappedErrors are passed\n if (!(wrappedErrors instanceof Array)) {\n params = ('undefined' === typeof errorCode ? [] : [errorCode]).concat(\n params,\n );\n errorCode = wrappedErrors;\n wrappedErrors = [];\n }\n\n // Call the parent constructor\n super(errorCode);\n\n // Filling error\n this.code = errorCode || 'E_UNEXPECTED';\n this.params = params;\n this.wrappedErrors = wrappedErrors;\n this.name = this.toString();\n\n if (Error.captureStackTrace) {\n Error.captureStackTrace(this, this.constructor);\n }\n }\n\n toString() {\n return (\n (this.wrappedErrors.length\n ? // eslint-disable-next-line\n this.wrappedErrors[this.wrappedErrors.length - 1].stack + os.EOL\n : '') +\n this.constructor.name +\n ': ' +\n this.code +\n ' (' +\n this.params.join(', ') +\n ')'\n );\n }\n}\n\n/**\n * Wraps any error and output a YError with an error\n * code and some params as debug values.\n * @param {Error} err\n * The error to wrap\n * @param {string} [errorCode = 'E_UNEXPECTED']\n * The error code corresponding to the actual error\n * @param {...any} [params]\n * Some additional debugging values\n * @return {YError}\n * The wrapped error\n */\nYError.wrap = function yerrorWrap(err, errorCode, ...params) {\n let yError = null;\n const wrappedErrorIsACode = _looksLikeAYErrorCode(err.message);\n const wrappedErrors = (err.wrappedErrors || []).concat(err);\n\n if (!errorCode) {\n if (wrappedErrorIsACode) {\n errorCode = err.message;\n } else {\n errorCode = 'E_UNEXPECTED';\n }\n }\n if (err.message && !wrappedErrorIsACode) {\n params.push(err.message);\n }\n yError = new YError(wrappedErrors, errorCode, ...params);\n return yError;\n};\n\n/**\n * Return a YError as is or wraps any other error and output\n * a YError with a code and some params as debug values.\n * @param {Error} err\n * The error to cast\n * @param {string} [errorCode = 'E_UNEXPECTED']\n * The error code corresponding to the actual error\n * @param {...any} [params]\n * Some additional debugging values\n * @return {YError}\n * The wrapped error\n */\nYError.cast = function yerrorCast(err, ...params) {\n if (_looksLikeAYError(err)) {\n return err;\n }\n return YError.wrap.apply(YError, [err].concat(params));\n};\n\n/**\n * Same than `YError.wrap()` but preserves the code\n * and the debug values of the error if it is\n * already an instance of the YError constructor.\n * @param {Error} err\n * The error to bump\n * @param {string} [errorCode = 'E_UNEXPECTED']\n * The error code corresponding to the actual error\n * @param {...any} [params]\n * Some additional debugging values\n * @return {YError}\n * The wrapped error\n */\nYError.bump = function yerrorBump(err, ...params) {\n if (_looksLikeAYError(err)) {\n return YError.wrap.apply(YError, [err, err.code].concat(err.params));\n }\n return YError.wrap.apply(YError, [err].concat(params));\n};\n\n/**\n * Allow to print a stack from anything (especially catched\n * errors that may or may not contain errors 🤷).\n * @param {Error} err\n * The error to print\n * @return {string}\n * The stack trace if any\n */\nexport function printStackTrace(err) {\n return typeof err === 'object' && typeof err.stack === 'function'\n ? err.stack()\n : `[no_stack_trace]: error is ${\n err != null && typeof err.toString === 'function'\n ? err.toString()\n : typeof err\n }`;\n}\n\n// In order to keep compatibility through major versions\n// we have to make kind of an cross major version instanceof\nfunction _looksLikeAYError(err) {\n return (\n err instanceof YError ||\n (err.constructor &&\n err.constructor.name &&\n err.constructor.name.endsWith('Error') &&\n 'string' === typeof err.code &&\n _looksLikeAYErrorCode(err.code) &&\n err.params &&\n err.params instanceof Array)\n );\n}\n\nfunction _looksLikeAYErrorCode(str) {\n return /^([A-Z0-9_]+)$/.test(str);\n}\n\nexport default YError;\nexport {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N,CAAaF,MAAb,CAA1B,CAAP;AACD,CALD;;AAeA,OAAO,SAAS2B,eAAT,CAAyBZ,GAAzB,EAA8B;EACnC,OAAO,OAAOA,GAAP,KAAe,QAAf,IAA2B,OAAOA,GAAG,CAACL,KAAX,KAAqB,UAAhD,GACHK,GAAG,CAACL,KAAJ,EADG,GAEF,8BACCK,GAAG,IAAI,IAAP,IAAe,OAAOA,GAAG,CAACV,QAAX,KAAwB,UAAvC,GACIU,GAAG,CAACV,QAAJ,EADJ,GAEI,OAAOU,GACZ,EANL;AAOD;;AAID,SAASQ,iBAAT,CAA2BR,GAA3B,EAAgC;EAC9B,OACEA,GAAG,YAAYlB,MAAf,IACCkB,GAAG,CAACP,WAAJ,IACCO,GAAG,CAACP,WAAJ,CAAgBJ,IADjB,IAECW,GAAG,CAACP,WAAJ,CAAgBJ,IAAhB,CAAqBwB,QAArB,CAA8B,OAA9B,CAFD,IAGC,aAAa,OAAOb,GAAG,CAACZ,IAHzB,IAICe,qBAAqB,CAACH,GAAG,CAACZ,IAAL,CAJtB,IAKCY,GAAG,CAACf,MALL,IAMCe,GAAG,CAACf,MAAJ,YAAsBC,KAR1B;AAUD;;AAED,SAASiB,qBAAT,CAA+BW,GAA/B,EAAoC;EAClC,OAAO,iBAAiBC,IAAjB,CAAsBD,GAAtB,CAAP;AACD;;AAED,eAAehC,MAAf;AACA,SAASA,MAAT"}
|
package/package.json
CHANGED
package/src/index.d.ts
CHANGED
package/src/index.js
CHANGED
|
@@ -124,6 +124,24 @@ YError.bump = function yerrorBump(err, ...params) {
|
|
|
124
124
|
return YError.wrap.apply(YError, [err].concat(params));
|
|
125
125
|
};
|
|
126
126
|
|
|
127
|
+
/**
|
|
128
|
+
* Allow to print a stack from anything (especially catched
|
|
129
|
+
* errors that may or may not contain errors 🤷).
|
|
130
|
+
* @param {Error} err
|
|
131
|
+
* The error to print
|
|
132
|
+
* @return {string}
|
|
133
|
+
* The stack trace if any
|
|
134
|
+
*/
|
|
135
|
+
export function printStackTrace(err) {
|
|
136
|
+
return typeof err === 'object' && typeof err.stack === 'function'
|
|
137
|
+
? err.stack()
|
|
138
|
+
: `[no_stack_trace]: error is ${
|
|
139
|
+
err != null && typeof err.toString === 'function'
|
|
140
|
+
? err.toString()
|
|
141
|
+
: typeof err
|
|
142
|
+
}`;
|
|
143
|
+
}
|
|
144
|
+
|
|
127
145
|
// In order to keep compatibility through major versions
|
|
128
146
|
// we have to make kind of an cross major version instanceof
|
|
129
147
|
function _looksLikeAYError(err) {
|