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 1 commit
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
26 changes: 26 additions & 0 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -335,3 +335,29 @@ 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.

currentErr error
prevErr error
}

func (err *chain) Error() string {
return err.currentErr.Error() + "\n" + err.prevErr.Error()
Copy link
Collaborator

Choose a reason for hiding this comment

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

Error should not got preErr.

e.g. TruncateErr, error info should only contain a column, b row value be truncate for end-user

Copy link
Author

Choose a reason for hiding this comment

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

Okay, that makes sense since this is joining two separate errors. The previous will show up in the formatter.

}

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

Choose a reason for hiding this comment

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

should we recursively find the root cause?

Copy link
Author

Choose a reason for hiding this comment

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

No: this is the causer interface that goes just one level deeper.

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

}

// 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")
}
}