Skip to content

Commit

Permalink
meta: refine error code for package meta (pingcap#13654)
Browse files Browse the repository at this point in the history
  • Loading branch information
Deardrops authored and qw4990 committed Nov 27, 2019
1 parent 875a0bc commit 01a9e13
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 21 deletions.
2 changes: 1 addition & 1 deletion domain/domain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ func (*testSuite) TestT(c *C) {
m, err := dom.GetSnapshotMeta(snapTS)
c.Assert(err, IsNil)
tblInfo1, err := m.GetTable(dbInfo.ID, tbl.Meta().ID)
c.Assert(err.Error(), Equals, meta.ErrDBNotExists.Error())
c.Assert(meta.ErrDBNotExists.Equal(err), IsTrue)
c.Assert(tblInfo1, IsNil)
m, err = dom.GetSnapshotMeta(currSnapTS)
c.Assert(err, IsNil)
Expand Down
32 changes: 12 additions & 20 deletions meta/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,13 @@ var (

var (
// ErrDBExists is the error for db exists.
ErrDBExists = terror.ClassMeta.New(codeDatabaseExists, "database already exists")
ErrDBExists = terror.ClassMeta.New(mysql.ErrDBCreateExists, mysql.MySQLErrName[mysql.ErrDBCreateExists])
// ErrDBNotExists is the error for db not exists.
ErrDBNotExists = terror.ClassMeta.New(codeDatabaseNotExists, "database doesn't exist")
ErrDBNotExists = terror.ClassMeta.New(mysql.ErrBadDB, mysql.MySQLErrName[mysql.ErrBadDB])
// ErrTableExists is the error for table exists.
ErrTableExists = terror.ClassMeta.New(codeTableExists, "table already exists")
ErrTableExists = terror.ClassMeta.New(mysql.ErrTableExists, mysql.MySQLErrName[mysql.ErrTableExists])
// ErrTableNotExists is the error for table not exists.
ErrTableNotExists = terror.ClassMeta.New(codeTableNotExists, "table doesn't exist")
ErrTableNotExists = terror.ClassMeta.New(mysql.ErrNoSuchTable, mysql.MySQLErrName[mysql.ErrNoSuchTable])
)

// Meta is for handling meta information in a transaction.
Expand Down Expand Up @@ -194,31 +194,31 @@ func (m *Meta) GenSchemaVersion() (int64, error) {
func (m *Meta) checkDBExists(dbKey []byte) error {
v, err := m.txn.HGet(mDBs, dbKey)
if err == nil && v == nil {
err = ErrDBNotExists
err = ErrDBNotExists.GenWithStack("database doesn't exist")
}
return errors.Trace(err)
}

func (m *Meta) checkDBNotExists(dbKey []byte) error {
v, err := m.txn.HGet(mDBs, dbKey)
if err == nil && v != nil {
err = ErrDBExists
err = ErrDBExists.GenWithStack("database already exists")
}
return errors.Trace(err)
}

func (m *Meta) checkTableExists(dbKey []byte, tableKey []byte) error {
v, err := m.txn.HGet(dbKey, tableKey)
if err == nil && v == nil {
err = ErrTableNotExists
err = ErrTableNotExists.GenWithStack("table doesn't exist")
}
return errors.Trace(err)
}

func (m *Meta) checkTableNotExists(dbKey []byte, tableKey []byte) error {
v, err := m.txn.HGet(dbKey, tableKey)
if err == nil && v != nil {
err = ErrTableExists
err = ErrTableExists.GenWithStack("table already exists")
}
return errors.Trace(err)
}
Expand Down Expand Up @@ -817,20 +817,12 @@ func (m *Meta) SetSchemaDiff(diff *model.SchemaDiff) error {
return errors.Trace(err)
}

// meta error codes.
const (
codeDatabaseExists terror.ErrCode = 1007
codeDatabaseNotExists terror.ErrCode = 1049
codeTableExists terror.ErrCode = 1050
codeTableNotExists terror.ErrCode = 1146
)

func init() {
metaMySQLErrCodes := map[terror.ErrCode]uint16{
codeDatabaseExists: mysql.ErrDBCreateExists,
codeDatabaseNotExists: mysql.ErrBadDB,
codeTableNotExists: mysql.ErrNoSuchTable,
codeTableExists: mysql.ErrTableExists,
mysql.ErrDBCreateExists: mysql.ErrDBCreateExists,
mysql.ErrBadDB: mysql.ErrBadDB,
mysql.ErrNoSuchTable: mysql.ErrNoSuchTable,
mysql.ErrTableExists: mysql.ErrTableExists,
}
terror.ErrClassToMySQLCodes[terror.ClassMeta] = metaMySQLErrCodes
}
5 changes: 5 additions & 0 deletions meta/meta_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ func (s *testSuite) TestMeta(c *C) {

err = t.CreateDatabase(dbInfo)
c.Assert(err, NotNil)
c.Assert(meta.ErrDBExists.Equal(err), IsTrue)

v, err := t.GetDatabase(1)
c.Assert(err, IsNil)
Expand Down Expand Up @@ -131,6 +132,7 @@ func (s *testSuite) TestMeta(c *C) {

err = t.CreateTableOrView(1, tbInfo)
c.Assert(err, NotNil)
c.Assert(meta.ErrTableExists.Equal(err), IsTrue)

tbInfo.Name = model.NewCIStr("tt")
err = t.UpdateTable(1, tbInfo)
Expand Down Expand Up @@ -193,11 +195,13 @@ func (s *testSuite) TestMeta(c *C) {
nonExistentID := int64(1234)
_, err = t.GenAutoTableID(currentDBID, nonExistentID, 10)
c.Assert(err, NotNil)
c.Assert(meta.ErrTableNotExists.Equal(err), IsTrue)
// Fail to update auto ID.
// The current database ID doesn't exist.
currentDBID = nonExistentID
_, err = t.GenAutoTableID(currentDBID, tid, 10)
c.Assert(err, NotNil)
c.Assert(meta.ErrDBNotExists.Equal(err), IsTrue)
// Test case for CreateTableAndSetAutoID.
tbInfo3 := &model.TableInfo{
ID: 3,
Expand Down Expand Up @@ -290,6 +294,7 @@ func (s *testSuite) TestSnapshot(c *C) {
c.Assert(n, Equals, int64(1))
_, err = snapMeta.GenGlobalID()
c.Assert(err, NotNil)
c.Assert(err.Error(), Equals, "[structure:5]write on snapshot")
}

func (s *testSuite) TestDDL(c *C) {
Expand Down

0 comments on commit 01a9e13

Please sign in to comment.