Skip to content

Commit 46a2f52

Browse files
committedApr 24, 2019
Return errors from JSON encode/decode
I found two JSON errors being shadowed; the one in amazon/validator.go is being hidden by errors.New(responseError.Message) -- which should be an empty string if there's a JSON error. So, it wouldn't report success, but this gives the caller better information on what failed. The second is in appstore/validator.go, which was ignoring encode errors before POSTing a verify request.
1 parent 04b80e5 commit 46a2f52

File tree

2 files changed

+6
-1
lines changed

2 files changed

+6
-1
lines changed
 

‎amazon/validator.go

+3
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,9 @@ func (c *Client) Verify(ctx context.Context, userID string, receiptID string) (I
102102
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
103103
responseError := IAPResponseError{}
104104
err = json.NewDecoder(resp.Body).Decode(&responseError)
105+
if err != nil {
106+
return result, err
107+
}
105108
return result, errors.New(responseError.Message)
106109
}
107110

‎appstore/validator.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,9 @@ func NewWithClient(client *http.Client) *Client {
9898
// Verify sends receipts and gets validation result
9999
func (c *Client) Verify(ctx context.Context, reqBody IAPRequest, result interface{}) error {
100100
b := new(bytes.Buffer)
101-
json.NewEncoder(b).Encode(reqBody)
101+
if err := json.NewEncoder(b).Encode(reqBody); err != nil {
102+
return err
103+
}
102104

103105
req, err := http.NewRequest("POST", c.ProductionURL, b)
104106
if err != nil {

0 commit comments

Comments
 (0)
Failed to load comments.