Skip to content

Commit

Permalink
Merge pull request #18 from JGiola/feature/preserve-api-internal-serv…
Browse files Browse the repository at this point in the history
…er-error-message

Preserve API invoked internal server error custom messages.
  • Loading branch information
mcollina authored Jun 15, 2019
2 parents 7cb00bb + e1aa595 commit 09e2fef
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 23 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.<br>
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`.<br>
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 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?<br>
Open an issue and let's discuss it! Even better a pull request!
Expand Down
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
5 changes: 4 additions & 1 deletion lib/httpErrors.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,10 @@ const httpErrors = {
},

internalServerError: function internalServerError (message) {
return new createError.InternalServerError(message)
const error = new createError.InternalServerError(message)
// mark error as explicit to allow custom message
error.explicitInternalServerError = true
return error
},

notImplemented: function notImplemented (message) {
Expand Down
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,18 @@
},
"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": {
"@types/node": "^12.0.7",
"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"
}
}
20 changes: 6 additions & 14 deletions test/httpErrorsReply.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
}
Expand Down Expand Up @@ -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)
})
})
})
})
Expand Down

0 comments on commit 09e2fef

Please sign in to comment.