Skip to content

Commit

Permalink
handler returns error message as json (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
benjie authored Apr 24, 2019
2 parents e1ed462 + 92d9356 commit b80865a
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,16 @@ const handler = (req, res) => {
if (err) {
// eslint-disable-next-line no-console
console.error(err);
res.writeHead(err.status || err.statusCode || 500);
res.end(err.message);
if (!res.headersSent) {
res.statusCode = err.status || err.statusCode || 500;
res.setHeader('Content-Type', 'application/json');
}
res.end(JSON.stringify({ errors: [{message: err.message}] }));
return;
}
if (!res.finished) {
if (!res.headersSent) {
res.writeHead(404);
res.statusCode = 404;
}
res.end(`'${req.url}' not found`);
}
Expand Down

0 comments on commit b80865a

Please sign in to comment.