Created
October 30, 2013 05:46
-
-
Save chemzqm/7227715 to your computer and use it in GitHub Desktop.
pretty error format for node
This file contains hidden or 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
require('longjohn') | |
var prettyjson = require('prettyjson') | |
function formatJson(object) { | |
// adds 4 spaces in front of each line | |
var json = prettyjson.render(object) | |
json = json.split('\n').join('\n ') | |
return ' ' + json | |
} | |
function playNiceError(error) { | |
// remove longjohn properties that break prettyjson | |
delete error.__cached_trace__ | |
delete error.__previous__ | |
// remove domain information we probably don't need | |
delete error.domain | |
} | |
function logError(error) { | |
playNiceError(error) | |
// Adding for fun; not for real use | |
error.code = '500B' | |
var metadata = formatJson(error) | |
var stack = error.stack.trim() | |
message = stack + '\n' | |
if (metadata.trim() !== '') { | |
message += ' Metadata:\n' + metadata | |
} | |
console.error(message) | |
} | |
process.on('uncaughtException', logError) | |
function throwError() { throw new Error('foo') } | |
setTimeout(throwError, Math.random()*1000) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment