validate 使用 grpc 标准异常处理 (#627)

pull/629/head
Vincent 4 years ago committed by GitHub
parent 521d240568
commit 9636e6bcf0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 8
      pkg/net/rpc/warden/validate.go

@ -2,8 +2,8 @@ package warden
import (
"context"
"github.com/go-kratos/kratos/pkg/ecode"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/grpc"
"gopkg.in/go-playground/validator.v9"
@ -15,7 +15,7 @@ var validate = validator.New()
func (s *Server) validate() grpc.UnaryServerInterceptor {
return func(ctx context.Context, req interface{}, args *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) {
if err = validate.Struct(req); err != nil {
err = ecode.Error(ecode.RequestErr, err.Error())
err = status.Error(codes.InvalidArgument, err.Error())
return
}
resp, err = handler(ctx, req)
@ -30,7 +30,7 @@ func (s *Server) RegisterValidation(key string, fn validator.Func) error {
return validate.RegisterValidation(key, fn)
}
//GetValidate return the default validate
// GetValidate return the default validate
func (s *Server) GetValidate() *validator.Validate {
return validate
}

Loading…
Cancel
Save