Skip to content

Instantly share code, notes, and snippets.

@1yx
Created April 18, 2016 03:48
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 1yx/5bbdc3579008e6ec6c821a98b3c949be to your computer and use it in GitHub Desktop.
Save 1yx/5bbdc3579008e6ec6c821a98b3c949be to your computer and use it in GitHub Desktop.
JSON stringify an Error in Express
// 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