forked from pkg/errors
-
Notifications
You must be signed in to change notification settings - Fork 38
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
make tidb adaptor in errors. #2
Merged
Merged
Changes from 2 commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
859388f
make tidb adaptor in errors.
lysu d4948b3
address comment
lysu b6e6740
address comment
lysu e7859cb
address comment
lysu 35194dc
address comment
lysu f28c952
remove logging
gregwebs 1953f98
don't change the pkg/errors interface
gregwebs 036cd12
some renames and use GetStackTracer
gregwebs 2a9170a
avoid creating duplicate stack traces
gregwebs 70a3293
keep tests as pkg/errors
gregwebs 3397c8a
fix tests
gregwebs 831aa37
add an Unwrap function
gregwebs 223d5a6
add the ErrorGroup interface
gregwebs e523106
add a Find function for searching an error chain
gregwebs 17afee1
Merge pull request #1 from gregwebs/dev-tidb-adaptor-no-log
lysu 0a67e46
Merge branch 'dev-tidb-adaptor' into dev-tidb-adaptor
lysu 2674428
Merge pull request #2 from gregwebs/dev-tidb-adaptor
lysu df8a356
address comment
lysu ba07308
address comment
lysu 0485f1f
address comment
lysu e446394
address comment
lysu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -115,6 +115,18 @@ func Errorf(format string, args ...interface{}) error { | |
} | ||
} | ||
|
||
type withStackAware interface { | ||
hasStack() bool | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should it be upper-case to |
||
} | ||
|
||
func hasStack(err error) bool { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. rename to |
||
errWithStack, hasStack := err.(withStackAware) | ||
if !hasStack { | ||
return false | ||
} | ||
return errWithStack.hasStack() | ||
} | ||
|
||
// fundamental is an error that has a message and a stack, but no caller. | ||
type fundamental struct { | ||
msg string | ||
|
@@ -139,6 +151,10 @@ func (f *fundamental) Format(s fmt.State, verb rune) { | |
} | ||
} | ||
|
||
func (f *fundamental) hasStack() bool { | ||
return true | ||
} | ||
|
||
// WithStack annotates err with a stack trace at the point WithStack was called. | ||
// If err is nil, WithStack returns nil. | ||
func WithStack(err error) error { | ||
|
@@ -174,33 +190,47 @@ func (w *withStack) Format(s fmt.State, verb rune) { | |
} | ||
} | ||
|
||
// Wrap returns an error annotating err with a stack trace | ||
// at the point Wrap is called, and the supplied message. | ||
// If err is nil, Wrap returns nil. | ||
func Wrap(err error, message string) error { | ||
func (w *withStack) hasStack() bool { | ||
return true | ||
} | ||
|
||
// Annotate returns an error annotating err with a stack trace | ||
// at the point Annotate is called, and the supplied message. | ||
// If err is nil, Annotate returns nil. | ||
func Annotate(err error, message string) error { | ||
if err == nil { | ||
return nil | ||
} | ||
hasStack := hasStack(err) | ||
err = &withMessage{ | ||
cause: err, | ||
msg: message, | ||
cause: err, | ||
msg: message, | ||
causeHasStack: hasStack, | ||
} | ||
if hasStack { | ||
return err | ||
} | ||
return &withStack{ | ||
err, | ||
callers(), | ||
} | ||
} | ||
|
||
// Wrapf returns an error annotating err with a stack trace | ||
// at the point Wrapf is call, and the format specifier. | ||
// If err is nil, Wrapf returns nil. | ||
func Wrapf(err error, format string, args ...interface{}) error { | ||
// Annotatef returns an error annotating err with a stack trace | ||
// at the point Annotatef is call, and the format specifier. | ||
// If err is nil, Annotatef returns nil. | ||
func Annotatef(err error, format string, args ...interface{}) error { | ||
if err == nil { | ||
return nil | ||
} | ||
hasStack := hasStack(err) | ||
err = &withMessage{ | ||
cause: err, | ||
msg: fmt.Sprintf(format, args...), | ||
cause: err, | ||
msg: fmt.Sprintf(format, args...), | ||
causeHasStack: hasStack, | ||
} | ||
if hasStack { | ||
return err | ||
} | ||
return &withStack{ | ||
err, | ||
|
@@ -215,18 +245,21 @@ func WithMessage(err error, message string) error { | |
return nil | ||
} | ||
return &withMessage{ | ||
cause: err, | ||
msg: message, | ||
cause: err, | ||
msg: message, | ||
causeHasStack: hasStack(err), | ||
} | ||
} | ||
|
||
type withMessage struct { | ||
cause error | ||
msg string | ||
cause error | ||
msg string | ||
causeHasStack bool | ||
} | ||
|
||
func (w *withMessage) Error() string { return w.msg + ": " + w.cause.Error() } | ||
func (w *withMessage) Cause() error { return w.cause } | ||
func (w *withMessage) Error() string { return w.msg + ": " + w.cause.Error() } | ||
func (w *withMessage) Cause() error { return w.cause } | ||
func (w *withMessage) hasStack() bool { return w.causeHasStack } | ||
|
||
func (w *withMessage) Format(s fmt.State, verb rune) { | ||
switch verb { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not
hasStackAware
?