forked from projectcalico/libcalico-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
errors.go
155 lines (133 loc) · 3.93 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
// Copyright (c) 2016 Tigera, Inc. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package errors
import (
"fmt"
)
// Error indicating a problem connecting to the backend.
type ErrorDatastoreError struct {
Err error
Identifier interface{}
}
func (e ErrorDatastoreError) Error() string {
return e.Err.Error()
}
// Error indicating a resource does not exist. Used when attempting to delete or
// udpate a non-existent resource.
type ErrorResourceDoesNotExist struct {
Err error
Identifier interface{}
}
func (e ErrorResourceDoesNotExist) Error() string {
return fmt.Sprintf("resource does not exist: %s", e.Identifier)
}
// Error indicating an operation is not supported.
type ErrorOperationNotSupported struct {
Operation string
Identifier interface{}
}
func (e ErrorOperationNotSupported) Error() string {
return fmt.Sprintf("operation %s is not supported on %s", e.Operation, e.Identifier)
}
// Error indicating a resource already exists. Used when attempting to create a
// resource that already exists.
type ErrorResourceAlreadyExists struct {
Err error
Identifier interface{}
}
func (e ErrorResourceAlreadyExists) Error() string {
return fmt.Sprintf("resource already exists: %s", e.Identifier)
}
// Error indicating a problem connecting to the backend.
type ErrorConnectionUnauthorized struct {
Err error
}
func (e ErrorConnectionUnauthorized) Error() string {
return "connection is unauthorized"
}
// Validation error containing the fields that are failed validation.
type ErrorValidation struct {
ErroredFields []ErroredField
}
type ErroredField struct {
Name string
Value interface{}
Reason string
}
func (e ErrorValidation) Error() string {
if len(e.ErroredFields) == 0 {
return "unknown validation error"
} else if len(e.ErroredFields) == 1 {
f := e.ErroredFields[0]
if f.Reason == "" {
return fmt.Sprintf("error with field %s = '%v'",
f.Name, f.Value)
} else {
return fmt.Sprintf("error with field %s = '%v' (%s)",
f.Name, f.Value, f.Reason)
}
} else {
s := "error with the following fields:\n"
for _, f := range e.ErroredFields {
if f.Reason == "" {
s = s + fmt.Sprintf("- %s = '%v'\n",
f.Name, f.Value)
} else {
s = s + fmt.Sprintf("- %s = '%v' (%s)\n",
f.Name, f.Value, f.Reason)
}
}
return s
}
}
// Error indicating insufficient identifiers have been supplied on a resource
// management request (create, apply, update, get, delete).
type ErrorInsufficientIdentifiers struct {
Name string
}
func (e ErrorInsufficientIdentifiers) Error() string {
return fmt.Sprintf("insufficient identifiers, missing '%s'", e.Name)
}
// Error indicating an atomic update attempt that failed due to a update conflict.
type ErrorResourceUpdateConflict struct {
Err error
Identifier interface{}
}
func (e ErrorResourceUpdateConflict) Error() string {
return fmt.Sprintf("update conflict: '%s'", e.Identifier)
}
// UpdateErrorIdentifier modifies the supplied error to use the new resource
// identifier.
func UpdateErrorIdentifier(err error, id interface{}) error {
if err == nil {
return nil
}
switch e := err.(type) {
case ErrorDatastoreError:
e.Identifier = id
err = e
case ErrorResourceDoesNotExist:
e.Identifier = id
err = e
case ErrorOperationNotSupported:
e.Identifier = id
err = e
case ErrorResourceAlreadyExists:
e.Identifier = id
err = e
case ErrorResourceUpdateConflict:
e.Identifier = id
err = e
}
return err
}