Created
June 22, 2015 21:12
-
-
Save joseym/61e1f12c4ab216cc23ca to your computer and use it in GitHub Desktop.
I wanted to have better error returns in my node apps, this gives a better stack trace for json responses
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Usage | |
var err = new Error('Hey th\'upid, you didn\'t Catch your shenanigans!'); | |
console.log(err.toJSON()); | |
// If run in the chrome console | |
// this is exceptionally usefully if debuggong locally, but stacks aren't so great in production | |
return { | |
stack: "Error: Hey th'upid, you didn't Catch your shenanigans! <anonymous>:2:13 at Object.InjectedScript._evaluateOn (<anonymous>:895:140) at Object.InjectedScript._evaluateAndWrap (<anonymous>:828:34)at Object.InjectedScript.evaluate (<anonymous>:694:21)", | |
message: "Hey th'upid, you didn't Catch your shenanigans!" | |
} | |
/** | |
* Pretty Errors | |
*/ | |
Object.defineProperty(Error.prototype, 'toJSON', { | |
value: function () { | |
var alt = {}; | |
Object.getOwnPropertyNames(this).forEach(function (key) { | |
alt[key] = this[key]; | |
}, this); | |
return alt; | |
}, | |
configurable: true | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment