Skip to content

Commit

Permalink
optimize error (apache#395)
Browse files Browse the repository at this point in the history
optimize error
  • Loading branch information
mooleetzi authored and georgehao committed May 7, 2023
1 parent fc0f5d7 commit e603ff9
Show file tree
Hide file tree
Showing 43 changed files with 147 additions and 274 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.18
require (
dubbo.apache.org/dubbo-go/v3 v3.0.3-rc2
github.com/DATA-DOG/go-sqlmock v1.5.0
github.com/agiledragon/gomonkey v2.0.2+incompatible
github.com/agiledragon/gomonkey/v2 v2.2.0
github.com/apache/dubbo-getty v1.4.9-0.20220825024508-3da63c3257fa
github.com/arana-db/parser v0.2.5
github.com/dubbogo/gost v1.12.6-0.20220824084206-300e27e9e524
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ github.com/afex/hystrix-go v0.0.0-20180502004556-fa1af6a1f4f5 h1:rFw4nCn9iMW+Vaj
github.com/afex/hystrix-go v0.0.0-20180502004556-fa1af6a1f4f5/go.mod h1:SkGFH1ia65gfNATL8TAiHDNxPzPdmEL5uirI2Uyuz6c=
github.com/agiledragon/gomonkey v2.0.2+incompatible h1:eXKi9/piiC3cjJD1658mEE2o3NjkJ5vDLgYjCQu0Xlw=
github.com/agiledragon/gomonkey v2.0.2+incompatible/go.mod h1:2NGfXu1a80LLr2cmWXGBDaHEjb1idR6+FVlX5T3D9hw=
github.com/agiledragon/gomonkey/v2 v2.2.0 h1:QJWqpdEhGV/JJy70sZ/LDnhbSlMrqHAWHcNOjz1kyuI=
github.com/agiledragon/gomonkey/v2 v2.2.0/go.mod h1:ap1AmDzcVOAz1YpeJ3TCzIgstoaWLA6jbbgxfB4w2iY=
github.com/ajstarks/svgo v0.0.0-20180226025133-644b8db467af/go.mod h1:K08gAheRH3/J6wwsYMMT4xOr94bZjxIelGM0+d/wbFw=
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
Expand Down
Empty file modified goimports.sh
100644 → 100755
Empty file.
5 changes: 3 additions & 2 deletions pkg/datasource/sql/at.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"github.com/seata/seata-go/pkg/datasource/sql/undo"
"github.com/seata/seata-go/pkg/protocol/branch"
"github.com/seata/seata-go/pkg/rm"
serr "github.com/seata/seata-go/pkg/util/errors"
)

func InitAT(cfg undo.Config, asyncCfg AsyncWorkerConfig) {
Expand Down Expand Up @@ -87,12 +88,12 @@ func (a *ATSourceManager) BranchRollback(ctx context.Context, branchResource rm.
}

if err := undoMgr.RunUndo(ctx, branchResource.Xid, branchResource.BranchId, dbResource.db, dbResource.dbName); err != nil {
transErr, ok := err.(*types.TransactionError)
transErr, ok := err.(*serr.SeataError)
if !ok {
return branch.BranchStatusPhaseoneFailed, err
}

if transErr.Code() == types.ErrorCodeBranchRollbackFailedUnretriable {
if transErr.Code == serr.TransactionErrorCodeBranchRollbackFailedUnretriable {
return branch.BranchStatusPhasetwoRollbackFailedUnretryable, nil
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/datasource/sql/datasource/base/meta_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ package base
import (
"context"
"database/sql"
"errors"
"fmt"
"sync"
"time"

Expand Down Expand Up @@ -154,7 +154,7 @@ func (c *BaseTableMetaCache) GetTableMeta(ctx context.Context, dbName, tableName
return *meta, nil
}

return types.TableMeta{}, errors.New("not found table metadata")
return types.TableMeta{}, fmt.Errorf("not found table metadata")
}

v.lastAccess = time.Now()
Expand Down
4 changes: 2 additions & 2 deletions pkg/datasource/sql/datasource/datasource_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ package datasource
import (
"context"
"database/sql"
"errors"
"fmt"
"sync"

"github.com/seata/seata-go/pkg/datasource/sql/types"
Expand Down Expand Up @@ -120,7 +120,7 @@ func (dm *BasicSourceManager) RegisterResource(resource rm.Resource) error {

// Unregister a model.Resource from the model.Resource Manager
func (dm *BasicSourceManager) UnregisterResource(resource rm.Resource) error {
return errors.New("unsupport unregister resource")
return fmt.Errorf("unsupport unregister resource")
}

// Get all resources managed by this manager
Expand Down
5 changes: 2 additions & 3 deletions pkg/datasource/sql/datasource/mysql/meta_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,10 @@ package mysql
import (
"context"
"database/sql"
"fmt"
"sync"
"time"

"github.com/pkg/errors"

"github.com/seata/seata-go/pkg/datasource/sql/datasource/base"
"github.com/seata/seata-go/pkg/datasource/sql/types"
)
Expand Down Expand Up @@ -56,7 +55,7 @@ func (c *TableMetaCache) Init(ctx context.Context, conn *sql.DB) error {
// GetTableMeta get table info from cache or information schema
func (c *TableMetaCache) GetTableMeta(ctx context.Context, dbName, tableName string) (*types.TableMeta, error) {
if tableName == "" {
return nil, errors.New("table name is empty")
return nil, fmt.Errorf("table name is empty")
}

conn, err := c.db.Conn(ctx)
Expand Down
5 changes: 3 additions & 2 deletions pkg/datasource/sql/datasource/mysql/trigger.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package mysql
import (
"context"
"database/sql"
"fmt"
"strings"

"github.com/pkg/errors"
Expand Down Expand Up @@ -70,7 +71,7 @@ func (m *mysqlTrigger) LoadOne(ctx context.Context, dbName string, tableName str
}
}
if len(tableMeta.Indexs) == 0 {
return nil, errors.Errorf("Could not found any index in the table: %s", tableName)
return nil, fmt.Errorf("could not found any index in the table: %s", tableName)
}

return &tableMeta, nil
Expand Down Expand Up @@ -144,7 +145,7 @@ func (m *mysqlTrigger) getColumnMetas(ctx context.Context, dbName string, table
}

if len(columnMetas) == 0 {
return nil, errors.New("can't find column")
return nil, fmt.Errorf("can't find column")
}

return columnMetas, nil
Expand Down
2 changes: 1 addition & 1 deletion pkg/datasource/sql/exec/at/insert_executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
"reflect"
"testing"

"github.com/agiledragon/gomonkey"
"github.com/agiledragon/gomonkey/v2"
"github.com/arana-db/parser/ast"
"github.com/arana-db/parser/model"
"github.com/arana-db/parser/test_driver"
Expand Down
3 changes: 1 addition & 2 deletions pkg/datasource/sql/exec/at/update_executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ import (
"reflect"
"testing"

"github.com/agiledragon/gomonkey"
_ "github.com/arana-db/parser/test_driver"
"github.com/agiledragon/gomonkey/v2"
"github.com/seata/seata-go/pkg/datasource/sql/datasource"
"github.com/seata/seata-go/pkg/datasource/sql/datasource/mysql"
"github.com/seata/seata-go/pkg/datasource/sql/exec"
Expand Down
5 changes: 2 additions & 3 deletions pkg/datasource/sql/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,9 @@ package sql
import (
"context"
"database/sql/driver"
"fmt"
"sync"

"github.com/pkg/errors"

"github.com/seata/seata-go/pkg/datasource/sql/datasource"
"github.com/seata/seata-go/pkg/protocol/branch"
"github.com/seata/seata-go/pkg/rm"
Expand Down Expand Up @@ -183,7 +182,7 @@ func (tx *Tx) report(success bool) error {
}
dataSourceManager := datasource.GetDataSourceManager(tx.tranCtx.TransactionMode.BranchType())
if dataSourceManager == nil {
return errors.New("get dataSourceManager failed")
return fmt.Errorf("get dataSourceManager failed")
}
retry := REPORT_RETRY_COUNT
for retry > 0 {
Expand Down
57 changes: 0 additions & 57 deletions pkg/datasource/sql/types/error.go

This file was deleted.

7 changes: 3 additions & 4 deletions pkg/datasource/sql/types/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@
package types

import (
"fmt"
"reflect"

"github.com/pkg/errors"
)

// ColumnMeta
Expand Down Expand Up @@ -132,7 +131,7 @@ func (m TableMeta) GetPrimaryKeyType() (int32, error) {
}
}
}
return 0, errors.New("get primary key type error")
return 0, fmt.Errorf("get primary key type error")
}

// GetPrimaryKeyTypeStrMap get all PK type to map
Expand All @@ -146,7 +145,7 @@ func (m TableMeta) GetPrimaryKeyTypeStrMap() (map[string]string, error) {
}
}
if len(pkMap) == 0 {
return nil, errors.New("get primary key type error")
return nil, fmt.Errorf("get primary key type error")
}
return pkMap, nil
}
3 changes: 1 addition & 2 deletions pkg/datasource/sql/undo/base/undo.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import (
"strings"

"github.com/arana-db/parser/mysql"
"github.com/pkg/errors"
"github.com/seata/seata-go/pkg/datasource/sql/datasource"
"github.com/seata/seata-go/pkg/datasource/sql/types"
"github.com/seata/seata-go/pkg/datasource/sql/undo"
Expand Down Expand Up @@ -433,7 +432,7 @@ func (m *BaseUndoLogManager) appendInParam(size int, str *strings.Builder) {
func Int64Slice2Str(values interface{}, sep string) (string, error) {
v, ok := values.([]int64)
if !ok {
return "", errors.New("param type is fault")
return "", fmt.Errorf("param type is fault")
}

var valuesText []string
Expand Down
26 changes: 13 additions & 13 deletions pkg/datasource/sql/undo/builder/mysql_insert_undo_log_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ package builder
import (
"context"
"database/sql/driver"
"fmt"
"strings"

"github.com/arana-db/parser/ast"
"github.com/pkg/errors"
"github.com/seata/seata-go/pkg/datasource/sql/types"
"github.com/seata/seata-go/pkg/datasource/sql/undo"
"github.com/seata/seata-go/pkg/datasource/sql/undo/executor"
Expand Down Expand Up @@ -91,12 +91,12 @@ func (u *MySQLInsertUndoLogBuilder) AfterImage(ctx context.Context, execCtx *typ
func (u *MySQLInsertUndoLogBuilder) buildAfterImageSQL(ctx context.Context, execCtx *types.ExecContext) (string, []driver.Value, error) {
// get all pk value
if execCtx == nil || execCtx.ParseContext == nil || execCtx.ParseContext.InsertStmt == nil {
return "", nil, errors.New("can't found execCtx or ParseContext or InsertStmt")
return "", nil, fmt.Errorf("can't found execCtx or ParseContext or InsertStmt")
}
parseCtx := execCtx.ParseContext
tableName := execCtx.ParseContext.InsertStmt.Table.TableRefs.Left.(*ast.TableSource).Source.(*ast.TableName).Name.O
if execCtx.MetaDataMap == nil {
return "", nil, errors.New("can't found MetaDataMap")
return "", nil, fmt.Errorf("can't found MetaDataMap")
}
meta := execCtx.MetaDataMap[tableName]
pkValuesMap, err := u.getPkValues(execCtx, parseCtx, meta)
Expand All @@ -106,15 +106,15 @@ func (u *MySQLInsertUndoLogBuilder) buildAfterImageSQL(ctx context.Context, exec

pkColumnNameList := meta.GetPrimaryKeyOnlyName()
if len(pkColumnNameList) == 0 {
return "", nil, errors.New("Pk columnName size is zero")
return "", nil, fmt.Errorf("Pk columnName size is zero")
}

dataTypeMap, err := meta.GetPrimaryKeyTypeStrMap()
if err != nil {
return "", nil, err
}
if len(dataTypeMap) != len(pkColumnNameList) {
return "", nil, errors.New("PK columnName size don't equal PK DataType size")
return "", nil, fmt.Errorf("PK columnName size don't equal PK DataType size")
}
var pkRowImages []types.RowImage

Expand Down Expand Up @@ -272,7 +272,7 @@ func (u *MySQLInsertUndoLogBuilder) parsePkValuesFromStatement(insertStmt *ast.I
}
pkIndexMap := u.getPkIndex(insertStmt, meta)
if pkIndexMap == nil || len(pkIndexMap) == 0 {
return nil, errors.New("pkIndex is not found")
return nil, fmt.Errorf("pkIndex is not found")
}
var pkIndexArray []int
for _, val := range pkIndexMap {
Expand All @@ -281,7 +281,7 @@ func (u *MySQLInsertUndoLogBuilder) parsePkValuesFromStatement(insertStmt *ast.I
}

if insertStmt == nil || len(insertStmt.Lists) == 0 {
return nil, errors.New("parCtx is nil, perhaps InsertStmt is empty")
return nil, fmt.Errorf("parCtx is nil, perhaps InsertStmt is empty")
}

pkValuesMap := make(map[string][]interface{})
Expand Down Expand Up @@ -349,7 +349,7 @@ func (u *MySQLInsertUndoLogBuilder) parsePkValuesFromStatement(insertStmt *ast.I
tmpPkName := pkName
tmpPkIndex := pkIndex
if tmpPkIndex >= len(list) {
return nil, errors.New("pkIndex out of range")
return nil, fmt.Errorf("pkIndex out of range")
}
if node, ok := list[tmpPkIndex].(ast.ValueExpr); ok {
pkValuesMap[tmpPkName] = append(pkValuesMap[tmpPkName], node.GetValue())
Expand Down Expand Up @@ -407,7 +407,7 @@ func (u *MySQLInsertUndoLogBuilder) getPkValuesByAuto(execCtx *types.ExecContext
pkValuesMap := make(map[string][]interface{})
pkMetaMap := metaData.GetPrimaryKeyMap()
if len(pkMetaMap) == 0 {
return nil, errors.New("pk map is empty")
return nil, fmt.Errorf("pk map is empty")
}
var autoColumnName string
for _, columnMeta := range pkMetaMap {
Expand All @@ -418,7 +418,7 @@ func (u *MySQLInsertUndoLogBuilder) getPkValuesByAuto(execCtx *types.ExecContext
}
}
if len(autoColumnName) == 0 {
return nil, errors.New("auto increment column not exist")
return nil, fmt.Errorf("auto increment column not exist")
}

updateCount, err := u.InsertResult.GetResult().RowsAffected()
Expand Down Expand Up @@ -485,12 +485,12 @@ func (u *MySQLInsertUndoLogBuilder) autoGeneratePks(execCtx *types.ExecContext,
step = curStepInt
}
} else {
return nil, errors.New("query is empty")
return nil, fmt.Errorf("query is empty")
}
}

if step == 0 {
return nil, errors.New("get increment step error")
return nil, fmt.Errorf("get increment step error")
}

var pkValues []interface{}
Expand Down Expand Up @@ -542,7 +542,7 @@ func getInsertRows(insertStmt *ast.InsertStmt, pkIndexArray []int) ([][]interfac
} else {
for _, index := range pkIndexArray {
if index == i {
return nil, errors.Errorf("Unknown SQLExpr:%v", node)
return nil, fmt.Errorf("Unknown SQLExpr:%v", node)
}
}
row = append(row, ast.DefaultExpr{})
Expand Down
Loading

0 comments on commit e603ff9

Please sign in to comment.