Added cause interface trait

This commit is contained in:
Dave Cheney 2015-12-27 15:45:25 +01:00
parent ee5ece78bc
commit 4dd713cae9
2 changed files with 11 additions and 3 deletions

View File

@ -33,3 +33,12 @@ func Cause(err error) error {
}
return err
}
// cause implements the interface required by Cause.
type cause struct {
err error
}
func (c *cause) Cause() error {
return c.err
}

View File

@ -38,11 +38,10 @@ type nilError struct{}
func (nilError) Error() string { return "nil error" }
type causeError struct {
err error
cause
}
func (e *causeError) Error() string { return "cause error" }
func (e *causeError) Cause() error { return e.err }
func TestCause(t *testing.T) {
tests := []struct {
@ -66,7 +65,7 @@ func TestCause(t *testing.T) {
want: io.EOF,
}, {
// caused error returns cause
err: &causeError{err: io.EOF},
err: &causeError{cause{err: io.EOF}},
want: io.EOF,
}}