Skip to content

Commit

Permalink
Fix error unmarshaling when key value is string (#125)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathaningram authored Nov 10, 2020
1 parent 632f626 commit e292691
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
10 changes: 9 additions & 1 deletion goshopify.go
Original file line number Diff line number Diff line change
Expand Up @@ -511,9 +511,10 @@ func CheckResponseError(r *http.Response) error {
// A map, parse each error for each key in the map.
// json always serializes into map[string]interface{} for objects
for k, v := range shopifyError.Errors.(map[string]interface{}) {
switch reflect.TypeOf(v).Kind() {
// Check to make sure the interface is a slice
// json always serializes JSON arrays into []interface{}
if reflect.TypeOf(v).Kind() == reflect.Slice {
case reflect.Slice:
for _, elem := range v.([]interface{}) {
// If the primary message of the response error is not set, use
// any message.
Expand All @@ -523,6 +524,13 @@ func CheckResponseError(r *http.Response) error {
topicAndElem := fmt.Sprintf("%v: %v", k, elem)
responseError.Errors = append(responseError.Errors, topicAndElem)
}
case reflect.String:
elem := v.(string)
if responseError.Message == "" {
responseError.Message = fmt.Sprintf("%v: %v", k, elem)
}
topicAndElem := fmt.Sprintf("%v: %v", k, elem)
responseError.Errors = append(responseError.Errors, topicAndElem)
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions goshopify_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -738,6 +738,10 @@ func TestCheckResponseError(t *testing.T) {
httpmock.NewStringResponse(400, `{"errors": { "order": ["order is wrong"] }}`),
ResponseError{Status: 400, Message: "order: order is wrong", Errors: []string{"order: order is wrong"}},
},
{
httpmock.NewStringResponse(400, `{"errors": { "collection_id": "collection_id is wrong" }}`),
ResponseError{Status: 400, Message: "collection_id: collection_id is wrong", Errors: []string{"collection_id: collection_id is wrong"}},
},
{
httpmock.NewStringResponse(400, `{error:bad request}`),
errors.New("invalid character 'e' looking for beginning of object key string"),
Expand Down

0 comments on commit e292691

Please sign in to comment.