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

add BuildChain, for associating two errors together #6

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
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
49 changes: 49 additions & 0 deletions chain_format_test.go
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)
}
58 changes: 56 additions & 2 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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 {
Copy link
Collaborator

@lysu lysu Sep 11, 2018

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.

func main() {
	_, err := time.ParseDuration("dfasdf")
	err = resetErrDataTooLong("c", 1,  err)
	err = resetErrDataTooLong("b", 2, err)
	fmt.Println(errors.ErrorStack(err))
}

func resetErrDataTooLong(colName string, rowIdx int, err error) error {
	newErr := types.ErrDataTooLong.Gen("Data too long for column '%v' at row %v", colName, rowIdx)
	return errors.Wrap(err, newErr)
}

should output:

time: invalid duration dfasdf
/home/robi/Code/go/src/github.com/pingcap/tidb/executor/test/zz.go:24: [types:1406]Data too long for column 'c' at row 1
/home/robi/Code/go/src/github.com/pingcap/tidb/executor/test/zz.go:24: [types:1406]Data too long for column 'b' at row 2

I think time: invalid duration dfasdf is what we need for this method

Copy link
Author

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.

error
prevErr error
}

func (err *chain) Error() string {
return err.error.Error()
}

func (err *chain) Cause() error {
return err.prevErr
Copy link
Collaborator

@lysu lysu Sep 11, 2018

Choose a reason for hiding this comment

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

hi @gregwebs , I check juju's impl again. It seem will return .error in Cause(), and Error() will go preErr when error == nil...should we follow them ~?

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}
}
10 changes: 10 additions & 0 deletions errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -369,3 +369,13 @@ func TestWalkDeep(t *testing.T) {
t.Errorf("found not exists")
}
}

func TestChain(t *testing.T) {
if got := Cause(io.EOF).Error(); got != "EOF" {
t.Errorf("expected EOF, got %v", got)
}
caused := "caused"
if got := Cause(BuildChain(io.EOF, errors.New(caused))).Error(); got != caused {
t.Errorf("BuildChain expected caused")
}
}
5 changes: 5 additions & 0 deletions juju_adaptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ func Trace(err error) error {
return AddStack(err)
}

// Annotate returns an error annotated with the given message.
// If the given error does not have a stack trace, one is added
// at the point Annotate is called.
// If err is nil, Annotate returns nil.
func Annotate(err error, message string) error {
if err == nil {
return nil
Expand All @@ -31,6 +35,7 @@ func Annotate(err error, message string) error {
}
}

// Annotatef behaves the same as Annotate but takes formatter arguments.
func Annotatef(err error, format string, args ...interface{}) error {
if err == nil {
return nil
Expand Down
11 changes: 10 additions & 1 deletion stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,22 @@ type StackTracer interface {
StackTrace() StackTrace
}

type hasGetStackTracer interface {
getStackTracer() StackTracer
}

// GetStackTracer will return the first StackTracer in the causer chain.
// This function is used by AddStack to avoid creating redundant stack traces.
//
// You can also use the StackTracer interface on the returned error to get the stack trace.
// This function will return nil if there is no StackTracer
func GetStackTracer(origErr error) StackTracer {
var stacked StackTracer
WalkDeep(origErr, func(err error) bool {
if st, ok := err.(hasGetStackTracer); ok {
if stacked = st.getStackTracer(); stacked != nil {
return true
}
}
if stackTracer, ok := err.(StackTracer); ok {
stacked = stackTracer
return true
Expand Down