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
Additional error logging when using setErrorHandler #4048
Comments
The error system in Fastify v4 has been refactored. If you want to customize things, you must do: fastify.setErrorHandler(function (error, request, reply) {
// some custom logging operations
const toSend = {
"message": error.message
"error": error.error
"statusCode": error.statusCode || 500 // default status code
}
reply.core(toSend.statusCode).send(toSend);
} Fastify will only set the http status code in the default error handler, which is not customizable. |
We should include this in the https://github.com/fastify/fastify/blob/main/docs/Guides/Migration-Guide-V4.md |
Hi @mcollina, thanks for the feedback. I tried the described approach, but still have some troubles. But first the good news: the additional logging is gone, as soon as I switched to the reply.code().send() statement 👍 fastify.setErrorHandler(function (error, request, reply) {
// some custom logging operations
const errorResponse = {
message: error.message,
error: error.error, // still missing in response json
statusCode: error.statusCode || 500
};
reply.code(errorResponse.statusCode).send(errorResponse);
}); I tested the setErrorHandler by setting up a route, that validates an incoming request header with the help of a JSON schema. The JSON schema defines a mandatory header property "Client-Locale". // JSON Schema for mandatory header property 'Client-Locale'
{
type: 'object',
required: ['Client-Locale'],
properties: {
'Client-Locale': {type: 'string'}
}
} In the prior fastify version, the setErrorHandler returned with a status code 400 as well as the error.error property "Bad Request", when the Client-Locale property was not present in the request header. Now the setErrorHandler returns the status code 500 and the "error"-property with the value "Bad Request" is missing. // Error object with fastify 3.26.0
// HTTP status code in header: 400
{
"message": "headers should have required property 'client-locale'",
"error": "Bad Request"
"statusCode": 400
}
// Error object with fastify 4.0.1
// HTTP status code in header: 500
{
"message": "headers must have required property 'client-locale'",
"statusCode": 500
} Is there anything else I have to consider for validation errors? |
Can you provide steps to reproduce? We often need a reproducible example, e.g. some code that allows someone else to recreate your problem by just copying and pasting it. If it involves more than a couple of different file, create a new repository on GitHub and add a link to that. |
Hi @mcollina, sure, I prepared a GitHub repo to demonstrate the issue: https://github.com/koanplaned/fastify-error-handler-issue It includes two fastify sample applications, which are the same except for the setErrorHandler implementation. Hope you find it useful to reproduce the issue. |
In the v4 example, you have to change handler: async function (request, reply) {
reply.send({ hello: 'world' });
} into handler: async function (request, reply) {
reply.send({ hello: 'world' });
return reply
} or handler: function (request, reply) {
reply.send({ hello: 'world' });
} |
Hi @mcollina, I tried both options, but this didn't change the result. The error handler still returns with error code 500 instead of 400 and with missing error.error property for both options. // Result with updated route handler implementation
HTTP/1.1 500 Internal Server Error
content-type: application/json; charset=utf-8
content-length: 82
Date: Sat, 18 Jun 2022 12:36:20 GMT
Connection: close
{
"message": "headers must have required property 'client-locale'",
"statusCode": 500
} I also played around with the setErrorHandler function a little bit more, e.g. fastify.setErrorHandler(function (error, request, reply) {
reply.send(error);
}
// or
fastify.setErrorHandler(async function (error, request, reply) {
reply.send(error);
return reply;
} With these tweaks, the error response was correct - but the additional error logging happened again. I added a new folder v4-2 and updated the code with the route handler adjustments: https://github.com/koanplaned/fastify-error-handler-issue/tree/master/v4-2 Please note: GET /tests/option-1 -> async route handler with additional return statement |
I finally understood the problem you are facing. we are not setting |
Yes, and it seems the same applies for error.error |
Would you like to send a Pull Request to address this issue? Remember to add unit tests. The function to update is likely Line 107 in 2324549
|
As part of the error handling refactoring of #3261, we should not be setting a custom status code for validation routes. We should rely only on the error.statusCode property instead and leave the user full customization capabilities. Unfortunately a change was missed. Fixes: #4048 Ref: #3261 Signed-off-by: Matteo Collina <hello@matteocollina.com>
Hi all,
I recently upgraded from fastify version 3.26.0 to 4.0.1 and now have an issue with the setErrorHandler method.
My setErrorHandler is implemented like this
In the previous version 3.26.0, the application did just my custom-defined logging and then returned a well-formed json like this:
Since the 4.0.1 version, there is now an additional error logging done by fastify, which I can not turn off. I narrowed the issue down to following statement:
My observation: If I send the "complete" error object, the additional logging occurs, but when I just send parts of the error (e.g. the error message) the additional logging is gone:
Is there a way to turn off the additional logging, when returning the "full" error object?
I want to use the "full" object, since I also need the statusCode and the error in the reply message like this:
As a workaround, I tried to construct this object myself, but I don't understand, how I can access the actual error statusCode to send it back, for example like this:
The property "error.statusCode" is undefined on the error object, so I guess there must be another way to access it somehow.
Help would be really appreciated here :-)
The text was updated successfully, but these errors were encountered: