-
Notifications
You must be signed in to change notification settings - Fork 2
/
errors.go
250 lines (208 loc) · 6.22 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
package vaultkv
import (
"encoding/json"
"errors"
"fmt"
"net/http"
"strings"
)
//ErrBadRequest represents 400 status codes that are returned from the API.
//See: your fault.
type ErrBadRequest struct {
message string
}
func (e *ErrBadRequest) Error() string {
return fmt.Sprintf("400 Bad Request: %s", e.message)
}
//IsBadRequest returns true if the error is an ErrBadRequest
func IsBadRequest(err error) bool {
_, is := err.(*ErrBadRequest)
return is
}
//ErrForbidden represents 403 status codes returned from the API. This could be
// if your auth is wrong or expired, or you simply don't have access to do the
// particular thing you're trying to do. Check your privilege.
type ErrForbidden struct {
message string
}
func (e *ErrForbidden) Error() string {
return fmt.Sprintf("403 Forbidden: %s", e.message)
}
//IsForbidden returns true if the error is an ErrForbidden
func IsForbidden(err error) bool {
_, is := err.(*ErrForbidden)
return is
}
//ErrNotFound represents 404 status codes returned from the API. This could be
// either that the thing you're looking for doesn't exist, or in some cases
// that you don't have access to the thing you're looking for and that Vault is
// hiding it from you.
type ErrNotFound struct {
message string
}
func (e *ErrNotFound) Error() string {
return fmt.Sprintf("404 Not Found: %s", e.message)
}
//IsNotFound returns true if the error is an ErrNotFound
func IsNotFound(err error) bool {
_, is := err.(*ErrNotFound)
return is
}
//ErrStandby is only returned from Health() if standbyok is set to false and the
// node you're querying is a standby.
type ErrStandby struct {
message string
}
func (e *ErrStandby) Error() string {
return fmt.Sprintf("429 Standby: %s", e.message)
}
//IsErrStandby returns true if the error is an ErrStandby
func IsErrStandby(err error) bool {
_, is := err.(*ErrStandby)
return is
}
//ErrDRSecondary is only returned from Health() if standbyok is set to false
//and the node you're querying is a secondary disaster recovery node.
type ErrDRSecondary struct {
message string
}
func (e *ErrDRSecondary) Error() string {
return fmt.Sprintf("472 DRSecondary: %s", e.message)
}
//IsErrDRSecondary returns true if the error is an ErrDRSecondary
func IsErrDRSecondary(err error) bool {
_, is := err.(*ErrDRSecondary)
return is
}
//ErrPerfStandby is only returned from Health() if standbyok is set to false
//and the node you're querying is a performance standby node.
type ErrPerfStandby struct {
message string
}
func (e *ErrPerfStandby) Error() string {
return fmt.Sprintf("473 PerfStandby %s", e.message)
}
//IsErrPerfStandby returns true if the error is an ErrPerfStandby
func IsErrPerfStandby(err error) bool {
_, is := err.(*ErrPerfStandby)
return is
}
//IsAnyStandbyErr returns true if the error is that the node is a standby or a
//performance standby
func IsAnyStandbyErr(err error) bool {
return IsErrStandby(err) || IsErrPerfStandby(err)
}
//ErrInternalServer represents 500 status codes that are returned from the API.
//See: their fault.
type ErrInternalServer struct {
message string
}
func (e *ErrInternalServer) Error() string {
return fmt.Sprintf("500 Internal Server Error: %s", e.message)
}
//IsInternalServer returns true if the error is an ErrInternalServer
func IsInternalServer(err error) bool {
_, is := err.(*ErrInternalServer)
return is
}
//ErrSealed represents the 503 status code that is returned by Vault most
// commonly if the Vault is currently sealed, but could also represent the Vault
// being in a maintenance state.
type ErrSealed struct {
message string
}
func (e *ErrSealed) Error() string {
return fmt.Sprintf("503 Sealed: %s", e.message)
}
//IsSealed returns true if the error is an ErrSealed
func IsSealed(err error) bool {
_, is := err.(*ErrSealed)
return is
}
//ErrUninitialized represents a 503 status code being returned and the Vault
//being uninitialized.
type ErrUninitialized struct {
message string
}
func (e *ErrUninitialized) Error() string {
return fmt.Sprintf("503 Uninitialized: %s", e.message)
}
//IsUninitialized returns true if the error is an ErrUninitialized
func IsUninitialized(err error) bool {
_, is := err.(*ErrUninitialized)
return is
}
//ErrTransport is returned if an error was encountered trying to reach the API,
// as opposed to an error from the API, is returned
type ErrTransport struct {
message string
}
func (e *ErrTransport) Error() string {
return fmt.Sprintf("Transport Error: %s", e.message)
}
//IsTransport returns true if the error is an ErrTransport
func IsTransport(err error) bool {
_, is := err.(*ErrTransport)
return is
}
//ErrKVUnsupported is returned by the KV object when the user requests an
// operation that cannot be performed by the actual version of the KV backend
// that the KV object is abstracting
type ErrKVUnsupported struct {
message string
}
func (e *ErrKVUnsupported) Error() string {
return fmt.Sprintf("Operation unsupported by KV version: %s", e.message)
}
//IsErrKVUnsupported returns true if the error is an ErrKVUnsupported
func IsErrKVUnsupported(err error) bool {
_, is := err.(*ErrKVUnsupported)
return is
}
type apiError struct {
Errors []string `json:"errors"`
}
func (v *Client) parseError(r *http.Response) (err error) {
errorsStruct := apiError{}
err = json.NewDecoder(r.Body).Decode(&errorsStruct)
if err != nil {
if contentType := r.Header.Get("Content-Type"); contentType != "application/json" {
return fmt.Errorf("Could not parse response body as JSON, and returned Content-Type is `%s'. Client may not be reaching Vault", contentType)
}
return err
}
errorMessage := strings.Join(errorsStruct.Errors, "\n")
switch r.StatusCode {
case 400:
err = &ErrBadRequest{message: errorMessage}
case 403:
err = &ErrForbidden{message: errorMessage}
case 404:
err = &ErrNotFound{message: errorMessage}
case 500:
err = &ErrInternalServer{message: errorMessage}
case 503:
err = v.parse503(errorMessage)
default:
err = errors.New(errorMessage)
}
return
}
func (v *Client) parse503(message string) (err error) {
err = v.Health(true)
if err == nil {
return nil
}
switch e := err.(type) {
case *ErrStandby:
e.message = message
err = e
case *ErrUninitialized:
e.message = message
err = e
case *ErrSealed:
e.message = message
err = e
}
return err
}