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

fixed 'bad access: nil dereference' when calling Error() method if it… #577

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all 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
7 changes: 7 additions & 0 deletions terror/terror.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"fmt"
"strconv"

"github.com/modern-go/reflect2"
"github.com/pingcap/errors"
"github.com/pingcap/parser/mysql"
log "github.com/sirupsen/logrus"
Expand Down Expand Up @@ -213,10 +214,16 @@ func (e *Error) Location() (file string, line int) {

// Error implements error interface.
func (e *Error) Error() string {
if reflect2.IsNil(e) {
Copy link
Contributor

Choose a reason for hiding this comment

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

why e == nil is not enough? *Error is a concrete type unlike interface{}.

Copy link
Author

Choose a reason for hiding this comment

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

you are right, e == nil is better.

return "<nil>"
}
return fmt.Sprintf("[%s:%d]%s", e.class, e.code, e.getMsg())
}

func (e *Error) getMsg() string {
if reflect2.IsNil(e) {
Copy link
Contributor

Choose a reason for hiding this comment

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

(ditto)

return "<nil>"
}
if len(e.args) > 0 {
return fmt.Sprintf(e.message, e.args...)
}
Expand Down