Skip to content

Commit 5617ff3

Browse files
19118605381911860538tonybase
authored
refactor: replace repeated error reasons with constants (#3612)
* refactor: replace repeated error reasons with constants * refactor: extract RATELIMIT error reason to constant --------- Co-authored-by: 1911860538 <[email protected]> Co-authored-by: Tony.Chen <[email protected]>
1 parent 5d532fe commit 5617ff3

File tree

8 files changed

+18
-12
lines changed

8 files changed

+18
-12
lines changed

contrib/middleware/validate/validate.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@ func ProtoValidate() middleware.Middleware {
2020
return func(ctx context.Context, req any) (reply any, err error) {
2121
if msg, ok := req.(proto.Message); ok {
2222
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)
2424
}
2525
}
2626

2727
// to compatible with the [old validator](https://github.com/envoyproxy/protoc-gen-validate)
2828
if v, ok := req.(validator); ok {
2929
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)
3131
}
3232
}
3333

contrib/polaris/ratelimit.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import (
1616

1717
// ErrLimitExceed is service unavailable due to rate limit exceeded.
1818
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")
2020
)
2121

2222
// Ratelimit Request rate limit middleware

errors/errors.go

+6
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@ const (
1515
UnknownCode = 500
1616
// UnknownReason is unknown reason for error info.
1717
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"
1824
// SupportPackageIsVersion1 this constant should not be referenced by any other code.
1925
SupportPackageIsVersion1 = true
2026
)

middleware/ratelimit/ratelimit.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
)
1212

1313
// 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")
1515

1616
// Option is ratelimit option.
1717
type Option func(*options)

middleware/validate/validate.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ func Validator() middleware.Middleware {
1919
return func(ctx context.Context, req any) (reply any, err error) {
2020
if v, ok := req.(validator); ok {
2121
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)
2323
}
2424
}
2525
return handler(ctx, req)

transport/http/binding/bind.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
// BindQuery bind vars parameters to target.
1313
func BindQuery(vars url.Values, target any) error {
1414
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())
1616
}
1717
return nil
1818
}
@@ -23,7 +23,7 @@ func BindForm(req *http.Request, target any) error {
2323
return err
2424
}
2525
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())
2727
}
2828
return nil
2929
}

transport/http/binding/bind_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func TestBindQuery(t *testing.T) {
4949
vars: map[string][]string{"age": {"kratos"}, "url": {"https://go-kratos.dev/"}},
5050
target: &TestBind2{},
5151
},
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'"),
5353
},
5454
{
5555
name: "test2",
@@ -118,7 +118,7 @@ func TestBindForm(t *testing.T) {
118118
},
119119
target: &TestBind2{},
120120
},
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'"),
122122
want: nil,
123123
},
124124
}

transport/http/codec.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -61,21 +61,21 @@ func DefaultRequestQuery(r *http.Request, v any) error {
6161
func DefaultRequestDecoder(r *http.Request, v any) error {
6262
codec, ok := CodecForRequest(r, "Content-Type")
6363
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")))
6565
}
6666
data, err := io.ReadAll(r.Body)
6767

6868
// reset body.
6969
r.Body = io.NopCloser(bytes.NewBuffer(data))
7070

7171
if err != nil {
72-
return errors.BadRequest("CODEC", err.Error())
72+
return errors.BadRequest(errors.CodecReason, err.Error())
7373
}
7474
if len(data) == 0 {
7575
return nil
7676
}
7777
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()))
7979
}
8080
return nil
8181
}

0 commit comments

Comments
 (0)