mirror of
https://github.com/go-kratos/kratos.git
synced 2025-01-10 00:29:01 +02:00
validate 使用 grpc 标准异常处理 (#627)
This commit is contained in:
parent
521d240568
commit
9636e6bcf0
@ -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…
Reference in New Issue
Block a user