Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

http_*: add log for http api and refine the err handle logic (#2997) #3307

Merged
Merged
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
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