Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preserve API invoked internal server error custom messages. #18

Merged
merged 4 commits into from
Jun 15, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 text is always preserved.
JGiola marked this conversation as resolved.
Show resolved Hide resolved

## 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,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"
}
}
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