1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-12 20:57:12 +01:00
metamask-extension/app/scripts/lib/cleanErrorStack.js
Thomas Huang 02a4da9ad2
Fix 12145 - Ledger errors being set as Error: [object, object] (#12176)
* Fixes #12145

When transactions fail, preserve error message when presented with an error object. Fallback to stringified error for other cases.

* Perserve error stack when err.stack is present, else set stack to message.

* Use optional chaining
2021-09-22 10:41:23 -07:00

23 lines
541 B
JavaScript

/**
* Returns error without stack trace for better UI display
* @param {Error} err - error
* @returns {Error} Error with clean stack trace.
*/
export default function cleanErrorStack(err) {
let { name } = err;
name = name === undefined ? 'Error' : String(name);
let msg = err.message;
msg = msg === undefined ? '' : String(msg);
if (name === '') {
err.stack = err.message;
} else if (msg === '') {
err.stack = err.name;
} else if (!err.stack) {
err.stack = `${err.name}: ${err.message}`;
}
return err;
}