From 6e6526efd9ecb8034328b1a7faf17e4a53109104 Mon Sep 17 00:00:00 2001 From: Tony Chen Date: Sat, 11 Sep 2021 18:56:51 +0800 Subject: [PATCH] fix: middleware uses sentinel error (#1463) --- middleware/circuitbreaker/circuitbreaker.go | 5 ++++- middleware/ratelimit/{limiter.go => ratelimit.go} | 5 ++++- middleware/recovery/recovery.go | 6 ++++-- 3 files changed, 12 insertions(+), 4 deletions(-) rename middleware/ratelimit/{limiter.go => ratelimit.go} (83%) diff --git a/middleware/circuitbreaker/circuitbreaker.go b/middleware/circuitbreaker/circuitbreaker.go index 054597c33..5317e5d22 100644 --- a/middleware/circuitbreaker/circuitbreaker.go +++ b/middleware/circuitbreaker/circuitbreaker.go @@ -11,6 +11,9 @@ import ( "github.com/go-kratos/kratos/v2/transport" ) +// ErrNotAllowed is request failed due to circuit breaker triggered. +var ErrNotAllowed = errors.New(503, "CIRCUITBREAKER", "request failed due to circuit breaker triggered") + // Option is circuit breaker option. type Option func(*options) @@ -46,7 +49,7 @@ func Client(opts ...Option) middleware.Middleware { // NOTE: when client reject requets locally, // continue add counter let the drop ratio higher. breaker.MarkFailed() - return nil, errors.New(503, "CIRCUITBREAKER", "request failed due to circuit breaker triggered") + return nil, ErrNotAllowed } // allowed reply, err := handler(ctx, req) diff --git a/middleware/ratelimit/limiter.go b/middleware/ratelimit/ratelimit.go similarity index 83% rename from middleware/ratelimit/limiter.go rename to middleware/ratelimit/ratelimit.go index bcfbb4fc9..5bfae27b1 100644 --- a/middleware/ratelimit/limiter.go +++ b/middleware/ratelimit/ratelimit.go @@ -9,6 +9,9 @@ import ( "github.com/go-kratos/kratos/v2/middleware" ) +// ErrLimitExceed is 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) @@ -37,7 +40,7 @@ func Server(opts ...Option) middleware.Middleware { done, e := options.limiter.Allow() if e != nil { // rejected - return nil, errors.New(429, "RATELIMIT", "service unavailable due to rate limit exceeded") + return nil, ErrLimitExceed } // allowed reply, err = handler(ctx, req) diff --git a/middleware/recovery/recovery.go b/middleware/recovery/recovery.go index 8cb26e24a..94f3eb547 100644 --- a/middleware/recovery/recovery.go +++ b/middleware/recovery/recovery.go @@ -2,7 +2,6 @@ package recovery import ( "context" - "fmt" "runtime" "github.com/go-kratos/kratos/v2/errors" @@ -10,6 +9,9 @@ import ( "github.com/go-kratos/kratos/v2/middleware" ) +// ErrUnknownRequest is unknown request error. +var ErrUnknownRequest = errors.InternalServer("UNKNOWN", "unknown request error") + // HandlerFunc is recovery handler func. type HandlerFunc func(ctx context.Context, req, err interface{}) error @@ -40,7 +42,7 @@ func Recovery(opts ...Option) middleware.Middleware { op := options{ logger: log.DefaultLogger, handler: func(ctx context.Context, req, err interface{}) error { - return errors.InternalServer("RECOVERY", fmt.Sprintf("panic triggered: %v", err)) + return ErrUnknownRequest }, } for _, o := range opts {