From 498f9e2f32f280ec2a1e96a2938723d45cb835ec Mon Sep 17 00:00:00 2001 From: Nick Thomas <nick@gitlab.com> Date: Thu, 13 Apr 2017 14:45:33 +0100 Subject: [PATCH] Stop Fail500 and LogError from panicing with nil request or error --- internal/helper/helpers.go | 10 ++++++++-- internal/helper/helpers_test.go | 11 +++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/internal/helper/helpers.go b/internal/helper/helpers.go index f695087b3d868..c9ce6221398c7 100644 --- a/internal/helper/helpers.go +++ b/internal/helper/helpers.go @@ -19,12 +19,18 @@ const NginxResponseBufferHeader = "X-Accel-Buffering" func Fail500(w http.ResponseWriter, r *http.Request, err error) { http.Error(w, "Internal server error", 500) - captureRavenError(r, err) + if err != nil { + captureRavenError(r, err) + } + printError(r, err) } func LogError(r *http.Request, err error) { - captureRavenError(r, err) + if err != nil { + captureRavenError(r, err) + } + printError(r, err) } diff --git a/internal/helper/helpers_test.go b/internal/helper/helpers_test.go index c26766a8655d3..9668058c7a23c 100644 --- a/internal/helper/helpers_test.go +++ b/internal/helper/helpers_test.go @@ -99,3 +99,14 @@ func TestApplicationJson(t *testing.T) { req.Header.Set("Content-Type", "text/plain") assert.False(t, IsApplicationJson(req), "expected not to match 'text/plain' as 'application/json'") } + +func TestFail500WorksWithNils(t *testing.T) { + body := bytes.NewBuffer(nil) + w := httptest.NewRecorder() + w.Body = body + + Fail500(w, nil, nil) + + assert.Equal(t, http.StatusInternalServerError, w.Code) + assert.Equal(t, "Internal server error\n", body.String()) +} -- GitLab