From 246b91edd49a7b86cd592cab3c636ba1ff855f0d Mon Sep 17 00:00:00 2001 From: Maxwell Date: Wed, 27 Nov 2019 12:57:07 +0800 Subject: [PATCH] meta: refine error code for package `meta` (#13654) --- domain/domain_test.go | 2 +- meta/meta.go | 32 ++++++++++++-------------------- meta/meta_test.go | 5 +++++ 3 files changed, 18 insertions(+), 21 deletions(-) diff --git a/domain/domain_test.go b/domain/domain_test.go index 9890cf6f72456..9e6e6d9fddace 100644 --- a/domain/domain_test.go +++ b/domain/domain_test.go @@ -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) diff --git a/meta/meta.go b/meta/meta.go index 289be03042c60..4c10e30cff46a 100644 --- a/meta/meta.go +++ b/meta/meta.go @@ -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. @@ -194,7 +194,7 @@ 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) } @@ -202,7 +202,7 @@ func (m *Meta) checkDBExists(dbKey []byte) error { 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) } @@ -210,7 +210,7 @@ func (m *Meta) checkDBNotExists(dbKey []byte) error { 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) } @@ -218,7 +218,7 @@ func (m *Meta) checkTableExists(dbKey []byte, tableKey []byte) error { 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) } @@ -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 } diff --git a/meta/meta_test.go b/meta/meta_test.go index c130580292edb..8c8c23f0e0b57 100644 --- a/meta/meta_test.go +++ b/meta/meta_test.go @@ -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) @@ -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) @@ -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, @@ -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) {