-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy patherrors.go
77 lines (66 loc) · 1.42 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package repeat
// TemporaryError allows not to stop repetitions process right now.
//
// This error never returns to the caller as is, only wrapped error.
type TemporaryError struct {
Cause error
}
func (e *TemporaryError) Error() string {
r := "repeat.temporary"
if e.Cause != nil {
r += ": " + e.Cause.Error()
}
return r
}
// HintTemporary makes a TemporaryError.
func HintTemporary(e error) error {
return &TemporaryError{Cause(e)}
}
// IsTemporary checks if passed error is TemporaryError.
func IsTemporary(e error) bool {
switch e.(type) {
case *TemporaryError:
return true
default:
return false
}
}
// StopError allows to stop repetition process without specifying a
// separate error.
//
// This error never returns to the caller as is, only wrapped error.
type StopError struct {
Cause error
}
func (e *StopError) Error() string {
r := "repeat.stop"
if e.Cause != nil {
r += ": " + e.Cause.Error()
}
return r
}
// HintStop makes a StopError.
func HintStop(e error) error {
return &StopError{Cause(e)}
}
// IsStop checks if passed error is StopError.
func IsStop(e error) bool {
switch e.(type) {
case *StopError:
return true
default:
return false
}
}
// Cause extracts the cause error from TemporaryError and StopError
// or return the passed one.
func Cause(err error) error {
switch e := err.(type) {
case *TemporaryError:
return e.Cause
case *StopError:
return e.Cause
default:
return err
}
}