-
Notifications
You must be signed in to change notification settings - Fork 51
/
policyerror.go
166 lines (143 loc) · 3.76 KB
/
policyerror.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package policy
import "fmt"
// ErrorReason is the reason for an error
type ErrorReason string
const (
// PUNotFound error reason
PUNotFound ErrorReason = "PUNotFound"
// PUNotUnique error reason
PUNotUnique ErrorReason = "PUNotUnique"
// PUCreateFailed error reason
PUCreateFailed ErrorReason = "PUCreateFailed"
// PUAlreadyActivated error reason
PUAlreadyActivated ErrorReason = "PUAlreadyActivated"
// PUPolicyPending error reason indicates that policy activation is pending.
PUPolicyPending ErrorReason = "PUPolicyPending"
// PUPolicyEnforcementFailed error reason indicates that enforcement failed.
PUPolicyEnforcementFailed
)
var policyErrorDescription = map[ErrorReason]string{
PUNotFound: "unable to find PU with ID",
PUNotUnique: "more than one PU with ID exist",
PUCreateFailed: "failed to create PU",
PUAlreadyActivated: "PU has been already activated previously",
}
// Error is a specific error type for context
type Error struct {
puID string
reason ErrorReason
err error
}
func (e *Error) Error() string {
desc, ok := policyErrorDescription[e.reason]
var err string
if e.err != nil {
err = ": " + e.err.Error()
}
if !ok {
return fmt.Sprintf("%s %s%s", e.reason, e.puID, err)
}
return fmt.Sprintf("%s %s: %s%s", e.reason, e.puID, desc, err)
}
// ErrPUNotFound creates a new context not found error
func ErrPUNotFound(puID string, err error) error {
return &Error{
puID: puID,
reason: PUNotFound,
err: err,
}
}
// ErrPUNotUnique creates a new not unique error
func ErrPUNotUnique(puID string, err error) error {
return &Error{
puID: puID,
reason: PUNotUnique,
err: err,
}
}
// ErrPUCreateFailed creates a new PU create failed error
func ErrPUCreateFailed(puID string, err error) error {
return &Error{
puID: puID,
reason: PUCreateFailed,
err: err,
}
}
// ErrPUAlreadyActivated creates a new PU already activated error
func ErrPUAlreadyActivated(puID string, err error) error {
return &Error{
puID: puID,
reason: PUAlreadyActivated,
err: err,
}
}
// ErrPUPolicyPending creates a new PU policy pending error.
func ErrPUPolicyPending(puID string, err error) error {
return &Error{
puID: puID,
reason: PUPolicyPending,
err: err,
}
}
// ErrPUPolicyEnforcementFailed creates a new PU policy pending error.
func ErrPUPolicyEnforcementFailed(puID string, err error) error {
return &Error{
puID: puID,
reason: PUPolicyEnforcementFailed,
err: err,
}
}
// IsErrPUNotFound checks if this error is a PU not found error
func IsErrPUNotFound(err error) bool {
switch t := err.(type) {
case *Error:
return t.reason == PUNotFound
default:
return false
}
}
// IsErrPUNotUnique checks if this error is a PU not unique error
func IsErrPUNotUnique(err error) bool {
switch t := err.(type) {
case *Error:
return t.reason == PUNotUnique
default:
return false
}
}
// IsErrPUCreateFailed checks if this error is a PU not unique error
func IsErrPUCreateFailed(err error) bool {
switch t := err.(type) {
case *Error:
return t.reason == PUCreateFailed
default:
return false
}
}
// IsErrPUAlreadyActivated checks if this error is a PU already activated error
func IsErrPUAlreadyActivated(err error) bool {
switch t := err.(type) {
case *Error:
return t.reason == PUAlreadyActivated
default:
return false
}
}
// IsErrPUPolicyPending checks if this error is a PU policy pending error.
func IsErrPUPolicyPending(err error) bool {
switch t := err.(type) {
case *Error:
return t.reason == PUPolicyPending
default:
return false
}
}
// IsErrPUEnforcementFailed checks if this error is a PU policy pending error.
func IsErrPUEnforcementFailed(err error) bool {
switch t := err.(type) {
case *Error:
return t.reason == PUPolicyEnforcementFailed
default:
return false
}
}