Skip to content

Commit

Permalink
Merge pull request #78 from thiagobustamante/master
Browse files Browse the repository at this point in the history
Default error handler must check the accept header
  • Loading branch information
thiagobustamante committed Nov 1, 2017
2 parents e9a76dd + 24fbe13 commit 23f633d
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM node:7-alpine
FROM node:8-alpine

ENV NODE_ENV production
# Create app directory
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "tree-gateway",
"version": "1.2.0",
"version": "1.2.1",
"homepage": "http://treegateway.org",
"description": "The Tree Gateway API Gateway",
"author": "Thiago da Rosa de Bustamante <trbustamante@gmail.com>",
Expand Down
20 changes: 18 additions & 2 deletions src/error/error-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,25 @@ export class ApiErrorHandler {
if (res.headersSent) { // important to allow default error handler to close connection if headers already sent
return next(err);
}
res.set('Content-Type', 'application/json');
const mime = req.accepts('json', 'xml', 'html', 'text');
res.status(err.statusCode || err.status || 500);
res.json({ error: err.message });
switch (mime) {
case 'json':
res.set('Content-Type', 'application/json');
res.json({ error: err.message });
break;
case 'xml':
res.set('Content-Type', 'application/xml');
res.send(`<error>${err.message}</error>`);
break;
case 'html':
res.set('Content-Type', 'text/html');
res.send(`<html><head></head><body>${err.message}</body></html>`);
break;
default:
res.set('Content-Type', 'text/plain');
res.send(err.message);
}
if (this.logger.isWarnEnabled()) {
this.logger.warn(`Error on API pipeline processing: ${err.message}`);
}
Expand Down

0 comments on commit 23f633d

Please sign in to comment.