diff --git a/errors.toml b/errors.toml index 77e74567dae..8726dc63a35 100755 --- a/errors.toml +++ b/errors.toml @@ -143,7 +143,7 @@ changefeed update error: %s ["CDC:ErrCheckClusterVersionFromPD"] error = ''' -failed to request PD +failed to request PD %s, please try again later ''' ["CDC:ErrCheckDataDirSatisfied"] diff --git a/pkg/errors/errors.go b/pkg/errors/errors.go index 6b97fb1f893..5acab118024 100644 --- a/pkg/errors/errors.go +++ b/pkg/errors/errors.go @@ -117,7 +117,7 @@ var ( // utilities related errors ErrToTLSConfigFailed = errors.Normalize("generate tls config failed", errors.RFCCodeText("CDC:ErrToTLSConfigFailed")) - ErrCheckClusterVersionFromPD = errors.Normalize("failed to request PD", errors.RFCCodeText("CDC:ErrCheckClusterVersionFromPD")) + ErrCheckClusterVersionFromPD = errors.Normalize("failed to request PD %s, please try again later", errors.RFCCodeText("CDC:ErrCheckClusterVersionFromPD")) ErrNewSemVersion = errors.Normalize("create sem version", errors.RFCCodeText("CDC:ErrNewSemVersion")) ErrCheckDirWritable = errors.Normalize("check dir writable failed", errors.RFCCodeText("CDC:ErrCheckDirWritable")) ErrCheckDirReadable = errors.Normalize("check dir readable failed", errors.RFCCodeText("CDC:ErrCheckDirReadable")) diff --git a/pkg/version/check.go b/pkg/version/check.go index ccc5d064fb0..436e6306927 100644 --- a/pkg/version/check.go +++ b/pkg/version/check.go @@ -110,28 +110,29 @@ func CheckPDVersion(ctx context.Context, pdAddr string, credential *security.Cre req, err := http.NewRequestWithContext( ctx, http.MethodGet, fmt.Sprintf("%s/pd/api/v1/version", pdAddr), nil) if err != nil { - return cerror.WrapError(cerror.ErrCheckClusterVersionFromPD, err) + return cerror.ErrCheckClusterVersionFromPD.GenWithStackByArgs(err) } resp, err := httpClient.Do(req) if err != nil { - return cerror.WrapError(cerror.ErrCheckClusterVersionFromPD, err) + return cerror.ErrCheckClusterVersionFromPD.GenWithStackByArgs(err) } defer resp.Body.Close() - if resp.StatusCode < 200 || resp.StatusCode >= 300 { - arg := fmt.Sprintf("response status: %s", resp.Status) - return cerror.ErrCheckClusterVersionFromPD.GenWithStackByArgs(arg) - } - content, err := io.ReadAll(resp.Body) - if err != nil { - return cerror.WrapError(cerror.ErrCheckClusterVersionFromPD, err) + if err != nil || resp.StatusCode < 200 || resp.StatusCode >= 300 { + var arg string + if err != nil { + arg = fmt.Sprintf("%s %s %s", resp.Status, content, err) + } else { + arg = fmt.Sprintf("%s %s", resp.Status, content) + } + return cerror.ErrCheckClusterVersionFromPD.GenWithStackByArgs(arg) } err = json.Unmarshal(content, &pdVer) if err != nil { - return cerror.WrapError(cerror.ErrCheckClusterVersionFromPD, err) + return cerror.ErrCheckClusterVersionFromPD.GenWithStackByArgs(err) } ver, err := semver.NewVersion(removeVAndHash(pdVer.Version)) diff --git a/pkg/version/check_test.go b/pkg/version/check_test.go index 38118c49ca2..99878761e32 100644 --- a/pkg/version/check_test.go +++ b/pkg/version/check_test.go @@ -17,6 +17,7 @@ import ( "context" "fmt" "net/http" + "net/http/httptest" "net/url" "testing" "time" @@ -171,7 +172,7 @@ func TestCheckClusterVersion(t *testing.T) { } err := CheckClusterVersion(context.Background(), &mock, pdAddrs, nil, false) - require.Regexp(t, ".*response status: .*", err) + require.Regexp(t, ".*400 Bad Request.*", err) } } @@ -361,3 +362,20 @@ func TestCheckTiCDCClusterVersion(t *testing.T) { } } } + +func TestCheckPDVersionError(t *testing.T) { + t.Parallel() + + var resp func(w http.ResponseWriter, r *http.Request) + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + resp(w, r) + })) + defer ts.Close() + + resp = func(w http.ResponseWriter, _ *http.Request) { + w.WriteHeader(http.StatusInternalServerError) + } + require.Contains(t, CheckPDVersion(context.TODO(), ts.URL, nil).Error(), + "[CDC:ErrCheckClusterVersionFromPD]failed to request PD 500 Internal Server Error , please try again later", + ) +} diff --git a/tools/check/check-errdoc.sh b/tools/check/check-errdoc.sh index 3ebf7269eca..5d77180a65f 100755 --- a/tools/check/check-errdoc.sh +++ b/tools/check/check-errdoc.sh @@ -17,5 +17,5 @@ set -euo pipefail cd -P . cp errors.toml /tmp/errors.toml.before -./tools/bin/errdoc-gen --source . --module github.com/pingcap/tiflow --output errors.toml --ignore proto,dm +./tools/bin/errdoc-gen --source . --module github.com/pingcap/tiflow --output errors.toml --ignore proto,dm,deployments diff -q errors.toml /tmp/errors.toml.before