forked from pkg/errors
-
Notifications
You must be signed in to change notification settings - Fork 39
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
add BuildChain, for associating two errors together #6
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package errors | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func resetErrDataTooLong(colName string, rowIdx int, err error) error { | ||
newErr := Errorf("Data too long for column '%v' at row %v", colName, rowIdx) | ||
return BuildChain(newErr, err) | ||
} | ||
func TestChainFormat(t *testing.T) { | ||
_, errTime := time.ParseDuration("dfasdf") | ||
err1 := resetErrDataTooLong("c", 1, errTime) | ||
err2 := resetErrDataTooLong("b", 2, err1) | ||
|
||
expectedV := "time: invalid duration dfasdf\n" + | ||
"Data too long for column 'c' at row 1\n" + | ||
"Data too long for column 'b' at row 2" | ||
if fmt.Sprintf("%v", err2) != expectedV { | ||
t.Errorf("expected %v", expectedV) | ||
} | ||
if fmt.Sprintf("%s", err2) != expectedV { | ||
t.Errorf("expected %v", expectedV) | ||
} | ||
|
||
expectedS := "\"time: invalid duration dfasdf\"\n" + | ||
"\"Data too long for column 'c' at row 1\"\n" + | ||
"\"Data too long for column 'b' at row 2\"" | ||
gotQ := fmt.Sprintf("%q", err2) | ||
if gotQ != expectedS { | ||
t.Errorf("expected %v, got %v", expectedS, gotQ) | ||
} | ||
|
||
expectedPV := `time: invalid duration dfasdf | ||
Data too long for column 'c' at row 1 | ||
github.com/pkg/errors.resetErrDataTooLong | ||
.+/github.com/pkg/errors/chain_format_test.go:10 | ||
github.com/pkg/errors.TestChainFormat | ||
.+/github.com/pkg/errors/chain_format_test.go:15 | ||
testing.tRunner | ||
.+testing.go:777 | ||
runtime.goexit | ||
.+asm_amd64.s:2361 | ||
.*Data too long for column 'b' at row 2` | ||
|
||
testFormatRegexp(t, 0, err2, "%+v", expectedPV) | ||
} |
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 |
---|---|---|
|
@@ -111,8 +111,7 @@ func Errorf(format string, args ...interface{}) error { | |
} | ||
|
||
// StackTraceAware is an optimization to avoid repetitive traversals of an error chain. | ||
// HasStack checks for this marker first. | ||
// Annotate/Wrap and Annotatef/Wrapf will produce this marker. | ||
// Annotate/Wrap and Annotatef/Wrapf will produce this interface. | ||
type StackTraceAware interface { | ||
HasStack() bool | ||
} | ||
|
@@ -319,3 +318,58 @@ func Find(origErr error, test func(error) bool) error { | |
}) | ||
return foundErr | ||
} | ||
|
||
// Chain ties together two errors, giving them a Causer interface and a concatenated Error message. | ||
type chain struct { | ||
error | ||
prevErr error | ||
} | ||
|
||
func (err *chain) Error() string { | ||
return err.error.Error() | ||
} | ||
|
||
func (err *chain) Cause() error { | ||
return err.prevErr | ||
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. hi @gregwebs , I check juju's impl again. It seem will return expect this two question, others are look good to me |
||
} | ||
|
||
// getStackTracer implements hasGetStackTracer. | ||
// Without this override, the GetStackTracer() fucntion would end up skipping the current error. | ||
// This may return nil. | ||
func (err *chain) getStackTracer() StackTracer { | ||
return GetStackTracer(err.error) | ||
} | ||
|
||
func (err *chain) Format(s fmt.State, verb rune) { | ||
switch verb { | ||
case 'v': | ||
if s.Flag('+') { | ||
fmt.Fprintf(s, "%+v\n", err.prevErr) | ||
if HasStack(err.prevErr) { | ||
fmt.Fprintf(s, "%v", err.error) | ||
} else { | ||
fmt.Fprintf(s, "%+v", err.error) | ||
} | ||
return | ||
} | ||
fallthrough | ||
case 's': | ||
fmt.Fprintf(s, "%s\n", err.prevErr) | ||
fmt.Fprintf(s, "%s", err.error) | ||
case 'q': | ||
fmt.Fprintf(s, "%q\n", err.prevErr) | ||
fmt.Fprintf(s, "%q", err.error) | ||
} | ||
} | ||
|
||
// BuildChain constructs a Chain. | ||
// If the newer error is already a Chain, it will return the original ErrorChain | ||
// modified so that the previous error is a chain. | ||
func BuildChain(currentErr error, prevErr error) error { | ||
if currentChain, ok := currentErr.(*chain); ok { | ||
middleErr := currentChain.prevErr | ||
currentChain.prevErr = &chain{middleErr, prevErr} | ||
return currentChain | ||
} | ||
return &chain{currentErr, prevErr} | ||
} |
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
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
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
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.
need override
ErrorStack
or%+v
to print errors info before wrap recursively.e.g.
should output:
I think
time: invalid duration dfasdf
is what we need for this methodThere 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.
Okay, the formatter is added.