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

Function TestErrorHandlingWithInternalHttpError

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

Source from the content-addressed store, hash-verified

414}
415
416func TestErrorHandlingWithInternalHttpError(t *testing.T) {
417 t.Parallel()
418
419 logBuffer := logtest.NewDefaultTestLogBuffer()
420 logger, err := log.NewDefaultLoggerFactory().Create(
421 log.WithOutputWriter(logBuffer),
422 )
423 assert.NoError(t, err)
424
425 httpServer := echo.New()
426 httpServer.Logger = httpserver.NewEchoLogger(logger)
427 httpServer.HTTPErrorHandler = httpserver.NewJsonErrorHandler(false, false).Handle()
428
429 httpServer.GET("/test", func(c echo.Context) error {
430 internalError := echo.NewHTTPError(http.StatusInternalServerError, "internal error")
431
432 return &echo.HTTPError{
433 Code: http.StatusInternalServerError,
434 Message: internalError.Error(),
435 Internal: internalError,
436 }
437 })
438
439 req := httptest.NewRequest(http.MethodGet, "/test", nil)
440 req = req.WithContext(logger.WithContext(context.Background()))
441 rec := httptest.NewRecorder()
442 httpServer.ServeHTTP(rec, req)
443
444 assert.Equal(t, http.StatusInternalServerError, rec.Code)
445 assert.Contains(t, rec.Body.String(), `{"message":"internal error"}`)
446
447 logtest.AssertContainLogRecord(t, logBuffer, map[string]interface{}{
448 "level": "error",
449 "error": "internal error",
450 "message": "error handler",
451 })
452}
453
454func TestErrorHandlingWithInternalHttpErrorWithStack(t *testing.T) {
455 t.Parallel()

Callers

nothing calls this directly

Calls 5

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

Tested by

no test coverage detected