Skip to content

Commit

Permalink
Merge pull request #716 from loadimpact/cloud-errmsg-fixes
Browse files Browse the repository at this point in the history
Improve the error messages from the Load Impact cloud API
  • Loading branch information
na-- authored Jul 23, 2018
2 parents 9531175 + 2895225 commit d9dbea1
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 9 deletions.
1 change: 1 addition & 0 deletions release notes/upcoming.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Description of feature.
## UX

* The consolidated user-supplied options (the final combination of config file options, exported script `options`, environment variables and command-line flags) are now exported back into the `options` script variable and can be accessed from the script. Thanks to @mohanprasaths for working on this! (#681 and #713)
* Improved error messages when outputting results to or executing tests in the Load Impact cloud (#716)

## Bugs fixed!

Expand Down
2 changes: 1 addition & 1 deletion stats/cloud/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func TestAuthorizedError(t *testing.T) {

assert.Equal(t, 1, called)
assert.Nil(t, resp)
assert.EqualError(t, err, ErrNotAuthorized.Error())
assert.EqualError(t, err, "403 Not allowed [err code 5]")
}

func TestRetry(t *testing.T) {
Expand Down
13 changes: 7 additions & 6 deletions stats/cloud/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,12 +160,6 @@ func checkResponse(r *http.Response) error {
return nil
}

if r.StatusCode == 401 {
return ErrNotAuthenticated
} else if r.StatusCode == 403 {
return ErrNotAuthorized
}

data, err := ioutil.ReadAll(r.Body)
if err != nil {
return err
Expand All @@ -175,13 +169,20 @@ func checkResponse(r *http.Response) error {
Error ErrorResponse `json:"error"`
}
if err := json.Unmarshal(data, &payload); err != nil {
if r.StatusCode == http.StatusUnauthorized {
return ErrNotAuthenticated
}
if r.StatusCode == http.StatusForbidden {
return ErrNotAuthorized
}
return errors.Errorf(
"Unexpected HTTP error from %s: %d %s",
r.Request.URL,
r.StatusCode,
http.StatusText(r.StatusCode),
)
}
payload.Error.Response = r
return payload.Error
}

Expand Down
7 changes: 5 additions & 2 deletions stats/cloud/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
package cloud

import (
"fmt"
"net/http"
"strconv"
"strings"

"github.com/pkg/errors"
Expand All @@ -45,8 +45,11 @@ type ErrorResponse struct {

func (e ErrorResponse) Error() string {
msg := e.Message
if e.Response != nil {
msg = fmt.Sprintf("%d %s", e.Response.StatusCode, msg)
}
if e.Code != 0 {
msg = strconv.Itoa(e.Code) + " " + msg
msg = fmt.Sprintf("%s [err code %d]", msg, e.Code)
}

var details []string
Expand Down

0 comments on commit d9dbea1

Please sign in to comment.