Skip to content

Commit 3c49fd9

Browse files
authored
Add nil check in ErrorResponse.Error method (#2971)
Fixes: #2970.
1 parent eea6e0a commit 3c49fd9

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

github/github.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1017,9 +1017,17 @@ type ErrorBlock struct {
10171017
}
10181018

10191019
func (r *ErrorResponse) Error() string {
1020-
return fmt.Sprintf("%v %v: %d %v %+v",
1021-
r.Response.Request.Method, sanitizeURL(r.Response.Request.URL),
1022-
r.Response.StatusCode, r.Message, r.Errors)
1020+
if r.Response != nil && r.Response.Request != nil {
1021+
return fmt.Sprintf("%v %v: %d %v %+v",
1022+
r.Response.Request.Method, sanitizeURL(r.Response.Request.URL),
1023+
r.Response.StatusCode, r.Message, r.Errors)
1024+
}
1025+
1026+
if r.Response != nil {
1027+
return fmt.Sprintf("%d %v %+v", r.Response.StatusCode, r.Message, r.Errors)
1028+
}
1029+
1030+
return fmt.Sprintf("%v %+v", r.Message, r.Errors)
10231031
}
10241032

10251033
// Is returns whether the provided error equals this error.

github/github_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2097,6 +2097,19 @@ func TestErrorResponse_Error(t *testing.T) {
20972097
if err.Error() == "" {
20982098
t.Errorf("Expected non-empty ErrorResponse.Error()")
20992099
}
2100+
2101+
//dont panic if request is nil
2102+
res = &http.Response{}
2103+
err = ErrorResponse{Message: "m", Response: res}
2104+
if err.Error() == "" {
2105+
t.Errorf("Expected non-empty ErrorResponse.Error()")
2106+
}
2107+
2108+
//dont panic if response is nil
2109+
err = ErrorResponse{Message: "m"}
2110+
if err.Error() == "" {
2111+
t.Errorf("Expected non-empty ErrorResponse.Error()")
2112+
}
21002113
}
21012114

21022115
func TestError_Error(t *testing.T) {

0 commit comments

Comments
 (0)