yerror 6.2.0 → 6.2.1
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 +9 -0
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +1 -1
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
- package/src/index.js +2 -2
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,12 @@
|
|
|
1
|
+
## [6.2.1](https://github.com/nfroidure/yerror/compare/v6.2.0...v6.2.1) (2022-12-19)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
### Bug Fixes
|
|
5
|
+
|
|
6
|
+
* **lib:** fix stack printing ([edf5fd3](https://github.com/nfroidure/yerror/commit/edf5fd32126a1689863cc995859243251d25c8b4))
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
|
|
1
10
|
# [6.2.0](https://github.com/nfroidure/yerror/compare/v6.1.1...v6.2.0) (2022-12-19)
|
|
2
11
|
|
|
3
12
|
|
package/dist/index.js
CHANGED
|
@@ -179,7 +179,7 @@ YError.bump = function yerrorBump(err, ...params) {
|
|
|
179
179
|
|
|
180
180
|
|
|
181
181
|
function printStackTrace(err) {
|
|
182
|
-
return typeof err === 'object' && typeof err.stack === '
|
|
182
|
+
return typeof err === 'object' && typeof err.stack === 'string' ? err.stack : `[no_stack_trace]: error is ${err != null && typeof err.toString === 'function' ? err.toString() : typeof err}`;
|
|
183
183
|
} // In order to keep compatibility through major versions
|
|
184
184
|
// we have to make kind of an cross major version instanceof
|
|
185
185
|
|
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","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 === '
|
|
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 === 'string'\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;MACrCD,MAAM,GAAG,CAAC,gBAAgB,OAAOD,SAAvB,GAAmC,EAAnC,GAAwC,CAACA,SAAD,CAAzC,EAAsDG,MAAtD,CACPF,MADO,CAAT;MAGAD,SAAS,GAAGD,aAAZ;MACAA,aAAa,GAAG,EAAhB;IACD,CAR8C,CAU/C;;;IACA,4EAAMC,SAAN,GAX+C,CAa/C;;IACA,MAAKI,IAAL,GAAYJ,SAAS,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AOA,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
|
@@ -101,7 +101,7 @@ YError.bump = function yerrorBump(err, ...params) {
|
|
|
101
101
|
};
|
|
102
102
|
|
|
103
103
|
export function printStackTrace(err) {
|
|
104
|
-
return typeof err === 'object' && typeof err.stack === '
|
|
104
|
+
return typeof err === 'object' && typeof err.stack === 'string' ? err.stack : `[no_stack_trace]: error is ${err != null && typeof err.toString === 'function' ? err.toString() : typeof err}`;
|
|
105
105
|
}
|
|
106
106
|
|
|
107
107
|
function _looksLikeAYError(err) {
|
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","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 === '
|
|
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","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 === 'string'\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,OAAOA,EAAP,MAAe,IAAf;;IAOMC,M;;;EASJ,gBAAYC,aAAZ,EAA2BC,SAA3B,EAAsC,GAAGC,MAAzC,EAAiD;IAAA;;IAAA;;IAE/C,IAAI,EAAEF,aAAa,YAAYG,KAA3B,CAAJ,EAAuC;MACrCD,MAAM,GAAG,CAAC,gBAAgB,OAAOD,SAAvB,GAAmC,EAAnC,GAAwC,CAACA,SAAD,CAAzC,EAAsDG,MAAtD,CACPF,MADO,CAAT;MAGAD,SAAS,GAAGD,aAAZ;MACAA,aAAa,GAAG,EAAhB;IACD;;IAGD,4EAAMC,SAAN;IAGA,MAAKI,IAAL,GAAYJ,SAAS,IAAI,cAAzB;IACA,MAAKC,MAAL,GAAcA,MAAd;IACA,MAAKF,aAAL,GAAqBA,aAArB;IACA,MAAKM,IAAL,GAAY,MAAKC,QAAL,EAAZ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AAG,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.js
CHANGED
|
@@ -133,8 +133,8 @@ YError.bump = function yerrorBump(err, ...params) {
|
|
|
133
133
|
* The stack trace if any
|
|
134
134
|
*/
|
|
135
135
|
export function printStackTrace(err) {
|
|
136
|
-
return typeof err === 'object' && typeof err.stack === '
|
|
137
|
-
? err.stack
|
|
136
|
+
return typeof err === 'object' && typeof err.stack === 'string'
|
|
137
|
+
? err.stack
|
|
138
138
|
: `[no_stack_trace]: error is ${
|
|
139
139
|
err != null && typeof err.toString === 'function'
|
|
140
140
|
? err.toString()
|