| 179 | } |
| 180 | |
| 181 | func TestErrorHandlingWithAlreadyCommittedResponse(t *testing.T) { |
| 182 | t.Parallel() |
| 183 | |
| 184 | logBuffer := logtest.NewDefaultTestLogBuffer() |
| 185 | logger, err := log.NewDefaultLoggerFactory().Create( |
| 186 | log.WithOutputWriter(logBuffer), |
| 187 | ) |
| 188 | assert.NoError(t, err) |
| 189 | |
| 190 | httpServer := echo.New() |
| 191 | httpServer.Logger = httpserver.NewEchoLogger(logger) |
| 192 | httpServer.HTTPErrorHandler = httpserver.NewJsonErrorHandler(false, false).Handle() |
| 193 | |
| 194 | httpServer.GET("/test", func(c echo.Context) error { |
| 195 | err := fmt.Errorf("custom error") |
| 196 | c.Error(err) |
| 197 | |
| 198 | return err |
| 199 | }) |
| 200 | |
| 201 | req := httptest.NewRequest(http.MethodGet, "/test", nil) |
| 202 | req = req.WithContext(logger.WithContext(context.Background())) |
| 203 | rec := httptest.NewRecorder() |
| 204 | httpServer.ServeHTTP(rec, req) |
| 205 | |
| 206 | assert.Equal(t, http.StatusInternalServerError, rec.Code) |
| 207 | assert.Contains(t, rec.Body.String(), `{"message":"custom error"}`) |
| 208 | |
| 209 | logtest.AssertContainLogRecord(t, logBuffer, map[string]interface{}{ |
| 210 | "level": "error", |
| 211 | "error": "custom error", |
| 212 | "message": "error handler", |
| 213 | }) |
| 214 | } |
| 215 | |
| 216 | func TestErrorHandlingWithHttpError(t *testing.T) { |
| 217 | t.Parallel() |