Skip to content

Commit

Permalink
*: fix some error related issues (pingcap#4032)
Browse files Browse the repository at this point in the history
  • Loading branch information
breezewish authored and zz-jason committed Aug 5, 2017
1 parent feecc6e commit e9785fd
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 20 deletions.
20 changes: 10 additions & 10 deletions ddl/ddl_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -575,7 +575,7 @@ func (d *ddl) buildTableInfo(tableName model.CIStr, cols []*table.Column, constr
fk.OnDelete = int(constr.Refer.OnDelete.ReferOpt)
fk.OnUpdate = int(constr.Refer.OnUpdate.ReferOpt)
if len(fk.Cols) != len(fk.RefCols) {
return nil, infoschema.ErrForeignKeyNotMatch
return nil, infoschema.ErrForeignKeyNotMatch.GenByArgs(tbInfo.Name.O)
}
if len(fk.Cols) == 0 {
// TODO: In MySQL, this case will report a parse error.
Expand Down Expand Up @@ -868,7 +868,7 @@ func (d *ddl) AddColumn(ctx context.Context, ti ast.Ident, spec *ast.AlterTableS
}
t, err := is.TableByName(ti.Schema, ti.Name)
if err != nil {
return errors.Trace(infoschema.ErrTableNotExists)
return errors.Trace(infoschema.ErrTableNotExists.GenByArgs(ti.Schema, ti.Name))
}

// Check whether added column has existed.
Expand Down Expand Up @@ -942,7 +942,7 @@ func (d *ddl) DropColumn(ctx context.Context, ti ast.Ident, colName model.CIStr)
}
t, err := is.TableByName(ti.Schema, ti.Name)
if err != nil {
return errors.Trace(infoschema.ErrTableNotExists)
return errors.Trace(infoschema.ErrTableNotExists.GenByArgs(ti.Schema, ti.Name))
}

// Check whether dropped column has existed.
Expand Down Expand Up @@ -1126,7 +1126,7 @@ func (d *ddl) getModifiableColumnJob(ctx context.Context, ident ast.Ident, origi
}
t, err := is.TableByName(ident.Schema, ident.Name)
if err != nil {
return nil, errors.Trace(infoschema.ErrTableNotExists)
return nil, errors.Trace(infoschema.ErrTableNotExists.GenByArgs(ident.Schema, ident.Name))
}

col := table.FindCol(t.Cols(), originalColName.L)
Expand Down Expand Up @@ -1295,7 +1295,7 @@ func (d *ddl) DropTable(ctx context.Context, ti ast.Ident) (err error) {

tb, err := is.TableByName(ti.Schema, ti.Name)
if err != nil {
return infoschema.ErrTableNotExists.GenByArgs(ti)
return errors.Trace(infoschema.ErrTableNotExists.GenByArgs(ti.Schema, ti.Name))
}

job := &model.Job{
Expand All @@ -1318,7 +1318,7 @@ func (d *ddl) TruncateTable(ctx context.Context, ti ast.Ident) error {
}
tb, err := is.TableByName(ti.Schema, ti.Name)
if err != nil {
return errors.Trace(infoschema.ErrTableNotExists)
return errors.Trace(infoschema.ErrTableNotExists.GenByArgs(ti.Schema, ti.Name))
}
newTableID, err := d.genGlobalID()
if err != nil {
Expand Down Expand Up @@ -1390,7 +1390,7 @@ func (d *ddl) CreateIndex(ctx context.Context, ti ast.Ident, unique bool, indexN
}
t, err := is.TableByName(ti.Schema, ti.Name)
if err != nil {
return errors.Trace(infoschema.ErrTableNotExists)
return errors.Trace(infoschema.ErrTableNotExists.GenByArgs(ti.Schema, ti.Name))
}

// Deal with anonymous index.
Expand Down Expand Up @@ -1446,7 +1446,7 @@ func (d *ddl) CreateForeignKey(ctx context.Context, ti ast.Ident, fkName model.C

t, err := is.TableByName(ti.Schema, ti.Name)
if err != nil {
return errors.Trace(infoschema.ErrTableNotExists)
return errors.Trace(infoschema.ErrTableNotExists.GenByArgs(ti.Schema, ti.Name))
}

fkInfo, err := buildFKInfo(fkName, keys, refer)
Expand Down Expand Up @@ -1477,7 +1477,7 @@ func (d *ddl) DropForeignKey(ctx context.Context, ti ast.Ident, fkName model.CIS

t, err := is.TableByName(ti.Schema, ti.Name)
if err != nil {
return errors.Trace(infoschema.ErrTableNotExists)
return errors.Trace(infoschema.ErrTableNotExists.GenByArgs(ti.Schema, ti.Name))
}

job := &model.Job{
Expand All @@ -1501,7 +1501,7 @@ func (d *ddl) DropIndex(ctx context.Context, ti ast.Ident, indexName model.CIStr
}
t, err := is.TableByName(ti.Schema, ti.Name)
if err != nil {
return errors.Trace(infoschema.ErrTableNotExists)
return errors.Trace(infoschema.ErrTableNotExists.GenByArgs(ti.Schema, ti.Name))
}

if indexInfo := findIndexByName(indexName.L, t.Meta().Indices); indexInfo == nil {
Expand Down
20 changes: 16 additions & 4 deletions ddl/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
package ddl

import (
"fmt"

"github.com/juju/errors"
"github.com/pingcap/tidb/infoschema"
"github.com/pingcap/tidb/kv"
Expand Down Expand Up @@ -73,15 +75,20 @@ func (d *ddl) onDropTable(t *meta.Meta, job *model.Job) (ver int64, _ error) {
if err != nil {
if terror.ErrorEqual(err, meta.ErrDBNotExists) {
job.State = model.JobCancelled
return ver, errors.Trace(infoschema.ErrDatabaseNotExists)
return ver, errors.Trace(infoschema.ErrDatabaseNotExists.GenByArgs(
fmt.Sprintf("(Schema ID %d)", schemaID),
))
}
return ver, errors.Trace(err)
}

// Check the table.
if tblInfo == nil {
job.State = model.JobCancelled
return ver, errors.Trace(infoschema.ErrTableNotExists)
return ver, errors.Trace(infoschema.ErrTableNotExists.GenByArgs(
fmt.Sprintf("(Schema ID %d)", schemaID),
fmt.Sprintf("(Table ID %d)", tableID),
))
}

originalState := job.SchemaState
Expand Down Expand Up @@ -157,12 +164,17 @@ func getTableInfo(t *meta.Meta, job *model.Job, schemaID int64) (*model.TableInf
if err != nil {
if terror.ErrorEqual(err, meta.ErrDBNotExists) {
job.State = model.JobCancelled
return nil, errors.Trace(infoschema.ErrDatabaseNotExists)
return nil, errors.Trace(infoschema.ErrDatabaseNotExists.GenByArgs(
fmt.Sprintf("(Schema ID %d)", schemaID),
))
}
return nil, errors.Trace(err)
} else if tblInfo == nil {
job.State = model.JobCancelled
return nil, errors.Trace(infoschema.ErrTableNotExists)
return nil, errors.Trace(infoschema.ErrTableNotExists.GenByArgs(
fmt.Sprintf("(Schema ID %d)", schemaID),
fmt.Sprintf("(Table ID %d)", tableID),
))
}

if tblInfo.State != model.StatePublic {
Expand Down
18 changes: 14 additions & 4 deletions infoschema/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package infoschema

import (
"fmt"
"sort"

"github.com/juju/errors"
Expand Down Expand Up @@ -43,7 +44,9 @@ func (b *Builder) ApplyDiff(m *meta.Meta, diff *model.SchemaDiff) ([]int64, erro

roDBInfo, ok := b.is.SchemaByID(diff.SchemaID)
if !ok {
return nil, ErrDatabaseNotExists
return nil, ErrDatabaseNotExists.GenByArgs(
fmt.Sprintf("(Schema ID %d)", diff.SchemaID),
)
}
var oldTableID, newTableID int64
tblIDs := make([]int64, 0, 2)
Expand Down Expand Up @@ -75,7 +78,9 @@ func (b *Builder) ApplyDiff(m *meta.Meta, diff *model.SchemaDiff) ([]int64, erro
if diff.Type == model.ActionRenameTable {
oldRoDBInfo, ok := b.is.SchemaByID(diff.OldSchemaID)
if !ok {
return nil, ErrDatabaseNotExists
return nil, ErrDatabaseNotExists.GenByArgs(
fmt.Sprintf("(Schema ID %d)", diff.OldSchemaID),
)
}
b.applyDropTable(oldRoDBInfo, oldTableID)
} else {
Expand Down Expand Up @@ -118,7 +123,9 @@ func (b *Builder) applyCreateSchema(m *meta.Meta, diff *model.SchemaDiff) error
if di == nil {
// When we apply an old schema diff, the database may has been dropped already, so we need to fall back to
// full load.
return ErrDatabaseNotExists
return ErrDatabaseNotExists.GenByArgs(
fmt.Sprintf("(Schema ID %d)", diff.SchemaID),
)
}
b.is.schemaMap[di.Name.L] = &schemaTables{dbInfo: di, tables: make(map[string]table.Table)}
return nil
Expand Down Expand Up @@ -147,7 +154,10 @@ func (b *Builder) applyCreateTable(m *meta.Meta, roDBInfo *model.DBInfo, tableID
if tblInfo == nil {
// When we apply an old schema diff, the table may has been dropped already, so we need to fall back to
// full load.
return ErrTableNotExists
return ErrTableNotExists.GenByArgs(
fmt.Sprintf("(Schema ID %d)", roDBInfo.ID),
fmt.Sprintf("(Table ID %d)", tableID),
)
}
if alloc == nil {
schemaID := roDBInfo.ID
Expand Down
2 changes: 1 addition & 1 deletion server/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -537,7 +537,7 @@ func (cc *clientConn) writeError(e error) error {
if te, ok = originErr.(*terror.Error); ok {
m = te.ToSQLError()
} else {
m = mysql.NewErrf(mysql.ErrUnknown, e.Error())
m = mysql.NewErrf(mysql.ErrUnknown, "%s", e.Error())
}

data := cc.alloc.AllocWithLen(4, 16+len(m.Message))
Expand Down
2 changes: 1 addition & 1 deletion terror/terror.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ func (e *Error) NotEqual(err error) bool {
// ToSQLError convert Error to mysql.SQLError.
func (e *Error) ToSQLError() *mysql.SQLError {
code := e.getMySQLErrorCode()
return mysql.NewErrf(code, e.getMsg())
return mysql.NewErrf(code, "%s", e.getMsg())
}

var defaultMySQLErrorCode uint16
Expand Down

0 comments on commit e9785fd

Please sign in to comment.