diff --git a/github/github.go b/github/github.go index edd86ab2ad8..834aa86bc6a 100644 --- a/github/github.go +++ b/github/github.go @@ -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. diff --git a/github/github_test.go b/github/github_test.go index 90e95b035f5..b994496cc01 100644 --- a/github/github_test.go +++ b/github/github_test.go @@ -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) {