Skip to content

Commit 0a81c0e

Browse files
authored
Merge pull request #112 from netlify/fix/routerZeroValueError
fix(router): early return when zero vallue of err in HandleError
2 parents eb929b0 + 8986997 commit 0a81c0e

File tree

3 files changed

+37
-12
lines changed

3 files changed

+37
-12
lines changed

router/errors.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package router
33
import (
44
"fmt"
55
"net/http"
6+
"unsafe"
67

78
"github.com/netlify/netlify-commons/tracing"
89
)
@@ -85,7 +86,8 @@ func httpError(code int, fmtString string, args ...interface{}) *HTTPError {
8586

8687
// HandleError will handle an error
8788
func HandleError(err error, w http.ResponseWriter, r *http.Request) {
88-
if err == nil {
89+
// if err is nil or nil pointer of type *HTTPError
90+
if err == nil || (*[2]uintptr)(unsafe.Pointer(&err))[1] == 0 {
8991
return
9092
}
9193

router/errors_test.go

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@ package router
33
import (
44
"errors"
55
"fmt"
6-
"github.com/netlify/netlify-commons/tracing"
7-
"github.com/sirupsen/logrus/hooks/test"
8-
"github.com/stretchr/testify/assert"
9-
"github.com/stretchr/testify/require"
106
"io/ioutil"
117
"net/http"
128
"net/http/httptest"
139
"testing"
10+
11+
"github.com/netlify/netlify-commons/tracing"
12+
"github.com/sirupsen/logrus/hooks/test"
13+
"github.com/stretchr/testify/assert"
14+
"github.com/stretchr/testify/require"
1415
)
1516

1617
func TestHandleError_ErrorIsNil(t *testing.T) {
@@ -28,6 +29,25 @@ func TestHandleError_ErrorIsNil(t *testing.T) {
2829
assert.Empty(t, w.Header())
2930
}
3031

32+
func TestHandleError_ErrorIsNilPointerToTypeHTTPError(t *testing.T) {
33+
logger, loggerOutput := test.NewNullLogger()
34+
w, r, _ := tracing.NewTracer(
35+
httptest.NewRecorder(),
36+
httptest.NewRequest(http.MethodGet, "/", nil),
37+
logger,
38+
"test",
39+
)
40+
41+
h := func(_ http.ResponseWriter, _ *http.Request) *HTTPError {
42+
return nil
43+
}
44+
45+
HandleError(h(w, r), w, r)
46+
47+
assert.Empty(t, loggerOutput.AllEntries())
48+
assert.Empty(t, w.Header())
49+
}
50+
3151
func TestHandleError_StandardError(t *testing.T) {
3252
logger, loggerOutput := test.NewNullLogger()
3353
w, r, _ := tracing.NewTracer(
@@ -39,7 +59,7 @@ func TestHandleError_StandardError(t *testing.T) {
3959

4060
HandleError(errors.New("random error"), w, r)
4161

42-
require.Len(t, loggerOutput.AllEntries(), 1)
62+
require.Len(t, loggerOutput.AllEntries(), 1)
4363
assert.Equal(t, "Unhandled server error: random error", loggerOutput.AllEntries()[0].Message)
4464
assert.Empty(t, w.Header())
4565
}
@@ -55,9 +75,9 @@ func TestHandleError_HTTPError(t *testing.T) {
5575
)
5676

5777
httpErr := &HTTPError{
58-
Code: http.StatusInternalServerError,
59-
Message: http.StatusText(http.StatusInternalServerError),
60-
InternalError: errors.New("random error"),
78+
Code: http.StatusInternalServerError,
79+
Message: http.StatusText(http.StatusInternalServerError),
80+
InternalError: errors.New("random error"),
6181
InternalMessage: "Something unexpected happened",
6282
}
6383

@@ -70,7 +90,6 @@ func TestHandleError_HTTPError(t *testing.T) {
7090
expectedBody := fmt.Sprintf(`{"code":500,"msg":"Internal Server Error","json":null,"error_id":"%s"}`, tracing.RequestID(r))
7191
assert.Equal(t, expectedBody, string(b))
7292

73-
require.Len(t, loggerOutput.AllEntries(), 1)
93+
require.Len(t, loggerOutput.AllEntries(), 1)
7494
assert.Equal(t, httpErr.InternalMessage, loggerOutput.AllEntries()[0].Message)
7595
}
76-

router/middleware.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,11 @@ func HealthCheck(route string, f APIHandler) Middleware {
9090
w.WriteHeader(http.StatusOK)
9191
return
9292
}
93-
HandleError(f(w, r), w, r)
93+
94+
if err := f(w, r); err != nil {
95+
HandleError(err, w, r)
96+
}
97+
9498
return
9599
}
96100
next.ServeHTTP(w, r)

0 commit comments

Comments
 (0)