Closed tocomp closed 9 months ago
Thanks, I found a way to convert ValidationError
in the package has this comments
// A ValidationError is returned if one or more constraints on a message are
// violated. This error type can be converted into a validate.Violations
// message via ToProto.
//
// err = validator.Validate(msg)
// var valErr *ValidationError
// if ok := errors.As(err, &valErr); ok {
// pb := valErr.ToProto()
// // ...
// }
ValidationError = errors.ValidationError
so I wrote the code based on this comments
if err := v.Validate(pr); err != nil {
var valErr *protovalidate.ValidationError
if ok := errors.As(err, &valErr); ok {
return nil, errors.New(
400,
"0",
valErr.ToProto().GetViolations()[0].GetMessage(), // I not check the err length
)
}
return nil, err
}
It's working! The API returns the error message
{
"code": 400,
"reason": "0",
"message": "xxxxxxxxxxxxxxxxxxxxxxxx",
}
Maybe we should add a check for the error length?