This repository has been archived by the owner on Oct 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 60
/
errors.go
80 lines (65 loc) · 1.55 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
package errors
import (
"fmt"
"github.com/pkg/errors"
"github.com/lyft/flytepropeller/pkg/apis/flyteworkflow/v1alpha1"
)
type ErrorMessage = string
type NodeError struct {
errors.StackTrace
Code ErrorCode
Message ErrorMessage
Node v1alpha1.NodeID
}
func (n *NodeError) Error() string {
return fmt.Sprintf("failed at Node[%s]. %v: %v", n.Node, n.Code, n.Message)
}
type NodeErrorWithCause struct {
*NodeError
cause error
}
func (n *NodeErrorWithCause) Error() string {
return fmt.Sprintf("%v, caused by: %v", n.NodeError.Error(), errors.Cause(n))
}
func (n *NodeErrorWithCause) Cause() error {
return n.cause
}
func errorf(c ErrorCode, n v1alpha1.NodeID, msgFmt string, args ...interface{}) *NodeError {
return &NodeError{
Code: 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.Code
isNodeError = true
return
}
e2, ok := err.(*NodeErrorWithCause)
if ok {
code = e2.Code
isNodeError = true
return
}
return
}