File tree 8 files changed +18
-12
lines changed
8 files changed +18
-12
lines changed Original file line number Diff line number Diff line change @@ -20,14 +20,14 @@ func ProtoValidate() middleware.Middleware {
20
20
return func (ctx context.Context , req any ) (reply any , err error ) {
21
21
if msg , ok := req .(proto.Message ); ok {
22
22
if err := protovalidate .Validate (msg ); err != nil {
23
- return nil , errors .BadRequest ("VALIDATOR" , err .Error ()).WithCause (err )
23
+ return nil , errors .BadRequest (errors . ValidatorReason , err .Error ()).WithCause (err )
24
24
}
25
25
}
26
26
27
27
// to compatible with the [old validator](https://github.com/envoyproxy/protoc-gen-validate)
28
28
if v , ok := req .(validator ); ok {
29
29
if err := v .Validate (); err != nil {
30
- return nil , errors .BadRequest ("VALIDATOR" , err .Error ()).WithCause (err )
30
+ return nil , errors .BadRequest (errors . ValidatorReason , err .Error ()).WithCause (err )
31
31
}
32
32
}
33
33
Original file line number Diff line number Diff line change @@ -16,7 +16,7 @@ import (
16
16
17
17
// ErrLimitExceed is service unavailable due to rate limit exceeded.
18
18
var (
19
- ErrLimitExceed = errors .New (429 , "RATELIMIT" , "service unavailable due to rate limit exceeded" )
19
+ ErrLimitExceed = errors .New (429 , errors . RateLimitReason , "service unavailable due to rate limit exceeded" )
20
20
)
21
21
22
22
// Ratelimit Request rate limit middleware
Original file line number Diff line number Diff line change @@ -15,6 +15,12 @@ const (
15
15
UnknownCode = 500
16
16
// UnknownReason is unknown reason for error info.
17
17
UnknownReason = ""
18
+ // ValidatorReason indicates an error related to request validation.
19
+ ValidatorReason = "VALIDATOR"
20
+ // CodecReason indicates an error related to encoding/decoding.
21
+ CodecReason = "CODEC"
22
+ // RateLimitReason indicates an error caused by rate limiting.
23
+ RateLimitReason = "RATELIMIT"
18
24
// SupportPackageIsVersion1 this constant should not be referenced by any other code.
19
25
SupportPackageIsVersion1 = true
20
26
)
Original file line number Diff line number Diff line change @@ -11,7 +11,7 @@ import (
11
11
)
12
12
13
13
// ErrLimitExceed is service unavailable due to rate limit exceeded.
14
- var ErrLimitExceed = errors .New (429 , "RATELIMIT" , "service unavailable due to rate limit exceeded" )
14
+ var ErrLimitExceed = errors .New (429 , errors . RateLimitReason , "service unavailable due to rate limit exceeded" )
15
15
16
16
// Option is ratelimit option.
17
17
type Option func (* options )
Original file line number Diff line number Diff line change @@ -19,7 +19,7 @@ func Validator() middleware.Middleware {
19
19
return func (ctx context.Context , req any ) (reply any , err error ) {
20
20
if v , ok := req .(validator ); ok {
21
21
if err := v .Validate (); err != nil {
22
- return nil , errors .BadRequest ("VALIDATOR" , err .Error ()).WithCause (err )
22
+ return nil , errors .BadRequest (errors . ValidatorReason , err .Error ()).WithCause (err )
23
23
}
24
24
}
25
25
return handler (ctx , req )
Original file line number Diff line number Diff line change @@ -12,7 +12,7 @@ import (
12
12
// BindQuery bind vars parameters to target.
13
13
func BindQuery (vars url.Values , target any ) error {
14
14
if err := encoding .GetCodec (form .Name ).Unmarshal ([]byte (vars .Encode ()), target ); err != nil {
15
- return errors .BadRequest ("CODEC" , err .Error ())
15
+ return errors .BadRequest (errors . CodecReason , err .Error ())
16
16
}
17
17
return nil
18
18
}
@@ -23,7 +23,7 @@ func BindForm(req *http.Request, target any) error {
23
23
return err
24
24
}
25
25
if err := encoding .GetCodec (form .Name ).Unmarshal ([]byte (req .Form .Encode ()), target ); err != nil {
26
- return errors .BadRequest ("CODEC" , err .Error ())
26
+ return errors .BadRequest (errors . CodecReason , err .Error ())
27
27
}
28
28
return nil
29
29
}
Original file line number Diff line number Diff line change @@ -49,7 +49,7 @@ func TestBindQuery(t *testing.T) {
49
49
vars : map [string ][]string {"age" : {"kratos" }, "url" : {"https://go-kratos.dev/" }},
50
50
target : & TestBind2 {},
51
51
},
52
- err : kratoserror .BadRequest ("CODEC" , "Field Namespace:age ERROR:Invalid Integer Value 'kratos' Type 'int' Namespace 'age'" ),
52
+ err : kratoserror .BadRequest (kratoserror . CodecReason , "Field Namespace:age ERROR:Invalid Integer Value 'kratos' Type 'int' Namespace 'age'" ),
53
53
},
54
54
{
55
55
name : "test2" ,
@@ -118,7 +118,7 @@ func TestBindForm(t *testing.T) {
118
118
},
119
119
target : & TestBind2 {},
120
120
},
121
- err : kratoserror .BadRequest ("CODEC" , "Field Namespace:age ERROR:Invalid Integer Value 'a' Type 'int' Namespace 'age'" ),
121
+ err : kratoserror .BadRequest (kratoserror . CodecReason , "Field Namespace:age ERROR:Invalid Integer Value 'a' Type 'int' Namespace 'age'" ),
122
122
want : nil ,
123
123
},
124
124
}
Original file line number Diff line number Diff line change @@ -61,21 +61,21 @@ func DefaultRequestQuery(r *http.Request, v any) error {
61
61
func DefaultRequestDecoder (r * http.Request , v any ) error {
62
62
codec , ok := CodecForRequest (r , "Content-Type" )
63
63
if ! ok {
64
- return errors .BadRequest ("CODEC" , fmt .Sprintf ("unregister Content-Type: %s" , r .Header .Get ("Content-Type" )))
64
+ return errors .BadRequest (errors . CodecReason , fmt .Sprintf ("unregister Content-Type: %s" , r .Header .Get ("Content-Type" )))
65
65
}
66
66
data , err := io .ReadAll (r .Body )
67
67
68
68
// reset body.
69
69
r .Body = io .NopCloser (bytes .NewBuffer (data ))
70
70
71
71
if err != nil {
72
- return errors .BadRequest ("CODEC" , err .Error ())
72
+ return errors .BadRequest (errors . CodecReason , err .Error ())
73
73
}
74
74
if len (data ) == 0 {
75
75
return nil
76
76
}
77
77
if err = codec .Unmarshal (data , v ); err != nil {
78
- return errors .BadRequest ("CODEC" , fmt .Sprintf ("body unmarshal %s" , err .Error ()))
78
+ return errors .BadRequest (errors . CodecReason , fmt .Sprintf ("body unmarshal %s" , err .Error ()))
79
79
}
80
80
return nil
81
81
}
You can’t perform that action at this time.
0 commit comments