Skip to content

Instantly share code, notes, and snippets.

@chemzqm
Created October 30, 2013 05:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chemzqm/7227715 to your computer and use it in GitHub Desktop.
Save chemzqm/7227715 to your computer and use it in GitHub Desktop.
pretty error format for node
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