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

remove duplicate error code check and add some public method #22

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 8 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
34 changes: 31 additions & 3 deletions terror_error.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ import (
"runtime"
"strconv"
"strings"

"github.com/pingcap/log"
"go.uber.org/zap"
)

// Error is the 'prototype' of a type of errors.
Expand Down Expand Up @@ -128,10 +131,10 @@ func (e *Error) Error() string {
if len(describe) == 0 {
describe = ErrCodeText(strconv.Itoa(int(e.code)))
}
return fmt.Sprintf("[%s] %s", e.RFCCode(), e.getMsg())
return fmt.Sprintf("[%s] %s", e.RFCCode(), e.GetMsg())
}

func (e *Error) getMsg() string {
func (e *Error) GetMsg() string {
if len(e.args) > 0 {
return fmt.Sprintf(e.message, e.args...)
}
Expand Down Expand Up @@ -251,7 +254,7 @@ type jsonError struct {
// This function is reserved for compatibility.
func (e *Error) MarshalJSON() ([]byte, error) {
return json.Marshal(&jsonError{
Error: e.getMsg(),
Error: e.GetMsg(),
Description: e.Description,
Workaround: e.Workaround,
RFCCode: e.RFCCode(),
Expand Down Expand Up @@ -291,3 +294,28 @@ func (e *Error) UnmarshalJSON(data []byte) error {
}
return nil
}

// MustNil cleans up and fatals if err is not nil.
func MustNil(err error, closeFuns ...func()) {
if err != nil {
for _, f := range closeFuns {
f()
}
log.Fatal("unexpected error", zap.Error(err))
}
}

// Call executes a function and checks the returned err.
func Call(fn func() error) {
err := fn()
if err != nil {
log.Error("function call errored", zap.Error(err))
}
}

// Log logs the error if it is not nil.
func Log(err error) {
if err != nil {
log.Error("encountered error", zap.Error(WithStack(err)))
}
}
22 changes: 19 additions & 3 deletions terror_registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,25 @@ type Registry struct {
// Same error code can be used in different error classes.
type ErrCode int

const (
// Executor error codes.

// CodeUnknown is for errors of unknown reason.
CodeUnknown ErrCode = -1
// CodeExecResultIsEmpty indicates execution result is empty.
CodeExecResultIsEmpty ErrCode = 3

// Expression error codes.

// CodeMissConnectionID indicates connection id is missing.
CodeMissConnectionID ErrCode = 1

// Special error codes.

// CodeResultUndetermined indicates the sql execution result is undetermined.
CodeResultUndetermined ErrCode = 2
Copy link
Member

Choose a reason for hiding this comment

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

Please do not add TiDB specific error to this lib. How about defining these code in TiDB?

Copy link
Author

Choose a reason for hiding this comment

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

has been moved to TiDB, PTAL

)

// ErrCodeText is a textual error code that represents a specific error type in a error class.
type ErrCodeText string

Expand Down Expand Up @@ -134,9 +153,6 @@ func (ec *ErrClass) DefineError() *Builder {
// RegisterError try to register an error to a class.
// return true if success.
func (ec *ErrClass) RegisterError(err *Error) bool {
if _, ok := ec.errors[err.ID()]; ok {
return false
}
err.class = ec
ec.errors[err.ID()] = err
return true
Expand Down