validation: set errdetails.BadRequest_FieldViolation slice in errdetails.BadRequest as details

Signed-off-by: Adphi <philippe.adrien.nousse@gmail.com>
This commit is contained in:
Adphi 2022-06-07 12:49:58 +02:00
parent ec06b7c4a2
commit 5662486b3b
Signed by: adphi
GPG Key ID: 46BE4062DB2397FF

View File

@ -5,7 +5,6 @@ import (
"google.golang.org/genproto/googleapis/rpc/errdetails" "google.golang.org/genproto/googleapis/rpc/errdetails"
"google.golang.org/grpc" "google.golang.org/grpc"
"google.golang.org/protobuf/proto"
"go.linka.cloud/grpc/errors" "go.linka.cloud/grpc/errors"
"go.linka.cloud/grpc/interceptors" "go.linka.cloud/grpc/interceptors"
@ -52,15 +51,15 @@ func errToStatus(err error) error {
} }
switch v := err.(type) { switch v := err.(type) {
case validatorError: case validatorError:
return errors.InvalidArgumentd(err, validatorErrorToGrpc(v)) return errors.InvalidArgumentd(err, &errdetails.BadRequest{FieldViolations: []*errdetails.BadRequest_FieldViolation{validatorErrorToGrpc(v)}})
case validatorMultiError: case validatorMultiError:
var details []proto.Message details := &errdetails.BadRequest{}
for _, v := range v.AllErrors() { for _, v := range v.AllErrors() {
if d, ok := v.(validatorError); ok { if d, ok := v.(validatorError); ok {
details = append(details, validatorErrorToGrpc(d)) details.FieldViolations = append(details.FieldViolations, validatorErrorToGrpc(d))
} }
} }
return errors.InvalidArgumentd(err, details...) return errors.InvalidArgumentd(err, details)
default: default:
return errors.InvalidArgument(err) return errors.InvalidArgument(err)
} }