Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add nil check in ErrorResponse.Error method #2971

Merged
merged 1 commit into from Oct 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 11 additions & 3 deletions github/github.go
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
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