refactor: standardize API error handling (#1635)

This commit is contained in:
Elias Schneider
2026-08-02 23:36:06 +02:00
committed by GitHub
parent 281bea54d3
commit 7a4d0dd275
108 changed files with 3163 additions and 1629 deletions
@@ -0,0 +1,64 @@
package httpserver
import (
"context"
"errors"
"net/http"
"net/http/httptest"
"testing"
"github.com/gin-gonic/gin"
"github.com/stretchr/testify/require"
)
func TestHandleAttachesAndAbortsOnError(t *testing.T) {
gin.SetMode(gin.TestMode)
c, _ := gin.CreateTestContext(httptest.NewRecorder())
expected := errors.New("request failed")
Handle(func(*gin.Context) error {
return expected
})(c)
require.True(t, c.IsAborted())
require.Len(t, c.Errors, 1)
require.ErrorIs(t, c.Errors[0], expected)
}
func TestHandleIgnoresCanceledRequests(t *testing.T) {
gin.SetMode(gin.TestMode)
c, _ := gin.CreateTestContext(httptest.NewRecorder())
Handle(func(*gin.Context) error {
return context.Canceled
})(c)
require.True(t, c.IsAborted())
require.Empty(t, c.Errors)
}
func TestHandleRecoversPanicsWithStack(t *testing.T) {
gin.SetMode(gin.TestMode)
c, _ := gin.CreateTestContext(httptest.NewRecorder())
Handle(func(*gin.Context) error {
panic("private panic details")
})(c)
require.True(t, c.IsAborted())
require.Len(t, c.Errors, 1)
require.ErrorContains(t, c.Errors[0], "panic in HTTP handler (string): private panic details")
require.ErrorContains(t, c.Errors[0], "TestHandleRecoversPanicsWithStack")
}
func TestHandlePreservesAbortHandlerPanic(t *testing.T) {
gin.SetMode(gin.TestMode)
c, _ := gin.CreateTestContext(httptest.NewRecorder())
require.PanicsWithValue(t, http.ErrAbortHandler, func() {
Handle(func(*gin.Context) error {
panic(http.ErrAbortHandler)
})(c)
})
require.Empty(t, c.Errors)
}