mirror of
https://github.com/interviewstreet/go-jira.git
synced 2025-01-22 03:10:10 +02:00
Merge pull request #92 from rbriski/issues/91
Fixed null pointer when no response is received (#91)
This commit is contained in:
commit
9f1498acb6
7
error.go
7
error.go
@ -19,6 +19,10 @@ type Error struct {
|
|||||||
|
|
||||||
// NewJiraError creates a new jira Error
|
// NewJiraError creates a new jira Error
|
||||||
func NewJiraError(resp *Response, httpError error) error {
|
func NewJiraError(resp *Response, httpError error) error {
|
||||||
|
if resp == nil {
|
||||||
|
return errors.Wrap(httpError, "No response returned")
|
||||||
|
}
|
||||||
|
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
body, err := ioutil.ReadAll(resp.Body)
|
body, err := ioutil.ReadAll(resp.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -28,7 +32,8 @@ func NewJiraError(resp *Response, httpError error) error {
|
|||||||
jerr := Error{HTTPError: httpError}
|
jerr := Error{HTTPError: httpError}
|
||||||
err = json.Unmarshal(body, &jerr)
|
err = json.Unmarshal(body, &jerr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrap(err, err.Error())
|
httpError = errors.Wrap(errors.New("Could not parse JSON"), httpError.Error())
|
||||||
|
return errors.Wrap(err, httpError.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
return &jerr
|
return &jerr
|
||||||
|
@ -29,6 +29,38 @@ func TestError_NewJiraError(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestError_NoResponse(t *testing.T) {
|
||||||
|
err := NewJiraError(nil, errors.New("Original http error"))
|
||||||
|
|
||||||
|
msg := err.Error()
|
||||||
|
if !strings.Contains(msg, "Original http error") {
|
||||||
|
t.Errorf("Expected the original error message: Got\n%s\n", msg)
|
||||||
|
}
|
||||||
|
|
||||||
|
if !strings.Contains(msg, "No response") {
|
||||||
|
t.Errorf("Expected the 'No response' error message: Got\n%s\n", msg)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestError_NoJSON(t *testing.T) {
|
||||||
|
setup()
|
||||||
|
defer teardown()
|
||||||
|
|
||||||
|
testMux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
fmt.Fprint(w, `<html>Not JSON</html>`)
|
||||||
|
})
|
||||||
|
|
||||||
|
req, _ := testClient.NewRequest("GET", "/", nil)
|
||||||
|
resp, _ := testClient.Do(req, nil)
|
||||||
|
|
||||||
|
err := NewJiraError(resp, errors.New("Original http error"))
|
||||||
|
msg := err.Error()
|
||||||
|
|
||||||
|
if !strings.Contains(msg, "Could not parse JSON") {
|
||||||
|
t.Errorf("Expected the 'Could not parse JSON' error message: Got\n%s\n", msg)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestError_NilOriginalMessage(t *testing.T) {
|
func TestError_NilOriginalMessage(t *testing.T) {
|
||||||
defer func() {
|
defer func() {
|
||||||
if r := recover(); r != nil {
|
if r := recover(); r != nil {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user