forked from openshift-online/ocm-sdk-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherrors.go
452 lines (410 loc) · 10.4 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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
/*
Copyright (c) 2020 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"
"io"
"net/http"
"strconv"
"strings"
"github.com/golang/glog"
jsoniter "github.com/json-iterator/go"
"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 {
bitmap_ uint32
id string
href string
code string
reason string
details interface{}
operationID string
}
// Error represents errors.
type Error struct {
bitmap_ uint32
id string
href string
code string
reason string
details interface{}
operationID string
}
// NewError creates a new builder that can then be used to create error objects.
func NewError() *ErrorBuilder {
return &ErrorBuilder{}
}
// ID sets the identifier of the error.
func (b *ErrorBuilder) ID(value string) *ErrorBuilder {
b.id = value
b.bitmap_ |= 1
return b
}
// HREF sets the link of the error.
func (b *ErrorBuilder) HREF(value string) *ErrorBuilder {
b.href = value
b.bitmap_ |= 2
return b
}
// Code sets the code of the error.
func (b *ErrorBuilder) Code(value string) *ErrorBuilder {
b.code = value
b.bitmap_ |= 4
return b
}
// Reason sets the reason of the error.
func (b *ErrorBuilder) Reason(value string) *ErrorBuilder {
b.reason = value
b.bitmap_ |= 8
return b
}
// OperationID sets the identifier of the operation that caused the error.
func (b *ErrorBuilder) OperationID(value string) *ErrorBuilder {
b.operationID = value
b.bitmap_ |= 16
return b
}
// Details sets additional details of the error.
func (b *ErrorBuilder) Details(value interface{}) *ErrorBuilder {
b.details = value
b.bitmap_ |= 32
return b
}
// Build uses the information stored in the builder to create a new error object.
func (b *ErrorBuilder) Build() (result *Error, err error) {
result = &Error{
id: b.id,
href: b.href,
code: b.code,
reason: b.reason,
details: b.details,
operationID: b.operationID,
bitmap_: b.bitmap_,
}
return
}
// 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.bitmap_&1 != 0 {
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.bitmap_&1 != 0
if ok {
value = e.id
}
return
}
// HREF returns the link to the error.
func (e *Error) HREF() string {
if e != nil && e.bitmap_&2 != 0 {
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.bitmap_&2 != 0
if ok {
value = e.href
}
return
}
// Code returns the code of the error.
func (e *Error) Code() string {
if e != nil && e.bitmap_&4 != 0 {
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.bitmap_&4 != 0
if ok {
value = e.code
}
return
}
// Reason returns the reason of the error.
func (e *Error) Reason() string {
if e != nil && e.bitmap_&8 != 0 {
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.bitmap_&8 != 0
if ok {
value = e.reason
}
return
}
// OperationID returns the identifier of the operation that caused the error.
func (e *Error) OperationID() string {
if e != nil && e.bitmap_&16 != 0 {
return e.operationID
}
return ""
}
// GetOperationID returns the identifier of the operation that caused the error and
// a flag indicating if that identifier does have a value.
func (e *Error) GetOperationID() (value string, ok bool) {
ok = e != nil && e.bitmap_&16 != 0
if ok {
value = e.operationID
}
return
}
// Details returns the details of the error
func (e *Error) Details() interface{} {
if e != nil && e.bitmap_&32 != 0 {
return e.details
}
return nil
}
// GetDetails returns the details of the error and a flag
// indicating if the details have a value.
func (e *Error) GetDetails() (value interface{}, ok bool) {
ok = e != nil && e.bitmap_&32 != 0
if ok {
value = e.details
}
return
}
// Error is the implementation of the error interface.
func (e *Error) Error() string {
chunks := make([]string, 0, 3)
if e.id != "" {
chunks = append(chunks, fmt.Sprintf("identifier is '%s'", e.id))
}
if e.code != "" {
chunks = append(chunks, fmt.Sprintf("code is '%s'", e.code))
}
if e.operationID != "" {
chunks = append(chunks, fmt.Sprintf("operation identifier is '%s'", e.operationID))
}
var result string
size := len(chunks)
if size == 1 {
result = chunks[0]
} else if size > 1 {
result = strings.Join(chunks[0:size-1], ", ") + " and " + chunks[size-1]
}
if e.reason != "" {
if result != "" {
result = result + ": "
}
result = result + e.reason
}
if result == "" {
result = "unknown error"
}
return result
}
// String returns a string representing the error.
func (e *Error) String() string {
return e.Error()
}
// UnmarshalError reads an error from the given source which can be an slice of
// bytes, a string, a reader or a JSON decoder.
func UnmarshalError(source interface{}) (object *Error, err error) {
iterator, err := helpers.NewIterator(source)
if err != nil {
return
}
object = readError(iterator)
err = iterator.Error
return
}
func readError(iterator *jsoniter.Iterator) *Error {
object := &Error{}
for {
field := iterator.ReadObject()
if field == "" {
break
}
switch field {
case "id":
object.id = iterator.ReadString()
object.bitmap_ |= 1
case "href":
object.href = iterator.ReadString()
object.bitmap_ |= 2
case "code":
object.code = iterator.ReadString()
object.bitmap_ |= 4
case "reason":
object.reason = iterator.ReadString()
object.bitmap_ |= 8
case "operation_id":
object.operationID = iterator.ReadString()
object.bitmap_ |= 16
case "details":
object.details = iterator.ReadAny().GetInterface()
object.bitmap_ |= 32
default:
iterator.ReadAny()
}
}
return object
}
// MarshalError writes an error to the given writer.
func MarshalError(e *Error, writer io.Writer) error {
stream := helpers.NewStream(writer)
writeError(e, stream)
stream.Flush()
return stream.Error
}
func writeError(e *Error, stream *jsoniter.Stream) {
stream.WriteObjectStart()
stream.WriteObjectField("kind")
stream.WriteString(ErrorKind)
if e.bitmap_&1 != 0 {
stream.WriteMore()
stream.WriteObjectField("id")
stream.WriteString(e.id)
}
if e.bitmap_&2 != 0 {
stream.WriteMore()
stream.WriteObjectField("href")
stream.WriteString(e.href)
}
if e.bitmap_&4 != 0 {
stream.WriteMore()
stream.WriteObjectField("code")
stream.WriteString(e.code)
}
if e.bitmap_&8 != 0 {
stream.WriteMore()
stream.WriteObjectField("reason")
stream.WriteString(e.reason)
}
if e.bitmap_&16 != 0 {
stream.WriteMore()
stream.WriteObjectField("operation_id")
stream.WriteString(e.operationID)
}
if e.bitmap_&32 != 0 {
stream.WriteMore()
stream.WriteObjectField("details")
stream.WriteVal(e.details)
}
stream.WriteObjectEnd()
}
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, object *Error) {
status, err := strconv.Atoi(object.ID())
if err != nil {
SendPanic(w, r)
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(status)
err = MarshalError(object, 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 := MarshalError(panicError, 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)
}