-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patherror.go
237 lines (209 loc) · 4.96 KB
/
error.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
package fitbit
import (
"encoding/json"
"fmt"
"net/http"
"strings"
)
// Error is the interface that has ability to return raw error returned from Fitbit APIs.
//
// This also implements the builtin error interface.
type Error interface {
Raw() []byte
Error() string
}
// RawError represents an error that only contains raw error returned from Fitbit APIs.
type RawError struct {
raw []byte
}
// Raw implements ability to return raw error.
func (e *RawError) Raw() []byte {
return e.raw
}
// Error implements the error interface.
func (e *RawError) Error() string {
return string(e.raw)
}
// MessageError represents an error returned from Fitbit APIs
// that contains error type and message fields.
type MessageError struct {
*RawError
Type string
Message string
}
// Error implements the error interface.
func (e *MessageError) Error() string {
return e.Message
}
// FieldNameMessageError represents an error returned from Fitbit APIs
// that contains MessageError plus fieldName.
type FieldNameMessageError struct {
*MessageError
FieldName string
}
// DetailError represents an error returned from Fitbit APIs
// that contains error title and detail fields.
type DetailError struct {
*RawError
Title string
Detail string
}
// Error implements the error interface.
func (e *DetailError) Error() string {
return e.Detail
}
// DetailSourceError represents an error returned from Fitbit APIs
// that contains DetailError plus source parameter.
type DetailSourceError struct {
*DetailError
Source struct {
Parameter string
}
}
type (
rawErrorResponse struct {
Success bool `json:"success"`
Errors []map[string]interface{} `json:"errors"`
}
// ErrorResponse represents an error response from Fitbit APIs.
ErrorResponse struct {
Success bool `json:"success"`
Errors []Error `json:"errors"`
}
)
// UnmarshalJSON implements the json.Unmarshaler interface.
func (e *ErrorResponse) UnmarshalJSON(b []byte) error {
raw := rawErrorResponse{
Success: true,
Errors: nil,
}
if err := json.Unmarshal(b, &raw); err != nil {
return err
}
e.Success = raw.Success
if len(raw.Errors) == 0 {
return nil
}
e.Errors = make([]Error, len(raw.Errors))
for i := range raw.Errors {
_err, err := extractError(raw.Errors[i])
if err != nil {
return err
}
e.Errors[i] = _err
}
return nil
}
func extractError(e map[string]interface{}) (Error, error) {
b, err := json.Marshal(e)
if err != nil {
return nil, err
}
rawError := &RawError{b}
// Check if it is MessageError or FieldNameMessageError
errorType, ok1 := e["errorType"].(string)
message, ok2 := e["message"].(string)
if ok1 && ok2 {
messageError := &MessageError{
RawError: rawError,
Type: errorType,
Message: message,
}
if fieldName, ok := e["fieldName"].(string); ok {
return &FieldNameMessageError{
MessageError: messageError,
FieldName: fieldName,
}, nil
}
return messageError, nil
}
// Check if it is DetailError or DetailSourceError
title, ok3 := e["title"].(string)
detail, ok4 := e["detail"].(string)
if ok3 && ok4 {
detailError := &DetailError{
RawError: rawError,
Title: title,
Detail: detail,
}
if source, ok := e["source"].(map[string]interface{}); ok {
if parameter, ok := source["parameter"].(string); ok {
return &DetailSourceError{
DetailError: detailError,
Source: struct {
Parameter string
}{
Parameter: parameter,
},
}, nil
}
}
return detailError, nil
}
return rawError, nil
}
func parseErrorResponse(b []byte) (*ErrorResponse, error) {
if !json.Valid(b) {
return nil, nil
}
var errResp ErrorResponse
if err := json.Unmarshal(b, &errResp); err != nil {
return nil, err
}
return &errResp, nil
}
// APIError represents an error that occurred on a request to Fitbit APIs.
type APIError struct {
ErrResp *ErrorResponse
HTTPResp *http.Response
Body []byte
}
// Error implements the error interface.
func (ae *APIError) Error() string {
if ae.ErrResp == nil || len(ae.ErrResp.Errors) == 0 {
return ae.HTTPResp.Status
}
errMsgs := make([]string, len(ae.ErrResp.Errors))
for i, e := range ae.ErrResp.Errors {
errMsgs[i] = e.Error()
}
return strings.Join(errMsgs, "\n")
}
// RequestError represents an error that occurred in a request process.
type RequestError struct {
Op string
URL string
Err error
}
func wrapAsRequestError(op, url string, err error) error {
if err == nil {
return nil
}
return &RequestError{
Op: op,
URL: url,
Err: err,
}
}
// Unwrap adds support for `errors` error wrapping.
func (e *RequestError) Unwrap() error {
return e.Err
}
// Error implements the error interface.
func (e *RequestError) Error() string {
return fmt.Sprintf("%s %q: %s", e.Op, e.URL, e.Err)
}
func parseError(r *http.Response, b []byte) error {
errResp, err := parseErrorResponse(b)
if err != nil {
return err
}
if errResp == nil || !errResp.Success {
return &APIError{
ErrResp: errResp,
HTTPResp: r,
Body: b,
}
}
return nil
}