Skip to content

Commit 0a70edf

Browse files
committed
[errors] Remove DirectCause
1 parent 431edab commit 0a70edf

File tree

3 files changed

+5
-36
lines changed

3 files changed

+5
-36
lines changed

errors/cause.go

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ type causer interface {
1313
// If you want get direct cause, use DirectCause.
1414
// If error is nil, it will return nil. If error is not wrapped it will return the error itself.
1515
// error wrapped using https://github.com/pkg/errors also satisfies this interface and can be unwrapped as well.
16-
// TODO: might consider rename it to Unwrap since we are deprecating causer interface
16+
// Deprecated: Use Walk if you need root cause or Unwrap if you need direct cause.
1717
func Cause(err error) error {
1818
if err == nil {
1919
return nil
@@ -29,21 +29,4 @@ func Cause(err error) error {
2929
}
3030
}
3131
return err
32-
}
33-
34-
// DirectCause returns the direct cause of the error (if any).
35-
// It does NOT follow the cause chain all the way down, just the first one (if any),
36-
// If you want to get root cause, use Cause
37-
func DirectCause(err error) error {
38-
if err == nil {
39-
return nil
40-
}
41-
switch err.(type) {
42-
case Wrapper:
43-
return err.(Wrapper).Unwrap()
44-
case causer:
45-
return err.(causer).Cause()
46-
default:
47-
return err
48-
}
49-
}
32+
}

errors/cause_test.go

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,4 @@ func TestCause(t *testing.T) {
2121

2222
stderr := stderrors.New("std")
2323
assert.Equal(t, stderr, errors.Cause(stderr))
24-
}
25-
26-
func TestDirectCause(t *testing.T) {
27-
assert.Nil(t, errors.DirectCause(nil))
28-
29-
errw := errors.Wrap(os.ErrClosed, "can't open closed file")
30-
errww := errors.Wrap(errw, "wrap again")
31-
assert.Equal(t, os.ErrClosed, errors.Cause(errww))
32-
assert.Equal(t, os.ErrClosed, errors.Cause(errw))
33-
assert.NotEqual(t, os.ErrClosed, errors.DirectCause(errww))
34-
assert.Equal(t, "can't open closed file", errors.DirectCause(errww).(errors.Messenger).Message())
35-
36-
stderr := stderrors.New("std")
37-
assert.Equal(t, stderr, errors.DirectCause(stderr))
38-
}
24+
}

errors/inspect.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import (
66

77
// inspect.go defines functions for inspecting wrapped error or error list
88

9-
// Unwrap returns the underlying error. If the error is not wrapped, it returns nil.
10-
// Its behavior is same as standard library.
9+
// Unwrap returns the first underlying error. If the error is not wrapped, it returns nil.
10+
// It does NOT follow the entire error chain. Its behavior is same as standard library.
1111
func Unwrap(err error) error {
1212
if err == nil {
1313
return nil

0 commit comments

Comments
 (0)