fix: middleware uses sentinel error (#1463)

pull/1465/head
Tony Chen 3 years ago committed by GitHub
parent 558070f4d9
commit 6e6526efd9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 5
      middleware/circuitbreaker/circuitbreaker.go
  2. 5
      middleware/ratelimit/ratelimit.go
  3. 6
      middleware/recovery/recovery.go

@ -11,6 +11,9 @@ import (
"github.com/go-kratos/kratos/v2/transport" "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. // Option is circuit breaker option.
type Option func(*options) type Option func(*options)
@ -46,7 +49,7 @@ func Client(opts ...Option) middleware.Middleware {
// NOTE: when client reject requets locally, // NOTE: when client reject requets locally,
// continue add counter let the drop ratio higher. // continue add counter let the drop ratio higher.
breaker.MarkFailed() breaker.MarkFailed()
return nil, errors.New(503, "CIRCUITBREAKER", "request failed due to circuit breaker triggered") return nil, ErrNotAllowed
} }
// allowed // allowed
reply, err := handler(ctx, req) reply, err := handler(ctx, req)

@ -9,6 +9,9 @@ import (
"github.com/go-kratos/kratos/v2/middleware" "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. // Option is ratelimit option.
type Option func(*options) type Option func(*options)
@ -37,7 +40,7 @@ func Server(opts ...Option) middleware.Middleware {
done, e := options.limiter.Allow() done, e := options.limiter.Allow()
if e != nil { if e != nil {
// rejected // rejected
return nil, errors.New(429, "RATELIMIT", "service unavailable due to rate limit exceeded") return nil, ErrLimitExceed
} }
// allowed // allowed
reply, err = handler(ctx, req) reply, err = handler(ctx, req)

@ -2,7 +2,6 @@ package recovery
import ( import (
"context" "context"
"fmt"
"runtime" "runtime"
"github.com/go-kratos/kratos/v2/errors" "github.com/go-kratos/kratos/v2/errors"
@ -10,6 +9,9 @@ import (
"github.com/go-kratos/kratos/v2/middleware" "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. // HandlerFunc is recovery handler func.
type HandlerFunc func(ctx context.Context, req, err interface{}) error type HandlerFunc func(ctx context.Context, req, err interface{}) error
@ -40,7 +42,7 @@ func Recovery(opts ...Option) middleware.Middleware {
op := options{ op := options{
logger: log.DefaultLogger, logger: log.DefaultLogger,
handler: func(ctx context.Context, req, err interface{}) error { 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 { for _, o := range opts {

Loading…
Cancel
Save