diff options
Diffstat (limited to 'workhorse/internal/helper')
-rw-r--r-- | workhorse/internal/helper/helpers.go | 20 | ||||
-rw-r--r-- | workhorse/internal/helper/helpers_test.go | 116 | ||||
-rw-r--r-- | workhorse/internal/helper/raven.go | 2 |
3 files changed, 7 insertions, 131 deletions
diff --git a/workhorse/internal/helper/helpers.go b/workhorse/internal/helper/helpers.go index 5f1e5fc51b3..f9b46181579 100644 --- a/workhorse/internal/helper/helpers.go +++ b/workhorse/internal/helper/helpers.go @@ -20,13 +20,9 @@ import ( const NginxResponseBufferHeader = "X-Accel-Buffering" -func LogError(r *http.Request, err error) { - LogErrorWithFields(r, err, nil) -} - -func LogErrorWithFields(r *http.Request, err error, fields log.Fields) { +func logErrorWithFields(r *http.Request, err error, fields log.Fields) { if err != nil { - captureRavenError(r, err, fields) + CaptureRavenError(r, err, fields) } printError(r, err, fields) @@ -34,12 +30,7 @@ func LogErrorWithFields(r *http.Request, err error, fields log.Fields) { func CaptureAndFail(w http.ResponseWriter, r *http.Request, err error, msg string, code int) { http.Error(w, msg, code) - LogError(r, err) -} - -func CaptureAndFailWithFields(w http.ResponseWriter, r *http.Request, err error, msg string, code int, fields log.Fields) { - http.Error(w, msg, code) - LogErrorWithFields(r, err, fields) + logErrorWithFields(r, err, nil) } func Fail500(w http.ResponseWriter, r *http.Request, err error) { @@ -47,7 +38,8 @@ func Fail500(w http.ResponseWriter, r *http.Request, err error) { } func Fail500WithFields(w http.ResponseWriter, r *http.Request, err error, fields log.Fields) { - CaptureAndFailWithFields(w, r, err, "Internal server error", http.StatusInternalServerError, fields) + http.Error(w, "Internal server error", http.StatusInternalServerError) + logErrorWithFields(r, err, fields) } func RequestEntityTooLarge(w http.ResponseWriter, r *http.Request, err error) { @@ -60,7 +52,7 @@ func printError(r *http.Request, err error, fields log.Fields) { "method": r.Method, "uri": mask.URL(r.RequestURI), }) - entry.WithFields(fields).WithError(err).Error("error") + entry.WithFields(fields).WithError(err).Error() } else { log.WithFields(fields).WithError(err).Error("unknown error") } diff --git a/workhorse/internal/helper/helpers_test.go b/workhorse/internal/helper/helpers_test.go index 6a895aded03..93d1ee33d59 100644 --- a/workhorse/internal/helper/helpers_test.go +++ b/workhorse/internal/helper/helpers_test.go @@ -2,13 +2,11 @@ package helper import ( "bytes" - "fmt" "io" "net/http" "net/http/httptest" "testing" - "github.com/sirupsen/logrus" "github.com/stretchr/testify/require" ) @@ -142,117 +140,3 @@ func TestFail500WorksWithNils(t *testing.T) { require.Equal(t, http.StatusInternalServerError, w.Code) require.Equal(t, "Internal server error\n", body.String()) } - -func TestLogError(t *testing.T) { - tests := []struct { - name string - method string - uri string - err error - logMatchers []string - }{ - { - name: "nil_request", - err: fmt.Errorf("crash"), - logMatchers: []string{ - `level=error msg="unknown error" error=crash`, - }, - }, - { - name: "nil_request_nil_error", - err: nil, - logMatchers: []string{ - `level=error msg="unknown error" error="<nil>"`, - }, - }, - { - name: "basic_url", - method: "GET", - uri: "http://localhost:3000/", - err: fmt.Errorf("error"), - logMatchers: []string{ - `level=error msg=error correlation_id= error=error method=GET uri="http://localhost:3000/"`, - }, - }, - { - name: "secret_url", - method: "GET", - uri: "http://localhost:3000/path?certificate=123&sharedSecret=123&import_url=the_url&my_password_string=password", - err: fmt.Errorf("error"), - logMatchers: []string{ - `level=error msg=error correlation_id= error=error method=GET uri="http://localhost:3000/path\?certificate=\[FILTERED\]&sharedSecret=\[FILTERED\]&import_url=\[FILTERED\]&my_password_string=\[FILTERED\]"`, - }, - }, - } - - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - buf := &bytes.Buffer{} - - oldOut := logrus.StandardLogger().Out - logrus.StandardLogger().Out = buf - defer func() { - logrus.StandardLogger().Out = oldOut - }() - - var r *http.Request - if tt.uri != "" { - r = httptest.NewRequest(tt.method, tt.uri, nil) - } - - LogError(r, tt.err) - - logString := buf.String() - for _, v := range tt.logMatchers { - require.Regexp(t, v, logString) - } - }) - } -} - -func TestLogErrorWithFields(t *testing.T) { - tests := []struct { - name string - request *http.Request - err error - fields map[string]interface{} - logMatcher string - }{ - { - name: "nil_request", - err: fmt.Errorf("crash"), - fields: map[string]interface{}{"extra_one": 123}, - logMatcher: `level=error msg="unknown error" error=crash extra_one=123`, - }, - { - name: "nil_request_nil_error", - err: nil, - fields: map[string]interface{}{"extra_one": 123, "extra_two": "test"}, - logMatcher: `level=error msg="unknown error" error="<nil>" extra_one=123 extra_two=test`, - }, - { - name: "basic_url", - request: httptest.NewRequest("GET", "http://localhost:3000/", nil), - err: fmt.Errorf("error"), - fields: map[string]interface{}{"extra_one": 123, "extra_two": "test"}, - logMatcher: `level=error msg=error correlation_id= error=error extra_one=123 extra_two=test method=GET uri="http://localhost:3000/`, - }, - } - - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - buf := &bytes.Buffer{} - - oldOut := logrus.StandardLogger().Out - logrus.StandardLogger().Out = buf - defer func() { - logrus.StandardLogger().Out = oldOut - }() - - LogErrorWithFields(tt.request, tt.err, tt.fields) - - logString := buf.String() - require.Contains(t, logString, tt.logMatcher) - }) - } -} diff --git a/workhorse/internal/helper/raven.go b/workhorse/internal/helper/raven.go index ea1d0e1f6cc..898e8ec85f8 100644 --- a/workhorse/internal/helper/raven.go +++ b/workhorse/internal/helper/raven.go @@ -17,7 +17,7 @@ var ravenHeaderBlacklist = []string{ "Private-Token", } -func captureRavenError(r *http.Request, err error, fields log.Fields) { +func CaptureRavenError(r *http.Request, err error, fields log.Fields) { client := raven.DefaultClient extra := raven.Extra{} |