Skip to content

Commit

Permalink
[[FEAT]] Use cause error message in wrapper error
Browse files Browse the repository at this point in the history
[[FIX]] Hide 5XX error details in text/plain
  • Loading branch information
jmendiara committed Apr 24, 2017
1 parent 5a32ef3 commit 3369361
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 11 deletions.
5 changes: 3 additions & 2 deletions lib/therror-connect.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ function errorHandler(options) {
return function errorHandlerMiddleware(err, req, res, next) {

if (!err.isTherror || !isServerTherror(err)) {
err = new UnexpectedErrorClass(err, 'Unexpected Error');
err = new UnexpectedErrorClass(err);
}

log && _.isFunction(err.log) && err.log({
Expand Down Expand Up @@ -96,7 +96,8 @@ function errorHandler(options) {

// node 6 adds the stacktrace when toStringing an error
function errToString(err) {
return err.name + ': ' + err.message;
let payload = err.toPayload();
return payload.error + ': ' + payload.message;
}

function isServerTherror(obj) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
"therror": "^3.0.0"
},
"peerDependencies": {
"therror": ">=1.0.0"
"therror": ">=3.0.0"
},
"dependencies": {
"accepts": "^1.3.3",
Expand Down
16 changes: 8 additions & 8 deletions test/therror-connect.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,38 +16,38 @@ describe('errorHandler()', function() {
});

describe('fallback to ServerError', function() {
it('should catch errors and transform to Unexpected ServerError', function(done) {
it('should catch errors and transform to Internal Server Error', function(done) {
var error = new Error('boom!');
var server = createServer(error);

request(server)
.get('/')
.set('Accept', 'text/plain')
.expect(500, 'InternalServerError: Unexpected Error', done);
.expect(500, 'InternalServerError: An internal server error occurred', done);
});

it('should catch strings and transform to Unexpected ServerError', function(done) {
it('should catch strings and transform to Internal Server Error', function(done) {
var server = createServer('boom!');
request(server)
.get('/')
.set('Accept', 'text/plain')
.expect(500, 'InternalServerError: Unexpected Error', done);
.expect(500, 'InternalServerError: An internal server error occurred', done);
});

it('should catch numbers and transform to Unexpected ServerError', function(done) {
it('should catch numbers and transform to Internal Server Error', function(done) {
var server = createServer(1);
request(server)
.get('/')
.set('Accept', 'text/plain')
.expect(500, 'InternalServerError: Unexpected Error', done);
.expect(500, 'InternalServerError: An internal server error occurred', done);
});

it('should catch objects and transform to Unexpected ServerError', function(done) {
it('should catch objects and transform to Internal Server Error', function(done) {
var server = createServer({foo: 1});
request(server)
.get('/')
.set('Accept', 'text/plain')
.expect(500, 'InternalServerError: Unexpected Error', done);
.expect(500, 'InternalServerError: An internal server error occurred', done);
});

it('should use UnexpectedError class provided', function(done) {
Expand Down

0 comments on commit 3369361

Please sign in to comment.