From caea04240cef1047b0f7250b639c4f6ae007c2df Mon Sep 17 00:00:00 2001 From: Rajat Jindal Date: Mon, 23 Oct 2023 21:40:37 +0530 Subject: [PATCH] add nil check in ErrorResponse.Error method Signed-off-by: Rajat Jindal --- github/github.go | 14 +++++++++++--- github/github_test.go | 13 +++++++++++++ 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/github/github.go b/github/github.go index edd86ab2ad..834aa86bc6 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 90e95b035f..b994496cc0 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) {