From d1f57b1dc33f110cc32c4d2eb58c0ffea130f121 Mon Sep 17 00:00:00 2001 From: Jacopo Andrea Giola Date: Thu, 2 May 2019 15:17:09 +0200 Subject: [PATCH 1/4] Update dependencies --- package.json | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/package.json b/package.json index 468aa4c..8dcd1f6 100644 --- a/package.json +++ b/package.json @@ -23,17 +23,17 @@ }, "homepage": "https://github.com/fastify/fastify-sensible#readme", "devDependencies": { - "fastify": "^2.0.0", + "fastify": "^2.3.0", "standard": "^12.0.1", - "tap": "^12.1.1" + "tap": "^12.7.0" }, "dependencies": { "fast-deep-equal": "^2.0.1", - "fastify-plugin": "^1.3.0", + "fastify-plugin": "^1.5.0", "forwarded": "^0.1.2", - "http-errors": "^1.7.1", - "proxy-addr": "^2.0.4", - "type-is": "^1.6.16", + "http-errors": "^1.7.2", + "proxy-addr": "^2.0.5", + "type-is": "^1.6.18", "vary": "^1.1.2" } } From 65276de8fe32ca570aeafdd7db9995ecd31a4642 Mon Sep 17 00:00:00 2001 From: Jacopo Andrea Giola Date: Thu, 2 May 2019 15:17:18 +0200 Subject: [PATCH 2/4] Change custom error handler. Add an additional properties to the internal server error in case is manually invoked by the user for always allowing the custom message. --- README.md | 4 +++- index.js | 2 +- lib/httpErrors.js | 5 ++++- test/httpErrorsReply.test.js | 20 ++++++-------------- 4 files changed, 14 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 0830627..b2fa7b7 100644 --- a/README.md +++ b/README.md @@ -151,10 +151,12 @@ const [err, user] = await fastify.to( ``` #### Custom error handler -This plugins also adds a custom error handler which hides the error message in case of `500` errors, instead it returns `Something went wrong`.
+This plugins also adds a custom error handler which hides the error message in case of `500` errors that are being generated by throwd errors, instead it returns `Something went wrong`.
This is especially useful if you are using *async* routes, where every uncaught error will be sent back to the user *(but do not worry, the original error message is logged as error in any case)*. If needed, it can be disabled by setting the option `errorHandler` to `false`. +If you return a `500` error via the API provided on `fastify.httpErrors` and `reply` objects, the custom text is always preserved. + ## Contributing Do you feel there is some utility that *everyone can agree on* which is not present?
Open an issue and let's discuss it! Even better a pull request! diff --git a/index.js b/index.js index e0d496e..3eb6678 100644 --- a/index.js +++ b/index.js @@ -38,7 +38,7 @@ function fastifySensible (fastify, opts, next) { if (opts.errorHandler !== false) { fastify.setErrorHandler(function (error, request, reply) { - if (reply.res.statusCode === 500) { + if (reply.res.statusCode === 500 && error.explicitInternalServerError !== true) { request.log.error(error) reply.send(new Error('Something went wrong')) } else { diff --git a/lib/httpErrors.js b/lib/httpErrors.js index 84d4762..de1f971 100644 --- a/lib/httpErrors.js +++ b/lib/httpErrors.js @@ -134,7 +134,10 @@ const httpErrors = { }, internalServerError: function internalServerError (message) { - return new createError.InternalServerError(message) + const error = new createError.InternalServerError(message) + // mark error as explicti to allow custom message + error.explicitInternalServerError = true + return error }, notImplemented: function notImplemented (message) { diff --git a/test/httpErrorsReply.test.js b/test/httpErrorsReply.test.js index f261ce9..7dfe070 100644 --- a/test/httpErrorsReply.test.js +++ b/test/httpErrorsReply.test.js @@ -37,7 +37,7 @@ test('Should generate the correct http error', t => { } else { t.deepEqual(JSON.parse(res.payload), { error: statusCodes[code], - message: code === '500' ? 'Something went wrong' : statusCodes[code], + message: statusCodes[code], statusCode: Number(code) }) } @@ -66,19 +66,11 @@ test('Should generate the correct http error (with custom message)', t => { }, (err, res) => { t.error(err) t.strictEqual(res.statusCode, Number(code)) - if (code === '500') { - t.deepEqual(JSON.parse(res.payload), { - error: statusCodes[code], - message: 'Something went wrong', - statusCode: Number(code) - }) - } else { - t.deepEqual(JSON.parse(res.payload), { - error: statusCodes[code], - message: 'custom', - statusCode: Number(code) - }) - } + t.deepEqual(JSON.parse(res.payload), { + error: statusCodes[code], + message: 'custom', + statusCode: Number(code) + }) }) }) }) From c71ed7cb5e11184399f35674fc6800f986e89adc Mon Sep 17 00:00:00 2001 From: Jacopo Andrea Giola Date: Thu, 2 May 2019 18:27:23 +0200 Subject: [PATCH 3/4] Fix typos --- README.md | 2 +- lib/httpErrors.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index b2fa7b7..92ddc86 100644 --- a/README.md +++ b/README.md @@ -151,7 +151,7 @@ const [err, user] = await fastify.to( ``` #### Custom error handler -This plugins also adds a custom error handler which hides the error message in case of `500` errors that are being generated by throwd errors, instead it returns `Something went wrong`.
+This plugins also adds a custom error handler which hides the error message in case of `500` errors that are being generated by throwed errors, instead it returns `Something went wrong`.
This is especially useful if you are using *async* routes, where every uncaught error will be sent back to the user *(but do not worry, the original error message is logged as error in any case)*. If needed, it can be disabled by setting the option `errorHandler` to `false`. diff --git a/lib/httpErrors.js b/lib/httpErrors.js index de1f971..106b21f 100644 --- a/lib/httpErrors.js +++ b/lib/httpErrors.js @@ -135,7 +135,7 @@ const httpErrors = { internalServerError: function internalServerError (message) { const error = new createError.InternalServerError(message) - // mark error as explicti to allow custom message + // mark error as explicit to allow custom message error.explicitInternalServerError = true return error }, From e1aa595961b0e3946fef6970764693ec77ce619b Mon Sep 17 00:00:00 2001 From: Jacopo Andrea Giola Date: Fri, 3 May 2019 10:06:24 +0200 Subject: [PATCH 4/4] Tweak to the documentation --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 92ddc86..29f477c 100644 --- a/README.md +++ b/README.md @@ -155,7 +155,7 @@ This plugins also adds a custom error handler which hides the error message in c This is especially useful if you are using *async* routes, where every uncaught error will be sent back to the user *(but do not worry, the original error message is logged as error in any case)*. If needed, it can be disabled by setting the option `errorHandler` to `false`. -If you return a `500` error via the API provided on `fastify.httpErrors` and `reply` objects, the custom text is always preserved. +If you return a `500` error via the API provided on `fastify.httpErrors` and `reply` objects, the custom error message is preserved and returned in the HTTP response body. ## Contributing Do you feel there is some utility that *everyone can agree on* which is not present?