Skip to content

Commit

Permalink
Fix panic for unauthorized response with nil error
Browse files Browse the repository at this point in the history
  • Loading branch information
Rekha Mittal authored and andygrunwald committed Mar 20, 2019
1 parent cbab0bb commit 53c4680
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
3 changes: 3 additions & 0 deletions error.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ func NewJiraError(resp *Response, httpError error) error {
return errors.Wrap(err, httpError.Error())
}
} else {
if httpError == nil {
return fmt.Errorf("Got Response Status %s:%s", resp.Status, string(body))
}
return errors.Wrap(httpError, fmt.Sprintf("%s: %s", resp.Status, string(body)))
}

Expand Down
19 changes: 19 additions & 0 deletions error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,25 @@ func TestError_NoJSON(t *testing.T) {
}
}

func TestError_Unauthorized_NilError(t *testing.T) {
setup()
defer teardown()

testMux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusUnauthorized)
fmt.Fprint(w, `User is not authorized`)
})

req, _ := testClient.NewRequest("GET", "/", nil)
resp, _ := testClient.Do(req, nil)

err := NewJiraError(resp, nil)
msg := err.Error()
if !strings.Contains(msg, "401 Unauthorized:User is not authorized") {
t.Errorf("Expected Unauthorized HTTP status: Got\n%s\n", msg)
}
}

func TestError_BadJSON(t *testing.T) {
setup()
defer teardown()
Expand Down

0 comments on commit 53c4680

Please sign in to comment.