From b463dc5147142d4435143b8e390bf52c80fc1837 Mon Sep 17 00:00:00 2001 From: Name <1911860538@qq.com> Date: Sun, 27 Apr 2025 11:06:26 +0800 Subject: [PATCH] Revert "refactor: replace repeated error reasons with constants (#3612)" (#3651) This reverts commit 5617ff3ff78449bfd252b426d142e18afba2df43. Co-authored-by: Tony.Chen --- contrib/middleware/validate/validate.go | 4 ++-- contrib/polaris/ratelimit.go | 2 +- errors/errors.go | 6 ------ middleware/ratelimit/ratelimit.go | 2 +- middleware/validate/validate.go | 2 +- transport/http/binding/bind.go | 4 ++-- transport/http/binding/bind_test.go | 4 ++-- transport/http/codec.go | 6 +++--- 8 files changed, 12 insertions(+), 18 deletions(-) diff --git a/contrib/middleware/validate/validate.go b/contrib/middleware/validate/validate.go index d4e4a17d1..d2ab6f4dc 100644 --- a/contrib/middleware/validate/validate.go +++ b/contrib/middleware/validate/validate.go @@ -20,14 +20,14 @@ func ProtoValidate() middleware.Middleware { return func(ctx context.Context, req any) (reply any, err error) { if msg, ok := req.(proto.Message); ok { if err := protovalidate.Validate(msg); err != nil { - return nil, errors.BadRequest(errors.ValidatorReason, err.Error()).WithCause(err) + return nil, errors.BadRequest("VALIDATOR", err.Error()).WithCause(err) } } // to compatible with the [old validator](https://github.com/envoyproxy/protoc-gen-validate) if v, ok := req.(validator); ok { if err := v.Validate(); err != nil { - return nil, errors.BadRequest(errors.ValidatorReason, err.Error()).WithCause(err) + return nil, errors.BadRequest("VALIDATOR", err.Error()).WithCause(err) } } diff --git a/contrib/polaris/ratelimit.go b/contrib/polaris/ratelimit.go index 045a0e6a8..8f9351318 100644 --- a/contrib/polaris/ratelimit.go +++ b/contrib/polaris/ratelimit.go @@ -16,7 +16,7 @@ import ( // ErrLimitExceed is service unavailable due to rate limit exceeded. var ( - ErrLimitExceed = errors.New(429, errors.RateLimitReason, "service unavailable due to rate limit exceeded") + ErrLimitExceed = errors.New(429, "RATELIMIT", "service unavailable due to rate limit exceeded") ) // Ratelimit Request rate limit middleware diff --git a/errors/errors.go b/errors/errors.go index 913ec7682..aaffa282c 100644 --- a/errors/errors.go +++ b/errors/errors.go @@ -15,12 +15,6 @@ const ( UnknownCode = 500 // UnknownReason is unknown reason for error info. UnknownReason = "" - // ValidatorReason indicates an error related to request validation. - ValidatorReason = "VALIDATOR" - // CodecReason indicates an error related to encoding/decoding. - CodecReason = "CODEC" - // RateLimitReason indicates an error caused by rate limiting. - RateLimitReason = "RATELIMIT" // SupportPackageIsVersion1 this constant should not be referenced by any other code. SupportPackageIsVersion1 = true ) diff --git a/middleware/ratelimit/ratelimit.go b/middleware/ratelimit/ratelimit.go index 48fbe2b09..9439d62de 100644 --- a/middleware/ratelimit/ratelimit.go +++ b/middleware/ratelimit/ratelimit.go @@ -11,7 +11,7 @@ import ( ) // ErrLimitExceed is service unavailable due to rate limit exceeded. -var ErrLimitExceed = errors.New(429, errors.RateLimitReason, "service unavailable due to rate limit exceeded") +var ErrLimitExceed = errors.New(429, "RATELIMIT", "service unavailable due to rate limit exceeded") // Option is ratelimit option. type Option func(*options) diff --git a/middleware/validate/validate.go b/middleware/validate/validate.go index 7187b1243..aa69bc808 100644 --- a/middleware/validate/validate.go +++ b/middleware/validate/validate.go @@ -19,7 +19,7 @@ func Validator() middleware.Middleware { return func(ctx context.Context, req any) (reply any, err error) { if v, ok := req.(validator); ok { if err := v.Validate(); err != nil { - return nil, errors.BadRequest(errors.ValidatorReason, err.Error()).WithCause(err) + return nil, errors.BadRequest("VALIDATOR", err.Error()).WithCause(err) } } return handler(ctx, req) diff --git a/transport/http/binding/bind.go b/transport/http/binding/bind.go index bfde9b04d..195dfe7af 100644 --- a/transport/http/binding/bind.go +++ b/transport/http/binding/bind.go @@ -12,7 +12,7 @@ import ( // BindQuery bind vars parameters to target. func BindQuery(vars url.Values, target any) error { if err := encoding.GetCodec(form.Name).Unmarshal([]byte(vars.Encode()), target); err != nil { - return errors.BadRequest(errors.CodecReason, err.Error()) + return errors.BadRequest("CODEC", err.Error()) } return nil } @@ -23,7 +23,7 @@ func BindForm(req *http.Request, target any) error { return err } if err := encoding.GetCodec(form.Name).Unmarshal([]byte(req.Form.Encode()), target); err != nil { - return errors.BadRequest(errors.CodecReason, err.Error()) + return errors.BadRequest("CODEC", err.Error()) } return nil } diff --git a/transport/http/binding/bind_test.go b/transport/http/binding/bind_test.go index 482c6062c..dd5ccc71c 100644 --- a/transport/http/binding/bind_test.go +++ b/transport/http/binding/bind_test.go @@ -49,7 +49,7 @@ func TestBindQuery(t *testing.T) { vars: map[string][]string{"age": {"kratos"}, "url": {"https://go-kratos.dev/"}}, target: &TestBind2{}, }, - err: kratoserror.BadRequest(kratoserror.CodecReason, "Field Namespace:age ERROR:Invalid Integer Value 'kratos' Type 'int' Namespace 'age'"), + err: kratoserror.BadRequest("CODEC", "Field Namespace:age ERROR:Invalid Integer Value 'kratos' Type 'int' Namespace 'age'"), }, { name: "test2", @@ -118,7 +118,7 @@ func TestBindForm(t *testing.T) { }, target: &TestBind2{}, }, - err: kratoserror.BadRequest(kratoserror.CodecReason, "Field Namespace:age ERROR:Invalid Integer Value 'a' Type 'int' Namespace 'age'"), + err: kratoserror.BadRequest("CODEC", "Field Namespace:age ERROR:Invalid Integer Value 'a' Type 'int' Namespace 'age'"), want: nil, }, } diff --git a/transport/http/codec.go b/transport/http/codec.go index c3e2c74ff..9c026f833 100644 --- a/transport/http/codec.go +++ b/transport/http/codec.go @@ -61,7 +61,7 @@ func DefaultRequestQuery(r *http.Request, v any) error { func DefaultRequestDecoder(r *http.Request, v any) error { codec, ok := CodecForRequest(r, "Content-Type") if !ok { - return errors.BadRequest(errors.CodecReason, fmt.Sprintf("unregister Content-Type: %s", r.Header.Get("Content-Type"))) + return errors.BadRequest("CODEC", fmt.Sprintf("unregister Content-Type: %s", r.Header.Get("Content-Type"))) } data, err := io.ReadAll(r.Body) @@ -69,13 +69,13 @@ func DefaultRequestDecoder(r *http.Request, v any) error { r.Body = io.NopCloser(bytes.NewBuffer(data)) if err != nil { - return errors.BadRequest(errors.CodecReason, err.Error()) + return errors.BadRequest("CODEC", err.Error()) } if len(data) == 0 { return nil } if err = codec.Unmarshal(data, v); err != nil { - return errors.BadRequest(errors.CodecReason, fmt.Sprintf("body unmarshal %s", err.Error())) + return errors.BadRequest("CODEC", fmt.Sprintf("body unmarshal %s", err.Error())) } return nil }