Skip to content

Commit

Permalink
Merge pull request #114 from Scalingo/fix/cli/447/regions_cache_only_…
Browse files Browse the repository at this point in the history
…regional_commands

errgo.Mask is terrible
  • Loading branch information
john-scalingo authored Jul 16, 2019
2 parents 9310c66 + 9af820c commit a5d5037
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 21 deletions.
15 changes: 7 additions & 8 deletions http/api_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
var (
defaultEndpoint = "https://api.scalingo.com"
defaultAPIVersion = "1"
ErrNoAuth = errgo.New("authentication required")
)

type APIRequest struct {
Expand Down Expand Up @@ -52,7 +51,7 @@ func (c *client) FillDefaultValues(req *APIRequest) error {
var err error
req.Token, err = c.TokenGenerator().GetAccessToken()
if err != nil {
return ErrNoAuth
return errgo.Notef(err, "fail to get the access token for this request")
}
}

Expand All @@ -75,7 +74,7 @@ func (statuses Statuses) Contains(status int) bool {
func (c *client) Do(req *APIRequest) (*http.Response, error) {
err := c.FillDefaultValues(req)
if err != nil {
return nil, errgo.Mask(err, errgo.Any)
return nil, errgo.Notef(err, "fail to fill client with default values")
}

endpoint := req.URL + req.Endpoint
Expand All @@ -91,22 +90,22 @@ func (c *client) Do(req *APIRequest) (*http.Response, error) {
case "WITH_BODY":
buffer, err := json.Marshal(req.Params)
if err != nil {
return nil, errgo.Mask(err, errgo.Any)
return nil, errgo.Notef(err, "fail to marshal params")
}
reader := bytes.NewReader(buffer)
req.HTTPRequest, err = http.NewRequest(req.Method, endpoint, reader)
if err != nil {
return nil, errgo.Mask(err, errgo.Any)
return nil, errgo.Notef(err, "fail to initialize the 'WITH_BODY' query")
}
case "GET", "DELETE":
values, err := req.BuildQueryFromParams()
if err != nil {
return nil, errgo.Mask(err, errgo.Any)
return nil, errgo.Notef(err, "fail to build the query params")
}
endpoint = fmt.Sprintf("%s?%s", endpoint, values.Encode())
req.HTTPRequest, err = http.NewRequest(req.Method, endpoint, nil)
if err != nil {
return nil, errgo.Mask(err, errgo.Any)
return nil, errgo.Notef(err, "fail to initialize the '%s' query", req.Method)
}
}

Expand Down Expand Up @@ -148,7 +147,7 @@ func (c *client) doRequest(req *http.Request) (*http.Response, error) {
return c.HTTPClient().Do(req)
}

func ParseJSON(res *http.Response, data interface{}) error {
func parseJSON(res *http.Response, data interface{}) error {
body, err := ioutil.ReadAll(res.Body)
if err != nil {
return errgo.Newf("fail to read body of request %v, %v", res.Request, err)
Expand Down
6 changes: 3 additions & 3 deletions http/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,17 +203,17 @@ func (c *client) SubresourceUpdate(resource, resourceID, subresource, id string,
func (c *client) DoRequest(req *APIRequest, data interface{}) error {
res, err := c.Do(req)
if err != nil {
return errgo.Mask(err, errgo.Any)
return err
}
defer res.Body.Close()

if data == nil {
return nil
}

err = ParseJSON(res, data)
err = parseJSON(res, data)
if err != nil {
return errgo.NoteMask(err, "fail to parse json of subresource response")
return errgo.Notef(err, "fail to parse JSON of subresource response")
}
return nil
}
Expand Down
18 changes: 9 additions & 9 deletions http/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,34 +89,34 @@ func NewRequestFailedError(res *http.Response, req *APIRequest) error {
switch res.StatusCode {
case 400:
var badRequestError BadRequestError
err := ParseJSON(res, &badRequestError)
err := parseJSON(res, &badRequestError)
if err != nil {
return errgo.Mask(err, errgo.Any)
return err
}
return &RequestFailedError{Code: res.StatusCode, APIError: badRequestError, Req: req}
case 401:
var apiErr APIError
ParseJSON(res, &apiErr)
parseJSON(res, &apiErr)
return &RequestFailedError{Code: res.StatusCode, APIError: errgo.New("unauthorized - you are not authorized to do this operation"), Req: req, Message: apiErr.Error}
case 402:
var paymentRequiredErr PaymentRequiredError
err := ParseJSON(res, &paymentRequiredErr)
err := parseJSON(res, &paymentRequiredErr)
if err != nil {
return errgo.Mask(err, errgo.Any)
return err
}
return &RequestFailedError{Code: res.StatusCode, APIError: paymentRequiredErr, Req: req}
case 404:
var notFoundErr NotFoundError
err := ParseJSON(res, &notFoundErr)
err := parseJSON(res, &notFoundErr)
if err != nil {
return errgo.Mask(err, errgo.Any)
return err
}
return &RequestFailedError{Code: res.StatusCode, APIError: notFoundErr, Req: req}
case 422:
var unprocessableError UnprocessableEntity
err := ParseJSON(res, &unprocessableError)
err := parseJSON(res, &unprocessableError)
if err != nil {
return errgo.Mask(err, errgo.Any)
return err
}
return &RequestFailedError{Code: res.StatusCode, APIError: unprocessableError, Req: req}
case 500:
Expand Down
2 changes: 1 addition & 1 deletion tokens.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func (c *Client) TokenExchange(token string) (string, error) {
var btRes BearerTokenRes
err = json.NewDecoder(res.Body).Decode(&btRes)
if err != nil {
return "", errgo.NoteMask(err, "invalid response from authentication service", errgo.Any)
return "", errgo.Notef(err, "invalid response from authentication service")
}

return btRes.Token, nil
Expand Down

0 comments on commit a5d5037

Please sign in to comment.