-
Notifications
You must be signed in to change notification settings - Fork 546
/
errors.go
147 lines (121 loc) · 2.78 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package errors
import (
"fmt"
"strings"
"github.com/flyteorg/flyte/flytepropeller/pkg/apis/flyteworkflow/v1alpha1"
)
type ErrorMessage = string
type NodeError struct {
ErrCode ErrorCode
Message ErrorMessage
Node v1alpha1.NodeID
}
func (n *NodeError) Code() ErrorCode {
return n.ErrCode
}
func (n *NodeError) Error() string {
return fmt.Sprintf("failed at Node[%s]. %v: %v", n.Node, n.ErrCode, n.Message)
}
func (n *NodeError) Is(target error) bool {
t, ok := target.(*NodeError)
if !ok {
return false
}
if n == nil && t == nil {
return true
}
if n == nil || t == nil {
return false
}
return n.ErrCode == t.ErrCode && (n.Message == t.Message || t.Message == "") && (n.Node == t.Node || t.Node == "")
}
type NodeErrorWithCause struct {
NodeError error
cause error
}
func (n *NodeErrorWithCause) Cause() error {
return n.cause
}
func (n *NodeErrorWithCause) Code() ErrorCode {
if asNodeErr, casted := n.NodeError.(*NodeError); casted {
return asNodeErr.Code()
}
return ""
}
func (n *NodeErrorWithCause) Error() string {
nodeError := ""
if n.NodeError != nil {
nodeError = n.NodeError.Error()
}
cause := ""
if n.cause != nil {
cause = n.cause.Error()
}
return fmt.Sprintf("%v, caused by: %v", nodeError, cause)
}
func (n *NodeErrorWithCause) Is(target error) bool {
t, ok := target.(*NodeErrorWithCause)
if !ok {
return false
}
if n == nil && t == nil {
return true
}
if n == nil || t == nil {
return false
}
return n.Is(target) && (n.cause == t.cause || t.cause == nil)
}
func (n *NodeErrorWithCause) Unwrap() error {
return n.cause
}
func errorf(c ErrorCode, n v1alpha1.NodeID, msgFmt string, args ...interface{}) error {
return &NodeError{
ErrCode: c,
Node: n,
Message: fmt.Sprintf(msgFmt, args...),
}
}
func Errorf(c ErrorCode, n v1alpha1.NodeID, msgFmt string, args ...interface{}) error {
return errorf(c, n, msgFmt, args...)
}
func Wrapf(c ErrorCode, n v1alpha1.NodeID, cause error, msgFmt string, args ...interface{}) error {
return &NodeErrorWithCause{
NodeError: errorf(c, n, msgFmt, args...),
cause: cause,
}
}
func Matches(err error, code ErrorCode) bool {
errCode, isNodeError := GetErrorCode(err)
if isNodeError {
return code == errCode
}
return false
}
func GetErrorCode(err error) (code ErrorCode, isNodeError bool) {
isNodeError = false
e, ok := err.(*NodeError)
if ok {
code = e.ErrCode
isNodeError = true
return
}
if e2, ok := err.(*NodeErrorWithCause); ok {
if ne, ok := e2.NodeError.(*NodeError); ok {
code = ne.ErrCode
isNodeError = true
return
}
}
return
}
type ErrorCollection struct {
Errors []error
}
func (e ErrorCollection) Error() string {
sb := strings.Builder{}
for idx, err := range e.Errors {
sb.WriteString(fmt.Sprintf("%v: %v\r\n", idx, err))
}
return sb.String()
}