-
Notifications
You must be signed in to change notification settings - Fork 66
Expose the status of requests #37
base: master
Are you sure you want to change the base?
Conversation
Thanks for your pull request. It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). 📝 Please visit https://cla.developers.google.com/ to sign. Once you've signed, please reply here (e.g.
|
I've also signed the license agreement |
CLAs look good, thanks! |
gcmResp.Status = httpResp.StatusCode | ||
|
||
if gcmResp.Status != http.StatusOK { | ||
return gcmResp, fmt.Errorf("Encountered HTTP status code %d", gcmResp.Status) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jpatel531 You are ditching the response body, which can contain useful info. A better way on my opinion is to always read the body, try to unmarshal it, and if failed return the body as is.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fair point @bolshoy - although from reading the reference ( https://developers.google.com/cloud-messaging/http-server-ref#error-codes ) it doesn't look like non-200 bodies contain anything particularly interesting. How would you suggest we return a raw body? Include a RawBody []byte
in the HttpResponse
struct?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jpatel531 I don't think the upstream client is really interested in the response status code and its meaning, it needs either a response object or an error explaining why there's no object. So in case of http statuses 400,401,5xx in the spec, it would be something along the lines of
gcmResp := &HttpResponse{}
body, err := ioutil.ReadAll(httpResp.Body)
defer httpResp.Body.Close()
if err != nil {
return nil, fmt.Errorf("error reading http response body: %v", err)
}
if httpResp.StatusCode >= http.StatusBadRequest { // Not sure about 3xx
return nil, fmt.Errorf(fmt.Sprintf("http error: %s (%s)", httpResp.Status, string(body)))
}
err = json.Unmarshal(body, &gcmResp)
if err != nil {
return nil, fmt.Errorf("error unmarshaling json from body: %v %s", err, string(body))
}
Hi there,
Whenever I got 401 errors, the library would panic. The GCM response for 401s are in HTML, whereas the library was trying to parse the body from JSON regardless of the status.
This PR adds a
Status
field to theHttpResponse
struct, and also returns an error upon a non-200 response.Let me know what you think