@@ -54,6 +54,26 @@ func TestHandleError_ErrorIsNilPointerToTypeHTTPError(t *testing.T) {
54
54
assert .Empty (t , w .Header ())
55
55
}
56
56
57
+ func TestHandleError_ErrorIsNilInterface (t * testing.T ) {
58
+ logger , loggerOutput := test .NewNullLogger ()
59
+ w , r , _ := tracing .NewTracer (
60
+ httptest .NewRecorder (),
61
+ httptest .NewRequest (http .MethodGet , "/" , nil ),
62
+ logger ,
63
+ "test" ,
64
+ "test" ,
65
+ )
66
+
67
+ h := func (_ http.ResponseWriter , _ * http.Request ) error {
68
+ return nil
69
+ }
70
+
71
+ HandleError (h (w , r ), w , r )
72
+
73
+ assert .Empty (t , loggerOutput .AllEntries ())
74
+ assert .Empty (t , w .Header ())
75
+ }
76
+
57
77
func TestHandleError_StandardError (t * testing.T ) {
58
78
logger , loggerOutput := test .NewNullLogger ()
59
79
w , r , _ := tracing .NewTracer (
@@ -99,7 +119,33 @@ func TestHandleError_HTTPError(t *testing.T) {
99
119
assert .Equal (t , expectedBody , string (b ))
100
120
101
121
require .Len (t , loggerOutput .AllEntries (), 1 )
102
- assert .Equal (t , httpErr .InternalMessage , loggerOutput .AllEntries ()[0 ].Message )
122
+ assert .Equal (t , "internal server error: " + httpErr .InternalMessage , loggerOutput .AllEntries ()[0 ].Message )
123
+ }
124
+
125
+ func TestHandleError_NoLogForNormalErrors (t * testing.T ) {
126
+ logger , loggerOutput := test .NewNullLogger ()
127
+ recorder := httptest .NewRecorder ()
128
+ w , r , _ := tracing .NewTracer (
129
+ recorder ,
130
+ httptest .NewRequest (http .MethodGet , "/" , nil ),
131
+ logger ,
132
+ "test" ,
133
+ "test" ,
134
+ )
135
+
136
+ httpErr := BadRequestError ("not found yo." )
137
+
138
+ HandleError (httpErr , w , r )
139
+
140
+ resp := recorder .Result ()
141
+ b , err := ioutil .ReadAll (resp .Body )
142
+ require .NoError (t , err )
143
+
144
+ expectedBody := fmt .Sprintf (`{"code":400,"msg":"not found yo.","error_id":"%s"}` , tracing .GetRequestID (r ))
145
+ assert .Equal (t , expectedBody , string (b ))
146
+
147
+ // we shouldn't log anything, this is a normal error
148
+ require .Len (t , loggerOutput .AllEntries (), 0 )
103
149
}
104
150
105
151
type OtherError struct {
0 commit comments