Skip to content

Commit

Permalink
http_*: add log for http api and refine the err handle logic (pingcap…
Browse files Browse the repository at this point in the history
  • Loading branch information
ti-chi-bot authored and 3AceShowHand committed Jan 13, 2022
1 parent b58d176 commit 84a4d26
Show file tree
Hide file tree
Showing 11 changed files with 203 additions and 123 deletions.
51 changes: 51 additions & 0 deletions cdc/capture/http_errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Copyright 2021 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.

package capture

import (
"strings"

"github.com/pingcap/errors"
cerror "github.com/pingcap/tiflow/pkg/errors"
)

// 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.ErrFilterRuleInvalid, cerror.ErrChangefeedUpdateRefused, cerror.ErrMySQLConnectionError,
cerror.ErrMySQLInvalidConfig,
}

// IsHTTPBadRequestError check if a error is a http bad request error
func IsHTTPBadRequestError(err error) bool {
if err == nil {
return false
}
for _, e := range httpBadRequestError {
if e.Equal(err) {
return true
}

rfcCode, ok := cerror.RFCCode(err)
if ok && e.RFCCode() == rfcCode {
return true
}

if strings.Contains(err.Error(), string(e.RFCCode())) {
return true
}
}
return false
}
33 changes: 33 additions & 0 deletions cdc/capture/http_errors_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright 2021 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.

package capture

import (
"testing"

"github.com/pingcap/errors"
cerror "github.com/pingcap/tiflow/pkg/errors"
"github.com/stretchr/testify/require"
)

func TestIsHTTPBadRequestError(t *testing.T) {
err := cerror.ErrAPIInvalidParam.GenWithStack("aa")
require.Equal(t, true, IsHTTPBadRequestError(err))
err = cerror.ErrAPIInvalidParam.Wrap(errors.New("aa"))
require.Equal(t, true, IsHTTPBadRequestError(err))
err = cerror.ErrPDEtcdAPIError.GenWithStack("aa")
require.Equal(t, false, IsHTTPBadRequestError(err))
err = nil
require.Equal(t, false, IsHTTPBadRequestError(err))
}
Loading

0 comments on commit 84a4d26

Please sign in to comment.