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
112 lines (94 loc) · 2.31 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
package errors
import (
"fmt"
"github.com/pkg/errors"
"github.com/flyteorg/flytepropeller/pkg/apis/flyteworkflow/v1alpha1"
)
type ErrorMessage = string
type WorkflowError struct {
errors.StackTrace
Code ErrorCode
Message ErrorMessage
Workflow v1alpha1.WorkflowID
}
func (w *WorkflowError) Error() string {
return fmt.Sprintf("Workflow[%s] failed. %v: %v", w.Workflow, w.Code, w.Message)
}
func (w *WorkflowError) Is(target error) bool {
t, ok := target.(*WorkflowError)
if !ok {
return false
}
if w == nil && t == nil {
return true
}
if w == nil || t == nil {
return false
}
return w.Code == t.Code
}
type WorkflowErrorWithCause struct {
*WorkflowError
cause error
}
func (w *WorkflowErrorWithCause) Cause() error {
return w.cause
}
func (w *WorkflowErrorWithCause) Error() string {
return fmt.Sprintf("%v, caused by: %v", w.WorkflowError.Error(), w.cause)
}
func (w *WorkflowErrorWithCause) Is(target error) bool {
t, ok := target.(*WorkflowErrorWithCause)
if !ok {
return false
}
if w == nil && t == nil {
return true
}
if w == nil || t == nil {
return false
}
return w.Code == t.Code && (w.cause == t.cause || t.cause == nil) && (w.Message == t.Message || t.Message == "") && (w.Workflow == t.Workflow || t.Workflow == "")
}
func (w *WorkflowErrorWithCause) Unwrap() error {
return w.cause
}
func errorf(c ErrorCode, w v1alpha1.WorkflowID, msgFmt string, args ...interface{}) *WorkflowError {
return &WorkflowError{
Code: c,
Workflow: w,
Message: fmt.Sprintf(msgFmt, args...),
}
}
func Errorf(c ErrorCode, w v1alpha1.WorkflowID, msgFmt string, args ...interface{}) error {
return errorf(c, w, msgFmt, args...)
}
func Wrapf(c ErrorCode, w v1alpha1.WorkflowID, cause error, msgFmt string, args ...interface{}) error {
return &WorkflowErrorWithCause{
WorkflowError: errorf(c, w, msgFmt, args...),
cause: cause,
}
}
func Matches(err error, code ErrorCode) bool {
errCode, isWorkflowError := GetErrorCode(err)
if isWorkflowError {
return code == errCode
}
return false
}
func GetErrorCode(err error) (code ErrorCode, isWorkflowError bool) {
isWorkflowError = false
e, ok := err.(*WorkflowError)
if ok {
code = e.Code
isWorkflowError = true
return
}
e2, ok := err.(*WorkflowErrorWithCause)
if ok {
code = e2.Code
isWorkflowError = true
return
}
return
}