forked from Graphmasters/occamy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
errors.go
133 lines (105 loc) · 3.45 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
package occamy
import (
"errors"
"fmt"
)
type ErrKind string
const (
// ErrKindInternalHandlerError covers situations where the
// handler encounters an internal error.
ErrKindInternalHandlerError ErrKind = "internal_handler_error"
// ErrKindTaskInterrupted is exclusively for when a task
// (generated by a handler) is interrupted by the occamy
// server through the context being cancelled.
ErrKindTaskInterrupted ErrKind = "task_interrupted"
// ErrKindInvalidBody covers situations where a message
// was received with invalid bodies.
ErrKindInvalidBody ErrKind = "invalid_body"
// ErrKindInvalidHeader covers situations where a message
// was received with invalid headers.
ErrKindInvalidHeader ErrKind = "invalid_headers"
// ErrKindInvalidTask covers situations where the task
// requested is invalid.
ErrKindInvalidTask ErrKind = "invalid_task"
// ErrKindMessageNotAcked is for when the ack method
// fails for a message.
ErrKindMessageNotAcked ErrKind = "message_not_acked"
// ErrKindMessageNotNacked is for when the nack method
// fails for a message.
ErrKindMessageNotNacked ErrKind = "message_not_nacked"
// ErrKindTaskNotAdded is exclusively for when a task
// (generated by a handler) could not be added.
ErrKindTaskNotAdded ErrKind = "task_not_added"
// ErrKindTaskNotKilled is exclusively for when a task
// (generated by a handler) could not be killed.
ErrKindTaskNotKilled ErrKind = "task_not_killed"
// ErrKindUnknownHandlerError represents an unknown handler
// error.
ErrKindUnknownHandlerError ErrKind = "unknown_handler_error"
// ErrKindUnknownTaskError represents an unknown task
// error.
ErrKindUnknownTaskError ErrKind = "unknown_task_error"
)
// Error represents an occamy err.
type Error struct {
err error // The underlying error
kind ErrKind // The kind of the error
}
// NewError creates a new Error. If the error inputted is nil, then nil will be returned.
func NewError(kind ErrKind, err error) error {
if err == nil {
return nil
}
return &Error{
kind: kind,
err: err,
}
}
// NewErrorf is equivalent to calling NewError(kind, fmt.Errorf(format, args...)).
func NewErrorf(kind ErrKind, format string, args ...interface{}) error {
return NewError(kind, fmt.Errorf(format, args...))
}
// Error returns a description of the error and is required to implement the builtin error interface.
func (c *Error) Error() string {
return c.err.Error()
}
// Kind returns the error kind.
func (c *Error) Kind() ErrKind {
return c.kind
}
// Unwrap will return the underlying error.
func (c *Error) Unwrap() error {
return c.err
}
// ExtractErrorKind finds the first error in err's chain that is an occamy
// Error and returns the err kind along with a bool to indicate that a
// Error was found. If an occamy Error is not found a value of false for
// the bool will be returned.
func ExtractErrorKind(err error) (ErrKind, bool) {
if err == nil {
return "", false
}
var target *Error
if !errors.As(err, &target) {
return "", false
}
return target.kind, true
}
func convertErrorIfNotOccamyError(err error, kind ErrKind) error {
if _, ok := ExtractErrorKind(err); !ok {
return NewError(kind, err)
}
return err
}
func convertErrorIfNotLocalErrorOrMismatch(err error, kind ErrKind, alternatives ...ErrKind) error {
actualKind, ok := ExtractErrorKind(err)
if !ok {
return NewError(kind, err)
}
for _, k := range append(alternatives, kind) {
if k == actualKind {
return err
}
}
return NewError(kind, err)
}