Skip to content

Commit

Permalink
feat(internal): add function to stub errors with a toJSON method
Browse files Browse the repository at this point in the history
  • Loading branch information
KyleRoss committed Apr 9, 2021
1 parent 647d58d commit d0707bd
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions lib/LogMessage.js
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,35 @@ class LogMessage {
)
);
}

/**
* Stubs an Error or Error-like object to include a toJSON method.
* @static
* @param {Error} err An Error or Error-like object.
* @return {Error}
*/
static stubError(err) {
err.toJSON = function () {
const keys = [
'name',
'message',
'stack'
].concat(Object.keys(err));

return keys.reduce((obj, key) => {
if(Object.prototype.hasOwnProperty.call(err, key)) {
const val = err[key];
if(typeof val !== 'function') {
obj[key] = val;
}
}

return obj;
}, {});
};

return err;
}
}

LogMessage.symbols = symbols;
Expand Down

0 comments on commit d0707bd

Please sign in to comment.