From a6367fd96b836d4fcee14e84d71fd8fa63911f7e Mon Sep 17 00:00:00 2001 From: yonwoo9 <50109209+yonwoo9@users.noreply.github.com> Date: Thu, 27 Jun 2024 17:47:10 +0800 Subject: [PATCH] fit:replace ctx err equal check as error.is (#5588) --- backend/utils/mysql/client.go | 3 ++- backend/utils/mysql/client/local.go | 4 ++-- backend/utils/mysql/client/remote.go | 4 ++-- backend/utils/postgresql/client/local.go | 4 ++-- backend/utils/postgresql/client/remote.go | 6 +++--- 5 files changed, 11 insertions(+), 10 deletions(-) diff --git a/backend/utils/mysql/client.go b/backend/utils/mysql/client.go index d61cb7783..364d0ea3e 100644 --- a/backend/utils/mysql/client.go +++ b/backend/utils/mysql/client.go @@ -3,6 +3,7 @@ package mysql import ( "context" "database/sql" + "errors" "fmt" "strings" "time" @@ -54,7 +55,7 @@ func NewMysqlClient(conn client.DBInfo) (MysqlClient, error) { global.LOG.Errorf("test mysql conn failed, err: %v", err) return nil, err } - if ctx.Err() == context.DeadlineExceeded { + if errors.Is(ctx.Err(), context.DeadlineExceeded) { return nil, buserr.New(constant.ErrExecTimeOut) } diff --git a/backend/utils/mysql/client/local.go b/backend/utils/mysql/client/local.go index b79c73616..44a7693da 100644 --- a/backend/utils/mysql/client/local.go +++ b/backend/utils/mysql/client/local.go @@ -350,7 +350,7 @@ func (r *Local) ExecSQL(command string, timeout uint) error { defer cancel() cmd := exec.CommandContext(ctx, "docker", itemCommand...) stdout, err := cmd.CombinedOutput() - if ctx.Err() == context.DeadlineExceeded { + if errors.Is(ctx.Err(), context.DeadlineExceeded) { return buserr.New(constant.ErrExecTimeOut) } stdStr := strings.ReplaceAll(string(stdout), "mysql: [Warning] Using a password on the command line interface can be insecure.\n", "") @@ -367,7 +367,7 @@ func (r *Local) ExecSQLForRows(command string, timeout uint) ([]string, error) { defer cancel() cmd := exec.CommandContext(ctx, "docker", itemCommand...) stdout, err := cmd.CombinedOutput() - if ctx.Err() == context.DeadlineExceeded { + if errors.Is(ctx.Err(), context.DeadlineExceeded) { return nil, buserr.New(constant.ErrExecTimeOut) } stdStr := strings.ReplaceAll(string(stdout), "mysql: [Warning] Using a password on the command line interface can be insecure.\n", "") diff --git a/backend/utils/mysql/client/remote.go b/backend/utils/mysql/client/remote.go index a1e4476b5..48a814e9e 100644 --- a/backend/utils/mysql/client/remote.go +++ b/backend/utils/mysql/client/remote.go @@ -387,7 +387,7 @@ func (r *Remote) ExecSQL(command string, timeout uint) error { if _, err := r.Client.ExecContext(ctx, command); err != nil { return err } - if ctx.Err() == context.DeadlineExceeded { + if errors.Is(ctx.Err(), context.DeadlineExceeded) { return buserr.New(constant.ErrExecTimeOut) } @@ -402,7 +402,7 @@ func (r *Remote) ExecSQLForHosts(timeout uint) ([]string, error) { if err != nil { return nil, err } - if ctx.Err() == context.DeadlineExceeded { + if errors.Is(ctx.Err(), context.DeadlineExceeded) { return nil, buserr.New(constant.ErrExecTimeOut) } var rows []string diff --git a/backend/utils/postgresql/client/local.go b/backend/utils/postgresql/client/local.go index 91e7cb699..b36fe6608 100644 --- a/backend/utils/postgresql/client/local.go +++ b/backend/utils/postgresql/client/local.go @@ -204,7 +204,7 @@ func (r *Local) ExecSQL(command string, timeout uint) error { defer cancel() cmd := exec.CommandContext(ctx, "docker", itemCommand...) stdout, err := cmd.CombinedOutput() - if ctx.Err() == context.DeadlineExceeded { + if errors.Is(ctx.Err(), context.DeadlineExceeded) { return buserr.New(constant.ErrExecTimeOut) } if err != nil || strings.HasPrefix(string(stdout), "ERROR ") { @@ -220,7 +220,7 @@ func (r *Local) ExecSQLForRows(command string, timeout uint) ([]string, error) { defer cancel() cmd := exec.CommandContext(ctx, "docker", itemCommand...) stdout, err := cmd.CombinedOutput() - if ctx.Err() == context.DeadlineExceeded { + if errors.Is(ctx.Err(), context.DeadlineExceeded) { return nil, buserr.New(constant.ErrExecTimeOut) } if err != nil || strings.HasPrefix(string(stdout), "ERROR ") { diff --git a/backend/utils/postgresql/client/remote.go b/backend/utils/postgresql/client/remote.go index 0b3b11c67..b4a954181 100644 --- a/backend/utils/postgresql/client/remote.go +++ b/backend/utils/postgresql/client/remote.go @@ -223,7 +223,7 @@ func (r *Remote) SyncDB() ([]SyncDBInfo, error) { } datas = append(datas, SyncDBInfo{Name: dbName, From: r.From, PostgresqlName: r.Database}) } - if ctx.Err() == context.DeadlineExceeded { + if errors.Is(ctx.Err(), context.DeadlineExceeded) { return nil, buserr.New(constant.ErrExecTimeOut) } return datas, nil @@ -240,7 +240,7 @@ func (r *Remote) ExecSQL(command string, timeout uint) error { if _, err := r.Client.ExecContext(ctx, command); err != nil { return err } - if ctx.Err() == context.DeadlineExceeded { + if errors.Is(ctx.Err(), context.DeadlineExceeded) { return buserr.New(constant.ErrExecTimeOut) } @@ -300,7 +300,7 @@ func loadImageTag() (string, error) { ctx, cancel := context.WithTimeout(context.Background(), 3*time.Minute) defer cancel() if _, err := client.ImagePull(ctx, itemTag, image.PullOptions{}); err != nil { - if ctx.Err() == context.DeadlineExceeded { + if errors.Is(ctx.Err(), context.DeadlineExceeded) { return itemTag, buserr.New(constant.ErrPgImagePull) } global.LOG.Errorf("image %s pull failed, err: %v", itemTag, err)