mirror of
https://github.com/go-kratos/kratos.git
synced 2025-01-14 02:33:03 +02:00
fix: middleware uses sentinel error (#1463)
This commit is contained in:
parent
558070f4d9
commit
6e6526efd9
@ -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)
|
||||
|
@ -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)
|
@ -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 {
|
||||
|
Loading…
Reference in New Issue
Block a user