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鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
Pass descriptive error message when "attachValidation: true" is used #5235
Comments
How are the LOGIN_ERROR_1 LOGIN_ERROR_2 etc generated? |
@mcollina They are hardcoded in the specific error response, or extracted from the enum. Something like this: if (request.validationError?.validation[0]) {
return { status: false, error_code: 'LOGIN_ERROR_1', error_message: 'Descriptive error message. ' };
}
if(userDoesNotExist) {
return { status: false, error_code: 'LOGIN_ERROR_2' };
}
if(passwordInvalid){
return { status: false, error_code: 'LOGIN_ERROR_3' };
} |
I don't really understand what you are asking, but I'd love to see how you would implement this feature. Would you like to send a PR? |
@mcollina Sorry if I didn't explain it properly. 馃槄 Basically, with the "message": "body must have required property 'obj1'" But, with the {
"statusCode": 400,
"code": "FST_ERR_VALIDATION",
"validation": [
{
"instancePath": "",
"schemaPath": "#/required",
"keyword": "required",
"params": {
"missingProperty": "obj1"
},
"message": "must have required property 'obj1'"
}
],
"validationContext": "body"
} It's hard to construct the descriptive error message based on the ajv's raw result, because there can be multiple types of validation errors. So, I am suggesting that even with the |
How? I'm not sure it's possible, given how things are set up. Also, generating that message is not really hard, take a look at our implementation. |
I don't see why it would be hard to implement. The message is generated by |
Prerequisites
馃殌 Feature Proposal
Looks like Fastify already has a great utility that will create descriptive error messages based on the raw "ajv" result.
However, when
attachValidation: true
option is used, only the raw "ajv" result will be passed to therequest.validationError
. Even though this raw result has all the data about the error, constructing a descriptive message requires additional development effort since there are a bunch of use cases to be covered.I am proposing that when
attachValidation: true
option is used, we send back a Fastify-generated error as well, as an additional property to therequest.validationError
.Or even better, just add a new config that will pass only the Fastify-generated error.
Motivation
The motivation for this feature is to keep maximum flexibility for custom error handling while making it easy to have descriptive error messages out of the box.
Example
Let's take Login functionality as an example.
I would like to have consistent error handling for the whole API, where each error will have a unique error code.
Only in case of input validation, I would like to send back additional context of what failed.
So, if any of the input validation failed, I would send back something like this:
If the user with the requested email is not found, I would send back something like this:
If a password is not correct, I would send back something like this:
The text was updated successfully, but these errors were encountered: