Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions api/middleware/middleware_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (

"github.com/gin-gonic/gin"
"github.com/pingcap/kvproto/pkg/keyspacepb"
cerrors "github.com/pingcap/ticdc/pkg/errors"
"github.com/pingcap/ticdc/pkg/node"
"github.com/pingcap/ticdc/pkg/pdutil"
"github.com/pingcap/ticdc/pkg/server"
Expand Down Expand Up @@ -205,6 +206,57 @@ func TestForwardToCoordinator(t *testing.T) {
}
}

func TestErrorHandleMiddleware(t *testing.T) {
gin.SetMode(gin.TestMode)

cases := []struct {
name string
err error
wantStatus int
}{
{
name: "no error returns 200",
err: nil,
wantStatus: http.StatusOK,
},
{
name: "registered bad-request error returns 400",
err: cerrors.ErrAPIInvalidParam.GenWithStackByArgs("bad input"),
wantStatus: http.StatusBadRequest,
},
{
name: "duplicate changefeed returns 400",
err: cerrors.ErrChangeFeedAlreadyExists.GenWithStackByArgs("foo"),
wantStatus: http.StatusBadRequest,
},
{
name: "unclassified error returns 500",
err: errors.New("boom"),
wantStatus: http.StatusInternalServerError,
},
}

for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
router := gin.New()
router.Use(ErrorHandleMiddleware())
router.GET("/test", func(c *gin.Context) {
if tc.err != nil {
_ = c.Error(tc.err)
return
}
c.Status(http.StatusOK)
})

w := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodGet, "/test", nil)
router.ServeHTTP(w, req)

require.Equal(t, tc.wantStatus, w.Code)
})
}
}

// mockServer implements server.Server interface for testing
type mockServer struct {
server.Server
Expand Down
3 changes: 2 additions & 1 deletion pkg/api/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ func NewHTTPError(err error) HTTPError {
// httpBadRequestError is some errors that will cause a BadRequestError in http handler
var httpBadRequestError = []*errors.Error{
cerror.ErrAPIInvalidParam, cerror.ErrSinkURIInvalid, cerror.ErrStartTsBeforeGC,
cerror.ErrChangeFeedNotExists, cerror.ErrTargetTsBeforeStartTs, cerror.ErrTableIneligible,
cerror.ErrChangeFeedNotExists, cerror.ErrChangeFeedAlreadyExists,
cerror.ErrTargetTsBeforeStartTs, cerror.ErrTableIneligible,
cerror.ErrFilterRuleInvalid, cerror.ErrChangefeedUpdateRefused, cerror.ErrMySQLConnectionError,
cerror.ErrMySQLInvalidConfig, cerror.ErrCaptureNotExist, cerror.ErrSchedulerRequestFailed,
cerror.ErrActiveActiveTSOIndexIncompatible,
Expand Down