Created
April 18, 2016 03:48
-
-
Save 1yx/5bbdc3579008e6ec6c821a98b3c949be to your computer and use it in GitHub Desktop.
JSON stringify an Error in Express
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
// development error handler | |
// will print stacktrace | |
if (app.get('env') === 'development') { | |
app.use(function(err, req, res, next) { | |
res.status(err.status || 500); | |
function err2JSON (err) { | |
var obj = JSON.parse( | |
JSON.stringify(err, Object.getOwnPropertyNames(err)) | |
); | |
if (obj.stack) { | |
obj.stack = obj.stack.split('\n'); | |
} | |
return obj; | |
} | |
res.json({ | |
ret: -1, | |
msg: err.message, | |
err: err2JSON(err) | |
}); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment