mirror of
				https://github.com/go-kratos/kratos.git
				synced 2025-10-30 23:47:59 +02:00 
			
		
		
		
	This reverts commit 5617ff3ff7.
Co-authored-by: Tony.Chen <zhihui_chen@foxmail.com>
			
			
This commit is contained in:
		| @@ -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) | ||||
| 				} | ||||
| 			} | ||||
|  | ||||
|   | ||||
| @@ -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 | ||||
|   | ||||
| @@ -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 | ||||
| ) | ||||
|   | ||||
| @@ -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) | ||||
|   | ||||
| @@ -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) | ||||
|   | ||||
| @@ -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 | ||||
| } | ||||
|   | ||||
| @@ -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, | ||||
| 		}, | ||||
| 	} | ||||
|   | ||||
| @@ -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 | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user