-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherrors.go
62 lines (49 loc) · 1.06 KB
/
errors.go
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package cache
import (
"bytes"
"fmt"
"strings"
)
type retryable interface {
IsRetryable() bool
}
// IsRetryable accepts an error and returns a boolean indicating if the operation
// that generated the error is retryable.
func IsRetryable(err error) bool {
re, ok := err.(retryable)
return ok && re.IsRetryable()
}
type RetryableError struct {
retryable bool
cause error
}
func (e RetryableError) IsRetryable() bool {
return e.retryable
}
func (e RetryableError) Error() string {
return e.cause.Error()
}
type CommandError struct {
Command string
Key string
Err error
}
func (r CommandError) Error() string {
return fmt.Sprintf("%s %s: %s", r.Command, r.Key, r.Err)
}
type CommandErrors []CommandError
func (r CommandErrors) Error() string {
buff := bytes.NewBufferString("")
for i := 0; i < len(r); i++ {
buff.WriteString(r[i].Error())
buff.WriteString("\n")
}
return strings.TrimSpace(buff.String())
}
func (r CommandErrors) Keys() []string {
keys := make([]string, len(r))
for i, e := range r {
keys[i] = e.Key
}
return keys
}