forked from localyyz/go-shopify
/
error.go
255 lines (221 loc) · 5.48 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
// Copyright 2019 The go-shopify AUTHORS. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package shopify
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"reflect"
)
// Shopify errors usually have the form:
// {
// "errors": {
// "title": [
// "something is wrong"
// ]
// }
// }
//
// TODO: https://github.com/Jeffail/gabs (????)
type ShopifyErrorer interface {
Type() string
}
type lineItemErrorValue struct {
Message string `json:"message"`
Options struct {
Remaining int `json:"remaining"`
} `json:"options"`
Code string `json:"code"`
}
type lineItemErrorField map[string][]lineItemErrorValue
type LineItemError struct {
ShopifyErrorer
Field string
Message string
Code string
Position string
//Quantity []struct {
//Message string `json:"message"`
//Options struct {
//Remaining int `json:"remaining"`
//} `json:"options"`
//Code string `json:"code"`
//} `json:"quantity"`
}
type DiscountCodeError struct {
Reason interface{} `json:"reason"`
ShopifyErrorer
}
type AddressError struct {
ShopifyErrorer
Key string `json:"key"`
Field string `json:"field"`
Code string `json:"code"`
Message string `json:"message"`
}
type EmailError struct {
ShopifyErrorer
Message string `json:"message"`
}
type ErrorResponse struct {
Errors interface{} `json:"errors"`
}
var (
// TODO: make this an unmarshall type
ErrNotEnoughInStock = `not_enough_in_stock`
)
func (e *LineItemError) Error() string {
return fmt.Sprintf("%s at pos(%s): %s %s", e.Type(), e.Position, e.Field, e.Message)
}
func (e *LineItemError) Type() string {
return `line_items`
}
func (e *DiscountCodeError) Error() string {
return fmt.Sprintf("%+v", e.Reason)
}
func (e *DiscountCodeError) Type() string {
return `discount_code`
}
func (e *AddressError) Error() string {
return fmt.Sprintf("%s: %s %s", e.Type(), e.Field, e.Message)
}
func (e *AddressError) Type() string {
return e.Key
}
func (e *EmailError) Error() string {
return fmt.Sprintf("email %s", e.Message)
}
func (e *EmailError) Type() string {
return `email`
}
func (r *ErrorResponse) Error() string {
if e, ok := r.Errors.(map[string]interface{}); ok {
for k, v := range e {
// value here can be a slice
return fmt.Sprintf("%s: %+v", k, v)
}
}
if e, ok := r.Errors.(string); ok {
return e
}
return "unknown, unparsed error"
}
func toAddressError(key, field string, listError []interface{}) *AddressError {
for _, ee := range listError {
// NOTE: parse the first error found
if ex, _ := ee.(map[string]interface{}); ex != nil {
code, _ := ex["code"].(string)
message, _ := ex["message"].(string)
return &AddressError{
Key: key,
Field: field,
Code: code,
Message: message,
}
}
}
return nil
}
// CheckResponse checks the API response for errors, and returns them if
// present. A response is considered an error if it has a status code outside
// the 200 range or equal to 202 Accepted.
// API error responses are expected to have either no response
// body, or a JSON response body that maps to ErrorResponse. Any other
// response body will be silently ignored.
func CheckResponse(r *http.Response) error {
if r.StatusCode == http.StatusAccepted {
return nil
}
if r.StatusCode == http.StatusForbidden {
return errors.New("forbidden")
}
if c := r.StatusCode; 200 <= c && c <= 299 {
return nil
}
errorResponse := &ErrorResponse{}
data, err := ioutil.ReadAll(r.Body)
if err == nil && data != nil {
json.Unmarshal(data, errorResponse)
}
return findFirstError(errorResponse)
}
func findFirstError(r *ErrorResponse) error {
rr, ok := r.Errors.(map[string]interface{})
if !ok {
return r
}
// find the first error, and return
for k, v := range rr {
if k == "email" {
return &EmailError{
Message: "is invalid",
}
}
if vv, ok := v.(map[string]interface{}); ok {
switch k {
//TODO: shipping_line: map[id:[map[code:expired message:has expired options:map[]]]]
case "line_items":
for pos, vvv := range vv {
b, _ := json.Marshal(vvv)
var e lineItemErrorField
json.Unmarshal(b, &e)
ee := &LineItemError{
Position: pos,
}
if e != nil {
for ek, ev := range e {
ee.Field = ek
ee.Message = ev[0].Message
ee.Code = ev[0].Code
break
}
}
return ee
}
case "checkout":
for kk, vvv := range vv {
switch kk {
case "discount_code":
// list of fields
if e, _ := vvv.([]interface{}); e != nil {
for _, ee := range e {
if ex, _ := ee.(map[string]interface{}); ex != nil {
for _, reason := range ex {
return &DiscountCodeError{Reason: reason}
}
}
}
}
}
}
case "shipping_address", "billing_address":
for kk, vvv := range vv {
if e, ok := vvv.([]interface{}); ok && e != nil {
return toAddressError(k, kk, e)
}
}
}
} else if vv, ok := v.([]interface{}); ok {
switch k {
case "discount_code":
for _, vvv := range vv {
vvvv := vvv.(map[string]interface{})
return &DiscountCodeError{
Reason: vvvv["message"],
}
}
}
} else {
// TODO: concrete error type..
// I'm not keen on using another package like errorx to wrap error
// cause here. maybe need to expand the error handling a lot more
// for things to make sense.
return fmt.Errorf("unknown shopify error key %s, %+v, %v", k, v, reflect.TypeOf(v))
}
}
return r
}