MCPcopy Create free account
hub / github.com/ankorstore/yokai / TestErrorHandlingWithAlreadyCommittedResponse

Function TestErrorHandlingWithAlreadyCommittedResponse

httpserver/error_test.go:181–214  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

179}
180
181func 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
216func TestErrorHandlingWithHttpError(t *testing.T) {
217 t.Parallel()

Callers

nothing calls this directly

Calls 6

WithContextMethod · 0.80
CreateMethod · 0.65
HandleMethod · 0.65
ErrorfMethod · 0.45
ErrorMethod · 0.45
StringMethod · 0.45

Tested by

no test coverage detected