Repository navigation
Expand file tree
/
Copy patherrors.go
More file actions
133 lines (102 loc) · 3.13 KB
/
Copy patherrors.go
File metadata and controls
133 lines (102 loc) · 3.13 KB
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 (
"fmt"
)
// region BasicErr Error
const (
// ErrInternalHandlerError covers situations where the
// handler encounters an internal error.
ErrInternalHandlerError BasicError = "internal_handler_error"
// ErrTaskInterrupted is exclusively for when a task
// (generated by a handler) is interrupted by the occamy
// server through the context being cancelled.
ErrTaskInterrupted BasicError = "task_interrupted"
// ErrInvalidBody covers situations where a message
// was received with invalid bodies.
ErrInvalidBody BasicError = "invalid_body"
// ErrInvalidHeader covers situations where a message
// was received with invalid headers.
ErrInvalidHeader BasicError = "invalid_headers"
// ErrInvalidTask covers situations where the task
// requested is invalid.
ErrInvalidTask BasicError = "invalid_task"
// ErrMessageNotAcked is for when the ack method
// fails for a message.
ErrMessageNotAcked BasicError = "message_not_acked"
// ErrMessageNotNacked is for when the nack method
// fails for a message.
ErrMessageNotNacked BasicError = "message_not_nacked"
// ErrTaskNotAdded is exclusively for when a task
// (generated by a handler) could not be added.
ErrTaskNotAdded BasicError = "task_not_added"
// ErrTaskNotKilled is exclusively for when a task
// (generated by a handler) could not be killed.
ErrTaskNotKilled BasicError = "task_not_killed"
)
type BasicError string
func (e BasicError) Error() string {
return string(e)
}
// endregion
// region Detailed Error
type DetailedError struct {
BasicErr BasicError
Cause string
}
func NewDetailedError(err BasicError, cause string) *DetailedError {
return &DetailedError{
BasicErr: err,
Cause: cause,
}
}
func (w *DetailedError) Error() string {
return fmt.Sprintf("%v: %v", w.BasicErr, w.Cause)
}
// endregion
// region Wrapped Error
type WrappedError struct {
BasicErr BasicError
InnerErr error
}
func NewWrappedError(basicErr BasicError, innerErr error) *WrappedError {
return &WrappedError{
BasicErr: basicErr,
InnerErr: innerErr,
}
}
func (w *WrappedError) Error() string {
return fmt.Sprintf("%v: %v", w.BasicErr, w.InnerErr)
}
// endregion
// region Error helpers
// extractSimpleError extracts the simple error. The second value returned
// signifies if it was possible/successful. It is only possible when the error
// is one of the local types of errors i.e. BasicError, DetailedError and
// WrappedError.
func extractSimpleError(err error) (BasicError, bool) {
switch explicitErr := err.(type) {
case BasicError:
return explicitErr, true
case *DetailedError:
return explicitErr.BasicErr, true
case *WrappedError:
return explicitErr.BasicErr, true
default:
return "", false
}
}
// This is expected to be useful later
// nolint
func wrapErrorIfNotLocalError(err error, simpleError BasicError) error {
if _, ok := extractSimpleError(err); !ok {
return NewWrappedError(simpleError, err)
}
return err
}
func wrapErrorIfNotLocalErrorOrMismatch(err error, expected BasicError) error {
if actual, ok := extractSimpleError(err); !ok || actual != expected {
return NewWrappedError(expected, err)
}
return err
}
// endregion