...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package runtime
16
17 import (
18 "encoding/json"
19 "fmt"
20 "io"
21 )
22
23
24
25 type ClientResponse interface {
26 Code() int
27 Message() string
28 GetHeader(string) string
29 GetHeaders(string) []string
30 Body() io.ReadCloser
31 }
32
33
34 type ClientResponseReaderFunc func(ClientResponse, Consumer) (interface{}, error)
35
36
37 func (read ClientResponseReaderFunc) ReadResponse(resp ClientResponse, consumer Consumer) (interface{}, error) {
38 return read(resp, consumer)
39 }
40
41
42
43 type ClientResponseReader interface {
44 ReadResponse(ClientResponse, Consumer) (interface{}, error)
45 }
46
47
48 func NewAPIError(opName string, payload interface{}, code int) *APIError {
49 return &APIError{
50 OperationName: opName,
51 Response: payload,
52 Code: code,
53 }
54 }
55
56
57 type APIError struct {
58 OperationName string
59 Response interface{}
60 Code int
61 }
62
63 func (o *APIError) Error() string {
64 var resp []byte
65 if err, ok := o.Response.(error); ok {
66 resp = []byte("'" + err.Error() + "'")
67 } else {
68 resp, _ = json.Marshal(o.Response)
69 }
70 return fmt.Sprintf("%s (status %d): %s", o.OperationName, o.Code, resp)
71 }
72
73 func (o *APIError) String() string {
74 return o.Error()
75 }
76
77
78 func (o *APIError) IsSuccess() bool {
79 return o.Code/100 == 2
80 }
81
82
83 func (o *APIError) IsRedirect() bool {
84 return o.Code/100 == 3
85 }
86
87
88 func (o *APIError) IsClientError() bool {
89 return o.Code/100 == 4
90 }
91
92
93 func (o *APIError) IsServerError() bool {
94 return o.Code/100 == 5
95 }
96
97
98 func (o *APIError) IsCode(code int) bool {
99 return o.Code == code
100 }
101
102
103
104 type ClientResponseStatus interface {
105 IsSuccess() bool
106 IsRedirect() bool
107 IsClientError() bool
108 IsServerError() bool
109 IsCode(int) bool
110 }
111
View as plain text