-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors.go
More file actions
143 lines (124 loc) · 3.12 KB
/
errors.go
File metadata and controls
143 lines (124 loc) · 3.12 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
134
135
136
137
138
139
140
141
142
143
package di
import (
"errors"
"fmt"
"reflect"
)
var ErrSkippedDependency = errors.New("skipped dependency")
const (
ErrTypeDependencyCreation = iota
ErrTypeDuplicatedName
ErrTypeDuplicatedRegistration
ErrTypeMissingDependency
ErrTypeInvalidType
ErrTypeInvalidConstructor
ErrTypeCyclicDependency
ErrTypeDependencyInitialization
ErrTypeDependencyShutdown
ErrTypeLifecycle
)
type Error struct {
errType int
message string
cause error
}
func (e *Error) ErrType() int {
return e.errType
}
func (e *Error) IsErrType(errType int) bool {
return e.errType == errType
}
func (e *Error) Error() string {
return e.message
}
func (e *Error) Unwrap() error {
return e.cause
}
func (e *Error) RootCause() error {
if e.cause == nil {
return e
}
if dierr, ok := e.cause.(*Error); ok {
return dierr.RootCause()
}
return e.cause
}
func newLifecycleError(cause string) *Error {
msg := fmt.Sprintf("context lifecycle error: %s", cause)
return &Error{
errType: ErrTypeLifecycle,
message: msg,
}
}
func newDuplicatedRegistrationError() *Error {
return &Error{
errType: ErrTypeDuplicatedRegistration,
message: "duplicated registration",
}
}
func newDependencyCreationError(objName *string, objType *reflect.Type, cause error) *Error {
msg := fmt.Sprintf("could not create dependency %s, cause:\n%s", descriptor(objName, objType), cause)
return &Error{
errType: ErrTypeDependencyCreation,
message: msg,
cause: cause,
}
}
func newMissingDependencyError(objName *string, objType *reflect.Type) *Error {
msg := fmt.Sprintf("missing dependency %s", descriptor(objName, objType))
return &Error{
errType: ErrTypeMissingDependency,
message: msg,
}
}
func newInvalidTypeError(objName *string, objType reflect.Type, expectedType reflect.Type) *Error {
msg := fmt.Sprintf("could not cast %s to %s", descriptor(objName, &objType), expectedType)
return &Error{
errType: ErrTypeInvalidType,
message: msg,
}
}
func newInitializationError(objType *reflect.Type, cause error) *Error {
msg := fmt.Sprintf("could not initialize dependency: %s, cause:\n%s", descriptor(nil, objType), cause)
return &Error{
errType: ErrTypeDependencyInitialization,
message: msg,
cause: cause,
}
}
func newShutdownError(objType *reflect.Type, cause error) *Error {
msg := fmt.Sprintf("could not shutdown dependency: %s, cause:\n%s", descriptor(nil, objType), cause)
return &Error{
errType: ErrTypeDependencyShutdown,
message: msg,
cause: cause,
}
}
func newCyclicDependencyError(path []string) *Error {
msg := ""
for _, d := range path {
msg = fmt.Sprintf("%s%s -> ", msg, d)
}
if len(path) > 0 {
msg = fmt.Sprintf("%s%s", msg, path[0])
}
msg = fmt.Sprintf("cyclic dependency: %s", msg)
return &Error{
errType: ErrTypeCyclicDependency,
message: msg,
}
}
func newInvalidConstructorError(cause string) *Error {
msg := fmt.Sprintf("invalid dependency constructor: %s", cause)
return &Error{
errType: ErrTypeInvalidConstructor,
message: msg,
}
}
func newDuplicatedNameError(name string) *Error {
msg := fmt.Sprintf("duplicated dependency name: %s", name)
return &Error{
errType: ErrTypeDuplicatedName,
message: msg,
}
}