Skip to content
This repository was archived by the owner on Dec 1, 2021. It is now read-only.

Commit 4dd713c

Browse files
committed
Added cause interface trait
1 parent ee5ece7 commit 4dd713c

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

errors.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,12 @@ func Cause(err error) error {
3333
}
3434
return err
3535
}
36+
37+
// cause implements the interface required by Cause.
38+
type cause struct {
39+
err error
40+
}
41+
42+
func (c *cause) Cause() error {
43+
return c.err
44+
}

errors_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,10 @@ type nilError struct{}
3838
func (nilError) Error() string { return "nil error" }
3939

4040
type causeError struct {
41-
err error
41+
cause
4242
}
4343

4444
func (e *causeError) Error() string { return "cause error" }
45-
func (e *causeError) Cause() error { return e.err }
4645

4746
func TestCause(t *testing.T) {
4847
tests := []struct {
@@ -66,7 +65,7 @@ func TestCause(t *testing.T) {
6665
want: io.EOF,
6766
}, {
6867
// caused error returns cause
69-
err: &causeError{err: io.EOF},
68+
err: &causeError{cause{err: io.EOF}},
7069
want: io.EOF,
7170
}}
7271

0 commit comments

Comments
 (0)