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

*: using standard error to replace terror (#19425) #19888

Merged
merged 5 commits into from
Sep 9, 2020
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
2 changes: 1 addition & 1 deletion ddl/db_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ func (s *testIntegrationSuite2) TestIssue6101(c *C) {
tk.MustExec("create table t1 (quantity decimal(2) unsigned);")
_, err := tk.Exec("insert into t1 values (500), (-500), (~0), (-1);")
terr := errors.Cause(err).(*terror.Error)
c.Assert(terr.Code(), Equals, terror.ErrCode(errno.ErrWarnDataOutOfRange))
c.Assert(terr.Code(), Equals, errors.ErrCode(errno.ErrWarnDataOutOfRange))
tk.MustExec("drop table t1")

tk.MustExec("set sql_mode=''")
Expand Down
5 changes: 3 additions & 2 deletions domain/domain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/pingcap/failpoint"
"github.com/pingcap/parser/ast"
"github.com/pingcap/parser/model"
"github.com/pingcap/parser/terror"
"github.com/pingcap/tidb/ddl"
"github.com/pingcap/tidb/domain/infosync"
"github.com/pingcap/tidb/errno"
Expand Down Expand Up @@ -458,6 +459,6 @@ func (*testSuite) TestSessionPool(c *C) {
}

func (*testSuite) TestErrorCode(c *C) {
c.Assert(int(ErrInfoSchemaExpired.ToSQLError().Code), Equals, errno.ErrInfoSchemaExpired)
c.Assert(int(ErrInfoSchemaChanged.ToSQLError().Code), Equals, errno.ErrInfoSchemaChanged)
c.Assert(int(terror.ToSQLError(ErrInfoSchemaExpired).Code), Equals, errno.ErrInfoSchemaExpired)
c.Assert(int(terror.ToSQLError(ErrInfoSchemaChanged).Code), Equals, errno.ErrInfoSchemaChanged)
}
22 changes: 11 additions & 11 deletions executor/executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1248,12 +1248,12 @@ func (s *testSuiteP2) TestUnion(c *C) {
err := tk.ExecToErr("select 1 from (select a from t limit 1 union all select a from t limit 1) tmp")
c.Assert(err, NotNil)
terr := errors.Cause(err).(*terror.Error)
c.Assert(terr.Code(), Equals, terror.ErrCode(mysql.ErrWrongUsage))
c.Assert(terr.Code(), Equals, errors.ErrCode(mysql.ErrWrongUsage))

err = tk.ExecToErr("select 1 from (select a from t order by a union all select a from t limit 1) tmp")
c.Assert(err, NotNil)
terr = errors.Cause(err).(*terror.Error)
c.Assert(terr.Code(), Equals, terror.ErrCode(mysql.ErrWrongUsage))
c.Assert(terr.Code(), Equals, errors.ErrCode(mysql.ErrWrongUsage))

_, err = tk.Exec("(select a from t order by a) union all select a from t limit 1 union all select a from t limit 1")
c.Assert(terror.ErrorEqual(err, plannercore.ErrWrongUsage), IsTrue, Commentf("err %v", err))
Expand Down Expand Up @@ -1630,23 +1630,23 @@ func (s *testSuiteP1) TestJSON(c *C) {
_, err = tk.Exec(`create table test_bad_json(a json default '{}')`)
c.Assert(err, NotNil)
terr = errors.Cause(err).(*terror.Error)
c.Assert(terr.Code(), Equals, terror.ErrCode(mysql.ErrBlobCantHaveDefault))
c.Assert(terr.Code(), Equals, errors.ErrCode(mysql.ErrBlobCantHaveDefault))

_, err = tk.Exec(`create table test_bad_json(a blob default 'hello')`)
c.Assert(err, NotNil)
terr = errors.Cause(err).(*terror.Error)
c.Assert(terr.Code(), Equals, terror.ErrCode(mysql.ErrBlobCantHaveDefault))
c.Assert(terr.Code(), Equals, errors.ErrCode(mysql.ErrBlobCantHaveDefault))

_, err = tk.Exec(`create table test_bad_json(a text default 'world')`)
c.Assert(err, NotNil)
terr = errors.Cause(err).(*terror.Error)
c.Assert(terr.Code(), Equals, terror.ErrCode(mysql.ErrBlobCantHaveDefault))
c.Assert(terr.Code(), Equals, errors.ErrCode(mysql.ErrBlobCantHaveDefault))

// check json fields cannot be used as key.
_, err = tk.Exec(`create table test_bad_json(id int, a json, key (a))`)
c.Assert(err, NotNil)
terr = errors.Cause(err).(*terror.Error)
c.Assert(terr.Code(), Equals, terror.ErrCode(mysql.ErrJSONUsedAsKey))
c.Assert(terr.Code(), Equals, errors.ErrCode(mysql.ErrJSONUsedAsKey))

// check CAST AS JSON.
result = tk.MustQuery(`select CAST('3' AS JSON), CAST('{}' AS JSON), CAST(null AS JSON)`)
Expand Down Expand Up @@ -1744,7 +1744,7 @@ func (s *testSuiteP1) TestGeneratedColumnWrite(c *C) {
if tt.err != 0 {
c.Assert(err, NotNil, Commentf("sql is `%v`", tt.stmt))
terr := errors.Cause(err).(*terror.Error)
c.Assert(terr.Code(), Equals, terror.ErrCode(tt.err), Commentf("sql is %v", tt.stmt))
c.Assert(terr.Code(), Equals, errors.ErrCode(tt.err), Commentf("sql is %v", tt.stmt))
} else {
c.Assert(err, IsNil)
}
Expand Down Expand Up @@ -1915,7 +1915,7 @@ func (s *testSuiteP1) TestGeneratedColumnRead(c *C) {
if tt.err != 0 {
c.Assert(err, NotNil)
terr := errors.Cause(err).(*terror.Error)
c.Assert(terr.Code(), Equals, terror.ErrCode(tt.err))
c.Assert(terr.Code(), Equals, errors.ErrCode(tt.err))
} else {
c.Assert(err, IsNil)
}
Expand Down Expand Up @@ -3283,7 +3283,7 @@ func (s *testSuite) TestContainDotColumn(c *C) {
tk.MustExec("drop table if exists t3")
_, err := tk.Exec("create table t3(s.a char);")
terr := errors.Cause(err).(*terror.Error)
c.Assert(terr.Code(), Equals, terror.ErrCode(mysql.ErrWrongTableName))
c.Assert(terr.Code(), Equals, errors.ErrCode(mysql.ErrWrongTableName))
}

func (s *testSuite) TestCheckIndex(c *C) {
Expand Down Expand Up @@ -4201,7 +4201,7 @@ func (s *testSuiteP2) TestSplitRegion(c *C) {
_, err := tk.Exec(`split table t index idx1 by ("abcd");`)
c.Assert(err, NotNil)
terr := errors.Cause(err).(*terror.Error)
c.Assert(terr.Code(), Equals, terror.ErrCode(mysql.WarnDataTruncated))
c.Assert(terr.Code(), Equals, errors.ErrCode(mysql.WarnDataTruncated))

// Test for split index region.
// Check min value is more than max value.
Expand Down Expand Up @@ -5944,7 +5944,7 @@ func (s *testSplitTable) TestKillTableReader(c *C) {
wg.Add(1)
go func() {
defer wg.Done()
c.Assert(int(errors.Cause(tk.QueryToErr("select * from t")).(*terror.Error).ToSQLError().Code), Equals, int(executor.ErrQueryInterrupted.Code()))
c.Assert(int(terror.ToSQLError(errors.Cause(tk.QueryToErr("select * from t")).(*terror.Error)).Code), Equals, int(executor.ErrQueryInterrupted.Code()))
}()
time.Sleep(1 * time.Second)
atomic.StoreUint32(&tk.Se.GetSessionVars().Killed, 1)
Expand Down
2 changes: 1 addition & 1 deletion executor/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -1338,7 +1338,7 @@ func (e *ShowExec) fetchShowWarnings(errOnly bool) error {
warn := errors.Cause(w.Err)
switch x := warn.(type) {
case *terror.Error:
sqlErr := x.ToSQLError()
sqlErr := terror.ToSQLError(x)
e.appendRow([]interface{}{w.Level, int64(sqlErr.Code), sqlErr.Message})
default:
e.appendRow([]interface{}{w.Level, int64(mysql.ErrUnknown), warn.Error()})
Expand Down
4 changes: 2 additions & 2 deletions executor/trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ func (e *TraceExec) executeChild(ctx context.Context, se sqlexec.SQLExecutor) {
if err != nil {
var errCode uint16
if te, ok := err.(*terror.Error); ok {
errCode = te.ToSQLError().Code
errCode = terror.ToSQLError(te).Code
}
logutil.Eventf(ctx, "execute with error(%d): %s", errCode, err.Error())
} else {
Expand All @@ -161,7 +161,7 @@ func drainRecordSet(ctx context.Context, sctx sessionctx.Context, rs sqlexec.Rec
if err != nil {
var errCode uint16
if te, ok := err.(*terror.Error); ok {
errCode = te.ToSQLError().Code
errCode = terror.ToSQLError(te).Code
}
logutil.Eventf(ctx, "execute with error(%d): %s", errCode, err.Error())
} else {
Expand Down
14 changes: 7 additions & 7 deletions expression/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,7 @@ func (s *testIntegrationSuite2) TestMathBuiltin(c *C) {
_, err = session.GetRows4Test(ctx, tk.Se, rs)
c.Assert(err, NotNil)
terr := errors.Cause(err).(*terror.Error)
c.Assert(terr.Code(), Equals, terror.ErrCode(mysql.ErrDataOutOfRange))
c.Assert(terr.Code(), Equals, errors.ErrCode(mysql.ErrDataOutOfRange))
c.Assert(rs.Close(), IsNil)

//for exp
Expand All @@ -479,7 +479,7 @@ func (s *testIntegrationSuite2) TestMathBuiltin(c *C) {
_, err = session.GetRows4Test(ctx, tk.Se, rs)
c.Assert(err, NotNil)
terr = errors.Cause(err).(*terror.Error)
c.Assert(terr.Code(), Equals, terror.ErrCode(mysql.ErrDataOutOfRange))
c.Assert(terr.Code(), Equals, errors.ErrCode(mysql.ErrDataOutOfRange))
c.Assert(rs.Close(), IsNil)
tk.MustExec("drop table if exists t")
tk.MustExec("create table t(a float)")
Expand All @@ -489,7 +489,7 @@ func (s *testIntegrationSuite2) TestMathBuiltin(c *C) {
_, err = session.GetRows4Test(ctx, tk.Se, rs)
c.Assert(err, NotNil)
terr = errors.Cause(err).(*terror.Error)
c.Assert(terr.Code(), Equals, terror.ErrCode(mysql.ErrDataOutOfRange))
c.Assert(terr.Code(), Equals, errors.ErrCode(mysql.ErrDataOutOfRange))
c.Assert(err.Error(), Equals, "[types:1690]DOUBLE value is out of range in 'exp(test.t.a)'")
c.Assert(rs.Close(), IsNil)

Expand Down Expand Up @@ -529,7 +529,7 @@ func (s *testIntegrationSuite2) TestMathBuiltin(c *C) {
_, err = session.GetRows4Test(ctx, tk.Se, rs)
c.Assert(err, NotNil)
terr = errors.Cause(err).(*terror.Error)
c.Assert(terr.Code(), Equals, terror.ErrCode(mysql.ErrDataOutOfRange))
c.Assert(terr.Code(), Equals, errors.ErrCode(mysql.ErrDataOutOfRange))
c.Assert(rs.Close(), IsNil)

// for round
Expand Down Expand Up @@ -608,7 +608,7 @@ func (s *testIntegrationSuite2) TestMathBuiltin(c *C) {
_, err = session.GetRows4Test(ctx, tk.Se, rs)
c.Assert(err, NotNil)
terr = errors.Cause(err).(*terror.Error)
c.Assert(terr.Code(), Equals, terror.ErrCode(mysql.ErrDataOutOfRange))
c.Assert(terr.Code(), Equals, errors.ErrCode(mysql.ErrDataOutOfRange))
c.Assert(rs.Close(), IsNil)

// for sign
Expand Down Expand Up @@ -1226,7 +1226,7 @@ func (s *testIntegrationSuite2) TestEncryptionBuiltin(c *C) {
_, err = session.GetRows4Test(ctx, tk.Se, rs)
c.Assert(err, NotNil, Commentf("%v", len))
terr := errors.Cause(err).(*terror.Error)
c.Assert(terr.Code(), Equals, terror.ErrCode(mysql.ErrDataOutOfRange), Commentf("%v", len))
c.Assert(terr.Code(), Equals, errors.ErrCode(mysql.ErrDataOutOfRange), Commentf("%v", len))
c.Assert(rs.Close(), IsNil)
}
tk.MustQuery("SELECT RANDOM_BYTES('1');")
Expand Down Expand Up @@ -3691,7 +3691,7 @@ func (s *testIntegrationSuite) TestAggregationBuiltinGroupConcat(c *C) {
tk.MustQuery("show warnings").Check(testutil.RowsWithSep("|", "Warning 1260 Some rows were cut by GROUPCONCAT(test.t.a)"))

_, err := tk.Exec("insert into d select group_concat(a) from t")
c.Assert(errors.Cause(err).(*terror.Error).Code(), Equals, terror.ErrCode(mysql.ErrCutValueGroupConcat))
c.Assert(errors.Cause(err).(*terror.Error).Code(), Equals, errors.ErrCode(mysql.ErrCutValueGroupConcat))

tk.Exec("set sql_mode=''")
tk.MustExec("insert into d select group_concat(a) from t")
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ require (
github.com/pingcap/goleveldb v0.0.0-20191226122134-f82aafb29989
github.com/pingcap/kvproto v0.0.0-20200818080353-7aaed8998596
github.com/pingcap/log v0.0.0-20200828042413-fce0951f1463
github.com/pingcap/parser v0.0.0-20200902091735-5e6adfc24e11
github.com/pingcap/parser v0.0.0-20200908132759-b65348b6244c
github.com/pingcap/sysutil v0.0.0-20200715082929-4c47bcac246a
github.com/pingcap/tidb-tools v4.0.6-0.20200828085514-03575b185007+incompatible
github.com/pingcap/tipb v0.0.0-20200618092958-4fad48b4c8c3
Expand All @@ -50,7 +50,7 @@ require (
go.etcd.io/etcd v0.5.0-alpha.5.0.20191023171146-3cf2f69b5738
go.uber.org/atomic v1.6.0
go.uber.org/automaxprocs v1.2.0
go.uber.org/zap v1.15.0
go.uber.org/zap v1.16.0
golang.org/x/net v0.0.0-20200813134508-3edf25e44fcc
golang.org/x/sync v0.0.0-20200625203802-6e8e738ad208
golang.org/x/sys v0.0.0-20200819171115-d785dc25833f
Expand Down
7 changes: 5 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -461,8 +461,8 @@ github.com/pingcap/parser v0.0.0-20200603032439-c4ecb4508d2f/go.mod h1:9v0Edh8Ib
github.com/pingcap/parser v0.0.0-20200623164729-3a18f1e5dceb/go.mod h1:vQdbJqobJAgFyiRNNtXahpMoGWwPEuWciVEK5A20NS0=
github.com/pingcap/parser v0.0.0-20200803072748-fdf66528323d/go.mod h1:vQdbJqobJAgFyiRNNtXahpMoGWwPEuWciVEK5A20NS0=
github.com/pingcap/parser v0.0.0-20200901062802-475ea5e2e0a7/go.mod h1:vQdbJqobJAgFyiRNNtXahpMoGWwPEuWciVEK5A20NS0=
github.com/pingcap/parser v0.0.0-20200902091735-5e6adfc24e11 h1:l269UdOdRK4NGoldejKY+9Tor7XmHU+XOuS+VQDwcH8=
github.com/pingcap/parser v0.0.0-20200902091735-5e6adfc24e11/go.mod h1:vQdbJqobJAgFyiRNNtXahpMoGWwPEuWciVEK5A20NS0=
github.com/pingcap/parser v0.0.0-20200908132759-b65348b6244c h1:oJn1X+lZwG4LG2DV+73lppFfnfy+3wXUwpoVgtIOQq8=
github.com/pingcap/parser v0.0.0-20200908132759-b65348b6244c/go.mod h1:RlLfMRJwFBSiXd2lUaWdV5pSXtrpyvZM8k5bbZWsheU=
github.com/pingcap/pd/v4 v4.0.0-rc.1.0.20200422143320-428acd53eba2/go.mod h1:s+utZtXDznOiL24VK0qGmtoHjjXNsscJx3m1n8cC56s=
github.com/pingcap/pd/v4 v4.0.0-rc.2.0.20200520083007-2c251bd8f181/go.mod h1:q4HTx/bA8aKBa4S7L+SQKHvjRPXCRV0tA0yRw0qkZSA=
github.com/pingcap/pd/v4 v4.0.5-0.20200817114353-e465cafe8a91 h1:zCOWP+kIzM6ZsXdu2QoM/W6+3vFZj04MYboMP2Obc0E=
Expand Down Expand Up @@ -609,6 +609,7 @@ github.com/ugorji/go/codec v1.1.5-pre/go.mod h1:tULtS6Gy1AE1yCENaw4Vb//HLH5njI2t
github.com/ugorji/go/codec v1.1.7/go.mod h1:Ax+UKWsSmolVDwsd+7N3ZtXu+yMGCf907BLYF3GoBXY=
github.com/unrolled/render v0.0.0-20171102162132-65450fb6b2d3/go.mod h1:tu82oB5W2ykJRVioYsB+IQKcft7ryBr7w12qMBUPyXg=
github.com/urfave/cli v1.20.0/go.mod h1:70zkFmudgCuE/ngEzBv17Jvp/497gISqfk5gWijbERA=
github.com/urfave/cli v1.22.2 h1:gsqYFH8bb9ekPA12kRo0hfjngWQjkJPlN9R0N78BoUo=
github.com/urfave/cli v1.22.2/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0=
github.com/urfave/cli/v2 v2.1.1/go.mod h1:SE9GqnLQmjVa0iPEY0f1w3ygNIYcIJ0OKPMoW2caLfQ=
github.com/urfave/negroni v0.3.0/go.mod h1:Meg73S6kFm/4PpbYdq35yYWoCZ9mS/YSx+lKnmiohz4=
Expand Down Expand Up @@ -657,6 +658,8 @@ go.uber.org/zap v1.13.0/go.mod h1:zwrFLgMcdUuIBviXEYEH1YKNaOBnKXsx2IPda5bBwHM=
go.uber.org/zap v1.14.1/go.mod h1:Mb2vm2krFEG5DV0W9qcHBYFtp/Wku1cvYaqPsS/WYfc=
go.uber.org/zap v1.15.0 h1:ZZCA22JRF2gQE5FoNmhmrf7jeJJ2uhqDUNRYKm8dvmM=
go.uber.org/zap v1.15.0/go.mod h1:Mb2vm2krFEG5DV0W9qcHBYFtp/Wku1cvYaqPsS/WYfc=
go.uber.org/zap v1.16.0 h1:uFRZXykJGK9lLY4HtgSw44DnIcAM+kRBP7x5m+NpAOM=
go.uber.org/zap v1.16.0/go.mod h1:MA8QOfq0BHJwdXa996Y4dYkAqRKB8/1K1QMMZVaNZjQ=
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/crypto v0.0.0-20181203042331-505ab145d0a9/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
Expand Down
2 changes: 1 addition & 1 deletion kv/error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func (s testErrorSuite) TestError(c *C) {
ErrWriteConflictInTiDB,
}
for _, err := range kvErrs {
code := err.ToSQLError().Code
code := terror.ToSQLError(err).Code
c.Assert(code != mysql.ErrUnknown && code == uint16(err.Code()), IsTrue, Commentf("err: %v", err))
}
}
4 changes: 1 addition & 3 deletions metrics/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
package metrics

import (
"strconv"

"github.com/pingcap/errors"
"github.com/pingcap/parser/terror"
"github.com/prometheus/client_golang/prometheus"
Expand Down Expand Up @@ -170,7 +168,7 @@ func ExecuteErrorToLabel(err error) string {
err = errors.Cause(err)
switch x := err.(type) {
case *terror.Error:
return x.Class().String() + ":" + strconv.Itoa(int(x.Code()))
return string(x.RFCCode())
default:
return "unknown"
}
Expand Down
2 changes: 1 addition & 1 deletion planner/core/errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func (s testErrorSuite) TestError(c *C) {
ErrAmbiguous,
}
for _, err := range kvErrs {
code := err.ToSQLError().Code
code := terror.ToSQLError(err).Code
c.Assert(code != mysql.ErrUnknown && code == uint16(err.Code()), IsTrue, Commentf("err: %v", err))
}
}
2 changes: 1 addition & 1 deletion privilege/privileges/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ func (p *MySQLPrivilege) LoadAll(ctx sessionctx.Context) error {
func noSuchTable(err error) bool {
e1 := errors.Cause(err)
if e2, ok := e1.(*terror.Error); ok {
if e2.Code() == terror.ErrCode(mysql.ErrNoSuchTable) {
if terror.ErrCode(e2.Code()) == terror.ErrCode(mysql.ErrNoSuchTable) {
return true
}
}
Expand Down
4 changes: 2 additions & 2 deletions server/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -1008,12 +1008,12 @@ func (cc *clientConn) writeError(ctx context.Context, e error) error {
)
originErr := errors.Cause(e)
if te, ok = originErr.(*terror.Error); ok {
m = te.ToSQLError()
m = terror.ToSQLError(te)
} else {
e := errors.Cause(originErr)
switch y := e.(type) {
case *terror.Error:
m = y.ToSQLError()
m = terror.ToSQLError(y)
default:
m = mysql.NewErrf(mysql.ErrUnknown, "%s", e.Error())
}
Expand Down
2 changes: 1 addition & 1 deletion sessionctx/variable/sysvar_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,6 @@ func (*testSysVarSuite) TestError(c *C) {
ErrUnsupportedIsolationLevel,
}
for _, err := range kvErrs {
c.Assert(err.ToSQLError().Code != mysql.ErrUnknown, IsTrue)
c.Assert(terror.ToSQLError(err).Code != mysql.ErrUnknown, IsTrue)
}
}
4 changes: 2 additions & 2 deletions store/mockstore/mocktikv/cop_handler_dag.go
Original file line number Diff line number Diff line change
Expand Up @@ -814,14 +814,14 @@ func toPBError(err error) *tipb.Error {
perr := new(tipb.Error)
switch x := err.(type) {
case *terror.Error:
sqlErr := x.ToSQLError()
sqlErr := terror.ToSQLError(x)
perr.Code = int32(sqlErr.Code)
perr.Msg = sqlErr.Message
default:
e := errors.Cause(err)
switch y := e.(type) {
case *terror.Error:
tmp := y.ToSQLError()
tmp := terror.ToSQLError(y)
perr.Code = int32(tmp.Code)
perr.Msg = tmp.Message
default:
Expand Down
2 changes: 1 addition & 1 deletion structure/structure_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ func (*testTxStructureSuite) TestError(c *C) {
structure.ErrWriteOnSnapshot,
}
for _, err := range kvErrs {
code := err.ToSQLError().Code
code := terror.ToSQLError(err).Code
c.Assert(code != mysql.ErrUnknown && code == uint16(err.Code()), IsTrue, Commentf("err: %v", err))
}
}
35 changes: 18 additions & 17 deletions table/table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ package table

import (
. "github.com/pingcap/check"
"github.com/pingcap/parser/terror"
mysql "github.com/pingcap/tidb/errno"
)

Expand All @@ -30,21 +31,21 @@ func (t *testTableSuite) TestSlice(c *C) {
}

func (t *testTableSuite) TestErrorCode(c *C) {
c.Assert(int(ErrColumnCantNull.ToSQLError().Code), Equals, mysql.ErrBadNull)
c.Assert(int(ErrUnknownColumn.ToSQLError().Code), Equals, mysql.ErrBadField)
c.Assert(int(errDuplicateColumn.ToSQLError().Code), Equals, mysql.ErrFieldSpecifiedTwice)
c.Assert(int(errGetDefaultFailed.ToSQLError().Code), Equals, mysql.ErrFieldGetDefaultFailed)
c.Assert(int(ErrNoDefaultValue.ToSQLError().Code), Equals, mysql.ErrNoDefaultForField)
c.Assert(int(ErrIndexOutBound.ToSQLError().Code), Equals, mysql.ErrIndexOutBound)
c.Assert(int(ErrUnsupportedOp.ToSQLError().Code), Equals, mysql.ErrUnsupportedOp)
c.Assert(int(ErrRowNotFound.ToSQLError().Code), Equals, mysql.ErrRowNotFound)
c.Assert(int(ErrTableStateCantNone.ToSQLError().Code), Equals, mysql.ErrTableStateCantNone)
c.Assert(int(ErrColumnStateCantNone.ToSQLError().Code), Equals, mysql.ErrColumnStateCantNone)
c.Assert(int(ErrColumnStateNonPublic.ToSQLError().Code), Equals, mysql.ErrColumnStateNonPublic)
c.Assert(int(ErrIndexStateCantNone.ToSQLError().Code), Equals, mysql.ErrIndexStateCantNone)
c.Assert(int(ErrInvalidRecordKey.ToSQLError().Code), Equals, mysql.ErrInvalidRecordKey)
c.Assert(int(ErrTruncatedWrongValueForField.ToSQLError().Code), Equals, mysql.ErrTruncatedWrongValueForField)
c.Assert(int(ErrUnknownPartition.ToSQLError().Code), Equals, mysql.ErrUnknownPartition)
c.Assert(int(ErrNoPartitionForGivenValue.ToSQLError().Code), Equals, mysql.ErrNoPartitionForGivenValue)
c.Assert(int(ErrLockOrActiveTransaction.ToSQLError().Code), Equals, mysql.ErrLockOrActiveTransaction)
c.Assert(int(terror.ToSQLError(ErrColumnCantNull).Code), Equals, mysql.ErrBadNull)
c.Assert(int(terror.ToSQLError(ErrUnknownColumn).Code), Equals, mysql.ErrBadField)
c.Assert(int(terror.ToSQLError(errDuplicateColumn).Code), Equals, mysql.ErrFieldSpecifiedTwice)
c.Assert(int(terror.ToSQLError(errGetDefaultFailed).Code), Equals, mysql.ErrFieldGetDefaultFailed)
c.Assert(int(terror.ToSQLError(ErrNoDefaultValue).Code), Equals, mysql.ErrNoDefaultForField)
c.Assert(int(terror.ToSQLError(ErrIndexOutBound).Code), Equals, mysql.ErrIndexOutBound)
c.Assert(int(terror.ToSQLError(ErrUnsupportedOp).Code), Equals, mysql.ErrUnsupportedOp)
c.Assert(int(terror.ToSQLError(ErrRowNotFound).Code), Equals, mysql.ErrRowNotFound)
c.Assert(int(terror.ToSQLError(ErrTableStateCantNone).Code), Equals, mysql.ErrTableStateCantNone)
c.Assert(int(terror.ToSQLError(ErrColumnStateCantNone).Code), Equals, mysql.ErrColumnStateCantNone)
c.Assert(int(terror.ToSQLError(ErrColumnStateNonPublic).Code), Equals, mysql.ErrColumnStateNonPublic)
c.Assert(int(terror.ToSQLError(ErrIndexStateCantNone).Code), Equals, mysql.ErrIndexStateCantNone)
c.Assert(int(terror.ToSQLError(ErrInvalidRecordKey).Code), Equals, mysql.ErrInvalidRecordKey)
c.Assert(int(terror.ToSQLError(ErrTruncatedWrongValueForField).Code), Equals, mysql.ErrTruncatedWrongValueForField)
c.Assert(int(terror.ToSQLError(ErrUnknownPartition).Code), Equals, mysql.ErrUnknownPartition)
c.Assert(int(terror.ToSQLError(ErrNoPartitionForGivenValue).Code), Equals, mysql.ErrNoPartitionForGivenValue)
c.Assert(int(terror.ToSQLError(ErrLockOrActiveTransaction).Code), Equals, mysql.ErrLockOrActiveTransaction)
}
2 changes: 1 addition & 1 deletion tablecodec/tablecodec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,7 @@ func (s *testTableCodecSuite) TestError(c *C) {
errInvalidIndexKey,
}
for _, err := range kvErrs {
code := err.ToSQLError().Code
code := terror.ToSQLError(err).Code
c.Assert(code != mysql.ErrUnknown && code == uint16(err.Code()), IsTrue, Commentf("err: %v", err))
}
}
Loading