Return the Meta on error

Previously, this would never actually return the Meta data structure
since by the time that it got parsed, we already knew that the request
was good.  Now, we do a special parse when we know that it failed so
that we can return the structured data (in particular, we want to be
able to use the HTTP status code).
This commit is contained in:
Douglas Danger Manley 2020-12-20 13:28:12 -05:00
parent d657643538
commit 7f8d829ff7

View File

@ -3,7 +3,6 @@ package groupme
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
)
@ -77,7 +76,14 @@ func (c Client) do(req *http.Request, i interface{}) error {
// Check Status Code is 1XX or 2XX
if getResp.StatusCode/100 > 2 {
return fmt.Errorf("%s: %s", getResp.Status, string(bytes))
resp := newJSONResponse(nil)
if err := json.Unmarshal(bytes, &resp); err != nil {
// We couldn't parse the output. Oh well; generate the appropriate error type anyway.
return &Meta{
Code: HTTPStatusCode(getResp.StatusCode),
}
}
return &resp.Meta
}
if i == nil {
@ -89,11 +95,6 @@ func (c Client) do(req *http.Request, i interface{}) error {
return err
}
// Check Status Code is 1XX or 2XX
if resp.Meta.Code/100 > 2 {
return &resp.Meta
}
return nil
}