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