-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy patherror.go
257 lines (213 loc) · 7.45 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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
package synapse
/********** GLOBAL VARIABLES **********/
/********** TYPES **********/
type (
// ResponseError represents an error returned by the SynapseFI API
ResponseError struct {
ErrorCode string `json:"errorCode"`
HTTPCode string `json:"httpCode"`
Message string `json:"message"`
}
// ActionPending represents ERROR_CODE 10
// Accepted, but action pending
ActionPending struct {
ErrorCode string `json:"errorCode"`
HTTPCode string `json:"httpCode"`
Message string `json:"message"`
}
// IncorrectClientCredentials represents ERROR_CODE 100
// Incorrect Client Credentials
IncorrectClientCredentials struct {
ErrorCode string `json:"errorCode"`
HTTPCode string `json:"httpCode"`
Message string `json:"message"`
}
// IncorrectUserCredentials represents ERROR_CODE 110
// Incorrect User Credentials
IncorrectUserCredentials struct {
ErrorCode string `json:"errorCode"`
HTTPCode string `json:"httpCode"`
Message string `json:"message"`
}
// UnauthorizedFingerprint represents ERROR_CODE 120
// Unauthorized Fingerprint
UnauthorizedFingerprint struct {
ErrorCode string `json:"errorCode"`
HTTPCode string `json:"httpCode"`
Message string `json:"message"`
}
// PayloadError represents ERROR_CODE 200
// Error in Payload (Error in payload formatting)
PayloadError struct {
ErrorCode string `json:"errorCode"`
HTTPCode string `json:"httpCode"`
Message string `json:"message"`
}
// UnauthorizedAction represents ERROR_CODE 300
// Unauthorized action (User/Client not allowed to perform this action)
UnauthorizedAction struct {
ErrorCode string `json:"errorCode"`
HTTPCode string `json:"httpCode"`
Message string `json:"message"`
}
// IncorrectValues represents ERROR_CODE 400
// Incorrect Values Supplied (eg. Insufficient balance, wrong MFA response, incorrect micro deposits)
IncorrectValues struct {
ErrorCode string `json:"errorCode"`
HTTPCode string `json:"httpCode"`
Message string `json:"message"`
}
// ObjectNotFound represents ERROR_CODE 404
// Object not found
ObjectNotFound struct {
ErrorCode string `json:"errorCode"`
HTTPCode string `json:"httpCode"`
Message string `json:"message"`
}
// ActionNotAllowed represents ERROR_CODE 410
// Action Not Allowed on the object (either you do not have permissions or the action on this object is not supported)
ActionNotAllowed struct {
ErrorCode string `json:"errorCode"`
HTTPCode string `json:"httpCode"`
Message string `json:"message"`
}
// TooManyRequests represents ERROR_CODE 429
// Too many requests hit the API too quickly.
TooManyRequests struct {
ErrorCode string `json:"errorCode"`
HTTPCode string `json:"httpCode"`
Message string `json:"message"`
}
// IdempotencyConflict represents ERROR_CODE 450
// Idempotency key already in use
IdempotencyConflict struct {
ErrorCode string `json:"errorCode"`
HTTPCode string `json:"httpCode"`
Message string `json:"message"`
}
// RequestFailed represents ERROR_CODE 460
// Request Failed but not due to server error
RequestFailed struct {
ErrorCode string `json:"errorCode"`
HTTPCode string `json:"httpCode"`
Message string `json:"message"`
}
// ServerError represents ERROR_CODE 500
// Server Error
ServerError struct {
ErrorCode string `json:"errorCode"`
HTTPCode string `json:"httpCode"`
Message string `json:"message"`
}
// ServiceUnavailable represents ERROR_CODE 503
// Service Unavailable. The server is currently unable to handle the request due to a temporary overload or scheduled maintenance.
ServiceUnavailable struct {
ErrorCode string `json:"errorCode"`
HTTPCode string `json:"httpCode"`
Message string `json:"message"`
}
// ServerTimeout represents ERROR_CODE 504
// Server Timeout
ServerTimeout struct {
ErrorCode string `json:"errorCode"`
HTTPCode string `json:"httpCode"`
Message string `json:"message"`
}
// DefaultError represents an unhandled HTTP error
// Pass this instead of nil
DefaultError struct {
ErrorCode string "000"
HTTPCode string "000"
Message string "Error code not found"
}
)
/********** METHODS **********/
func (e *ActionPending) Error() string {
return formatErrorMessage(e.HTTPCode, e.ErrorCode, e.Message)
}
func (e *IncorrectClientCredentials) Error() string {
return formatErrorMessage(e.HTTPCode, e.ErrorCode, e.Message)
}
func (e *IncorrectUserCredentials) Error() string {
return formatErrorMessage(e.HTTPCode, e.ErrorCode, e.Message)
}
func (e *UnauthorizedFingerprint) Error() string {
return formatErrorMessage(e.HTTPCode, e.ErrorCode, e.Message)
}
func (e *PayloadError) Error() string {
return formatErrorMessage(e.HTTPCode, e.ErrorCode, e.Message)
}
func (e *UnauthorizedAction) Error() string {
return formatErrorMessage(e.HTTPCode, e.ErrorCode, e.Message)
}
func (e *IncorrectValues) Error() string {
return formatErrorMessage(e.HTTPCode, e.ErrorCode, e.Message)
}
func (e *ObjectNotFound) Error() string {
return formatErrorMessage(e.HTTPCode, e.ErrorCode, e.Message)
}
func (e *ActionNotAllowed) Error() string {
return formatErrorMessage(e.HTTPCode, e.ErrorCode, e.Message)
}
func (e *TooManyRequests) Error() string {
return formatErrorMessage(e.HTTPCode, e.ErrorCode, e.Message)
}
func (e *IdempotencyConflict) Error() string {
return formatErrorMessage(e.HTTPCode, e.ErrorCode, e.Message)
}
func (e *RequestFailed) Error() string {
return formatErrorMessage(e.HTTPCode, e.ErrorCode, e.Message)
}
func (e *ServerError) Error() string {
return formatErrorMessage(e.HTTPCode, e.ErrorCode, e.Message)
}
func (e *ServiceUnavailable) Error() string {
return formatErrorMessage(e.HTTPCode, e.ErrorCode, e.Message)
}
func (e *ServerTimeout) Error() string {
return formatErrorMessage(e.HTTPCode, e.ErrorCode, e.Message)
}
func (e *DefaultError) Error() string {
return formatErrorMessage(e.HTTPCode, e.ErrorCode, e.Message)
}
func handleAPIError(errorCode, httpCode, message string) error {
apiErrors := map[string]error{
"10": &ActionPending{errorCode, httpCode, message},
"100": &IncorrectClientCredentials{errorCode, httpCode, message},
"110": &IncorrectUserCredentials{errorCode, httpCode, message},
"120": &UnauthorizedFingerprint{errorCode, httpCode, message},
"200": &PayloadError{errorCode, httpCode, message},
"300": &UnauthorizedAction{errorCode, httpCode, message},
"400": &IncorrectValues{errorCode, httpCode, message},
"404": &ObjectNotFound{errorCode, httpCode, message},
"410": &ActionNotAllowed{errorCode, httpCode, message},
"429": &TooManyRequests{errorCode, httpCode, message},
"450": &IdempotencyConflict{errorCode, httpCode, message},
"460": &RequestFailed{errorCode, httpCode, message},
"500": &ServerError{errorCode, httpCode, message},
"503": &ServiceUnavailable{errorCode, httpCode, message},
"504": &ServerTimeout{errorCode, httpCode, message},
}
if code, ok := apiErrors[errorCode]; ok {
return code
}
return &DefaultError{}
}
func handleHTTPError(d []byte) error {
data := readStream(d)
errCode := data["error_code"].(string)
httpCode := data["http_code"].(string)
msg := data["error"].(map[string]interface{})["en"].(string)
return handleAPIError(errCode, httpCode, msg)
}
// HELPER METHODS
func formatErrorMessage(httpCode, errorCode, msg string) string {
return "http_code " + httpCode + " error_code " + errorCode + " " + msg
}
func formatErrorObject(httpCode, errorCode, msg string) map[string]interface{} {
return map[string]interface{}{
"http_code": httpCode,
"error_code": errorCode,
"error": msg,
}
}