Skip to content

Commit

Permalink
add nil check in ErrorResponse.Error method
Browse files Browse the repository at this point in the history
Signed-off-by: Rajat Jindal <[email protected]>
  • Loading branch information
rajatjindal committed Oct 23, 2023
1 parent eea6e0a commit caea042
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
14 changes: 11 additions & 3 deletions github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -1017,9 +1017,17 @@ type ErrorBlock struct {
}

func (r *ErrorResponse) Error() string {
return fmt.Sprintf("%v %v: %d %v %+v",
r.Response.Request.Method, sanitizeURL(r.Response.Request.URL),
r.Response.StatusCode, r.Message, r.Errors)
if r.Response != nil && r.Response.Request != nil {
return fmt.Sprintf("%v %v: %d %v %+v",
r.Response.Request.Method, sanitizeURL(r.Response.Request.URL),
r.Response.StatusCode, r.Message, r.Errors)
}

if r.Response != nil {
return fmt.Sprintf("%d %v %+v", r.Response.StatusCode, r.Message, r.Errors)
}

return fmt.Sprintf("%v %+v", r.Message, r.Errors)
}

// Is returns whether the provided error equals this error.
Expand Down
13 changes: 13 additions & 0 deletions github/github_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2097,6 +2097,19 @@ func TestErrorResponse_Error(t *testing.T) {
if err.Error() == "" {
t.Errorf("Expected non-empty ErrorResponse.Error()")
}

//dont panic if request is nil
res = &http.Response{}
err = ErrorResponse{Message: "m", Response: res}
if err.Error() == "" {
t.Errorf("Expected non-empty ErrorResponse.Error()")
}

//dont panic if response is nil
err = ErrorResponse{Message: "m"}
if err.Error() == "" {
t.Errorf("Expected non-empty ErrorResponse.Error()")
}
}

func TestError_Error(t *testing.T) {
Expand Down

0 comments on commit caea042

Please sign in to comment.