-
Notifications
You must be signed in to change notification settings - Fork 120
/
errors.go
360 lines (322 loc) · 8.09 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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
/*
Copyright (c) 2019 Red Hat, Inc.
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.
*/
// IMPORTANT: This file has been generated automatically, refrain from modifying it manually as all
// your changes will be lost when the file is generated again.
package errors // github.com/openshift-online/ocm-sdk-go/errors
import (
"fmt"
"net/http"
"strconv"
"github.com/golang/glog"
"github.com/openshift-online/ocm-sdk-go/helpers"
)
// Error kind is the name of the type used to represent errors.
const ErrorKind = "Error"
// ErrorNilKind is the name of the type used to nil errors.
const ErrorNilKind = "ErrorNil"
// ErrorBuilder is a builder for the error type.
type ErrorBuilder struct {
id *string
href *string
code *string
reason *string
}
// Error represents errors.
type Error struct {
id *string
href *string
code *string
reason *string
}
// NewError returns a new ErrorBuilder
func NewError() *ErrorBuilder {
return new(ErrorBuilder)
}
// ID sets the id field for the ErrorBuilder
func (e *ErrorBuilder) ID(id string) *ErrorBuilder {
e.id = &id
return e
}
// HREF sets the href field for the ErrorBuilder
func (e *ErrorBuilder) HREF(href string) *ErrorBuilder {
e.href = &href
return e
}
// Code sets the cpde field for the ErrorBuilder
func (e *ErrorBuilder) Code(code string) *ErrorBuilder {
e.code = &code
return e
}
// Reason sets the reason field for the ErrorBuilder
func (e *ErrorBuilder) Reason(reason string) *ErrorBuilder {
e.reason = &reason
return e
}
// Build builds a new error type or returns an error.
func (e *ErrorBuilder) Build() (*Error, error) {
err := new(Error)
err.reason = e.reason
err.code = e.code
err.id = e.id
err.href = e.href
return err, nil
}
// Kind returns the name of the type of the error.
func (e *Error) Kind() string {
if e == nil {
return ErrorNilKind
}
return ErrorKind
}
// ID returns the identifier of the error.
func (e *Error) ID() string {
if e != nil && e.id != nil {
return *e.id
}
return ""
}
// GetID returns the identifier of the error and a flag indicating if the
// identifier has a value.
func (e *Error) GetID() (value string, ok bool) {
ok = e != nil && e.id != nil
if ok {
value = *e.id
}
return
}
// HREF returns the link to the error.
func (e *Error) HREF() string {
if e != nil && e.href != nil {
return *e.href
}
return ""
}
// GetHREF returns the link of the error and a flag indicating if the
// link has a value.
func (e *Error) GetHREF() (value string, ok bool) {
ok = e != nil && e.href != nil
if ok {
value = *e.href
}
return
}
// Code returns the code of the error.
func (e *Error) Code() string {
if e != nil && e.code != nil {
return *e.code
}
return ""
}
// GetCode returns the link of the error and a flag indicating if the
// code has a value.
func (e *Error) GetCode() (value string, ok bool) {
ok = e != nil && e.code != nil
if ok {
value = *e.code
}
return
}
// Reason returns the reason of the error.
func (e *Error) Reason() string {
if e != nil && e.reason != nil {
return *e.reason
}
return ""
}
// GetReason returns the link of the error and a flag indicating if the
// reason has a value.
func (e *Error) GetReason() (value string, ok bool) {
ok = e != nil && e.reason != nil
if ok {
value = *e.reason
}
return
}
// Error is the implementation of the error interface.
func (e *Error) Error() string {
if e.reason != nil {
return *e.reason
}
if e.code != nil {
return *e.code
}
if e.id != nil {
return *e.id
}
return "unknown error"
}
// UnmarshalError reads an error from the given which can be an slice of bytes, a
// string, a reader or a JSON decoder.
func UnmarshalError(source interface{}) (object *Error, err error) {
decoder, err := helpers.NewDecoder(source)
if err != nil {
return
}
data := new(errorData)
err = decoder.Decode(data)
if err != nil {
return
}
object, err = data.unwrap()
return
}
// MarshalError writes an error to the given destination which can be an slice of bytes, a
// string, a reader or a JSON decoder.
func (e *Error) MarshalError(destination interface{}) error {
encoder, err := helpers.NewEncoder(destination)
if err != nil {
return err
}
object, err := e.wrap()
if err != nil {
return err
}
err = encoder.Encode(object)
if err != nil {
return err
}
return nil
}
// errorData is the data structure used internally to marshal and unmarshal errors.
type errorData struct {
Kind *string "json:\"kind,omitempty\""
ID *string "json:\"id,omitempty\""
HREF *string "json:\"href,omitempty\""
Code *string "json:\"code,omitempty\""
Reason *string "json:\"reason,omitempty\""
}
// unwrap is the method used internally to convert the JSON unmarshalled data to an
// error.
func (d *errorData) unwrap() (object *Error, err error) {
if d == nil {
return
}
object = new(Error)
if d.Kind != nil && *d.Kind != ErrorKind {
err = fmt.Errorf(
"expected kind '%s' but got '%s'",
ErrorKind, *d.Kind,
)
return
}
object.id = d.ID
object.href = d.HREF
object.code = d.Code
object.reason = d.Reason
return
}
// wrap is the method used internally to convert the JSON unmarshalled data to an
// error.
func (d *Error) wrap() (object *errorData, err error) {
if d == nil {
return
}
object = new(errorData)
if d.Kind() != "" && d.Kind() != ErrorKind {
err = fmt.Errorf(
"expected kind '%s' but got '%s'",
ErrorKind, d.Kind(),
)
return
}
object.ID = d.id
object.HREF = d.href
object.Code = d.code
object.Reason = d.reason
return
}
var panicID = "1000"
var panicError, _ = NewError().
ID(panicID).
Reason("An unexpected error happened, please check the log of the service " +
"for details").
Build()
// SendError writes a given error and status code to a response writer.
// if an error occurred it will log the error and exit.
// This methods is used internaly and no backwards compatibily is guaranteed.
func SendError(w http.ResponseWriter, r *http.Request, error *Error) {
status, err := strconv.Atoi(error.ID())
if err != nil {
SendPanic(w, r)
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(status)
err = error.MarshalError(w)
if err != nil {
glog.Errorf("Can't send response body for request '%s'", r.URL.Path)
return
}
}
// SendPanic sends a panic error response to the client, but it doesn't end the process.
// This methods is used internaly and no backwards compatibily is guaranteed.
func SendPanic(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
err := panicError.MarshalError(w)
if err != nil {
glog.Errorf(
"Can't send panic response for request '%s': %s",
r.URL.Path,
err.Error(),
)
}
}
// SendNotFound sends a generic 404 error.
func SendNotFound(w http.ResponseWriter, r *http.Request) {
reason := fmt.Sprintf(
"Can't find resource for path '%s''",
r.URL.Path,
)
body, err := NewError().
ID("404").
Reason(reason).
Build()
if err != nil {
SendPanic(w, r)
return
}
SendError(w, r, body)
}
// SendMethodNotAllowed sends a generic 405 error.
func SendMethodNotAllowed(w http.ResponseWriter, r *http.Request) {
reason := fmt.Sprintf(
"Method '%s' isn't supported for path '%s''",
r.Method, r.URL.Path,
)
body, err := NewError().
ID("405").
Reason(reason).
Build()
if err != nil {
SendPanic(w, r)
return
}
SendError(w, r, body)
}
// SendInternalServerError sends a generic 500 error.
func SendInternalServerError(w http.ResponseWriter, r *http.Request) {
reason := fmt.Sprintf(
"Can't process '%s' request for path '%s' due to an internal"+
"server error",
r.Method, r.URL.Path,
)
body, err := NewError().
ID("500").
Reason(reason).
Build()
if err != nil {
SendPanic(w, r)
return
}
SendError(w, r, body)
}