-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy patherrors.go
More file actions
41 lines (35 loc) · 1.17 KB
/
errors.go
File metadata and controls
41 lines (35 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package circuit
import "fmt"
// Error represents a circuit breaker error with optional context.
type Error struct {
msg string
BreakerName string
State State
}
func (e Error) Error() string {
if e.BreakerName != "" {
return fmt.Sprintf("%s [breaker=%s]", e.msg, e.BreakerName)
}
return e.msg
}
// Is supports errors.Is by comparing the base message only,
// ignoring contextual fields like BreakerName and State.
func (e Error) Is(target error) bool {
t, ok := target.(Error)
if !ok {
return false
}
return e.msg == t.msg
}
// withContext returns a copy of the error with breaker context attached.
func (e Error) withContext(name string, state State) Error {
return Error{msg: e.msg, BreakerName: name, State: state}
}
var (
ErrNotInitialized = Error{msg: "circuit: breaker must be instantiated with NewBreaker"}
ErrTimeout = Error{msg: "circuit: breaker timed out"}
ErrStateUnknown = Error{msg: "circuit: unknown state"}
ErrStateOpen = Error{msg: "circuit: the circuit breaker is open"}
ErrStateThrottled = Error{msg: "circuit: breaker is throttled"}
ErrUnnamedBreaker = Error{msg: "circuit: breakers used in a breaker box must have a name"}
)