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 59
/
errors.go
80 lines (65 loc) · 1.65 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 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)
}
type WorkflowErrorWithCause struct {
*WorkflowError
cause error
}
func (w *WorkflowErrorWithCause) Error() string {
return fmt.Sprintf("%v, caused by: %v", w.WorkflowError.Error(), w.cause)
}
func (w *WorkflowErrorWithCause) Cause() 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
}