-
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
Conversation
@lysu PTAL |
errors.go
Outdated
} | ||
|
||
func (err *chain) Cause() error { | ||
return err.prevErr |
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.
should we recursively find the root cause?
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.
No: this is the causer
interface that goes just one level deeper.
errors.go
Outdated
} | ||
|
||
func (err *chain) Error() string { | ||
return err.currentErr.Error() + "\n" + err.prevErr.Error() |
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.
Error should not got preErr.
e.g. TruncateErr, error info should only contain a column, b row value be truncate
for end-user
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.
Okay, that makes sense since this is joining two separate errors. The previous will show up in the formatter.
@@ -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 { |
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.
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
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.
Okay, the formatter is added.
IMHO, I still think do |
40b4ed6
to
818f6c4
Compare
} | ||
|
||
func (err *chain) Cause() error { | ||
return err.prevErr |
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.
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
The implementation for vertical composition of distinct errors is turning out to be complex and fragile. I think it is better to just do annotations vertically. For combining errors, we can use ErrorGroup and combine horizontally. You could use uber-go/multierr. |
The nee for this came up in pingcap/tidb#7151 (comment)