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

meta: refine error code for package meta #13654

Merged
merged 16 commits into from
Nov 27, 2019
Merged
Show file tree
Hide file tree
Changes from 6 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
3 changes: 1 addition & 2 deletions domain/domain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import (
"github.com/pingcap/tidb/ddl"
"github.com/pingcap/tidb/domain/infosync"
"github.com/pingcap/tidb/kv"
"github.com/pingcap/tidb/meta"
"github.com/pingcap/tidb/metrics"
"github.com/pingcap/tidb/sessionctx/variable"
"github.com/pingcap/tidb/store/mockstore"
Expand Down Expand Up @@ -268,7 +267,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(err.Error(), Equals, "[meta:1049]database doesn't exist")
c.Assert(tblInfo1, IsNil)
m, err = dom.GetSnapshotMeta(currSnapTS)
c.Assert(err, IsNil)
Expand Down
36 changes: 12 additions & 24 deletions meta/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,14 @@ 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])
Deardrops marked this conversation as resolved.
Show resolved Hide resolved
// 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 @@ -195,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 @@ -818,23 +817,12 @@ func (m *Meta) SetSchemaDiff(diff *model.SchemaDiff) error {
return errors.Trace(err)
}

// meta error codes.
const (
codeInvalidTableKey terror.ErrCode = 1
codeInvalidDBKey = 2

codeDatabaseExists = 1007
codeDatabaseNotExists = 1049
codeTableExists = 1050
codeTableNotExists = 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(err.Error(), Equals, "[meta:1007]database already exists")
Deardrops marked this conversation as resolved.
Show resolved Hide resolved

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(err.Error(), Equals, "[meta:1050]table already exists")

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(err.Error(), Equals, "[meta:1146]table doesn't exist")
// 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(err.Error(), Equals, "[meta:1049]database doesn't exist")
// 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")
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an unexported error, so we check it by compare string.

}

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