Skip to content

Commit

Permalink
Create error on http response error (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
sardyy authored and DaniFrancisco committed Nov 8, 2018
1 parent 1ba0de0 commit 15529e5
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions vero/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"net/http"

"github.com/uniplaces/vero-go"
"github.com/pkg/errors"
)

const baseUrl = "https://api.getvero.com/api/v2/%v"
Expand Down Expand Up @@ -149,5 +150,18 @@ func (VeroClient) send(url string, data map[string]interface{}, method string) (
return []byte{}, err
}

if response.StatusCode != http.StatusOK {
return []byte{}, createErrorWithHTTPResponseMessage(body)
}

return body, nil
}

func createErrorWithHTTPResponseMessage(responseBody []byte) error {
var responseData map[string]interface{}
if err := json.Unmarshal(responseBody, &responseData); err != nil {
return err
}

return errors.New(fmt.Sprintf("%s", responseData["message"]))
}

0 comments on commit 15529e5

Please sign in to comment.