From 3369361a604fe7e7905ea92930d3cd9159b06bd3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javier=20Mendiara=20Ca=C3=B1ardo?= Date: Mon, 24 Apr 2017 17:19:36 +0200 Subject: [PATCH] [[FEAT]] Use cause error message in wrapper error [[FIX]] Hide 5XX error details in text/plain --- lib/therror-connect.js | 5 +++-- package.json | 2 +- test/therror-connect.spec.js | 16 ++++++++-------- 3 files changed, 12 insertions(+), 11 deletions(-) diff --git a/lib/therror-connect.js b/lib/therror-connect.js index 142a3ff..e40aa62 100644 --- a/lib/therror-connect.js +++ b/lib/therror-connect.js @@ -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({ @@ -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) { diff --git a/package.json b/package.json index 8a26f16..86bdb63 100644 --- a/package.json +++ b/package.json @@ -46,7 +46,7 @@ "therror": "^3.0.0" }, "peerDependencies": { - "therror": ">=1.0.0" + "therror": ">=3.0.0" }, "dependencies": { "accepts": "^1.3.3", diff --git a/test/therror-connect.spec.js b/test/therror-connect.spec.js index f7713cc..84b8c99 100644 --- a/test/therror-connect.spec.js +++ b/test/therror-connect.spec.js @@ -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) {