Skip to content

Commit 1a3d895

Browse files
committed
remove errors.As-related changes
1 parent fc849e5 commit 1a3d895

2 files changed

Lines changed: 0 additions & 68 deletions

File tree

error_113.go

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,6 @@
22

33
package errorx
44

5-
import "reflect"
6-
7-
// todo add errorx.As()?
8-
// todo make another 'go error type' for passing into errors.As()?
9-
// todo when fixed, add tests for wrap/decorate etc.
10-
// As checks if target is of the same type as current error and, if true, sets target to this error value.
11-
// NB: Call to errors.As() converts any type of errorx error to any other type,
12-
// therefore such calls are currently unsafe for errorx errors and will likely break semantics.
13-
// Note than calls to errors.Is() do not suffer from the same issue.
14-
func (e *Error) As(target interface{}) bool {
15-
targetError, ok := target.(*error)
16-
if !ok {
17-
return false
18-
}
19-
20-
if !e.Is(*targetError) {
21-
return false
22-
}
23-
24-
targetVal := reflect.ValueOf(target)
25-
targetVal.Elem().Set(reflect.ValueOf(e))
26-
return true
27-
}
28-
295
// Is returns true if and only if target is errorx error that passes errorx type check against current error.
306
// This behaviour is exactly the same as that of IsOfType().
317
// See also: errors.Is()

error_113_test.go

Lines changed: 0 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ package errorx
44

55
import (
66
"errors"
7-
"fmt"
87
"io"
98
"testing"
109

@@ -103,46 +102,3 @@ func TestErrorIs(t *testing.T) {
103102
})
104103
}
105104

106-
func TestErrorAs(t *testing.T) {
107-
t.Run("Trivial", func(t *testing.T) {
108-
err := fooReturnsError()
109-
target := testType.NewWithNoMessage()
110-
require.True(t, errors.As(err, &target))
111-
require.EqualValues(t, "whoops", target.Message())
112-
output := fmt.Sprintf("%+v", target)
113-
require.Contains(t, output, "fooReturnsError", output)
114-
})
115-
116-
// Current errors.As allows no customization in this behaviour; if go types are assignable, here we go
117-
t.Run("NegativeBroken", func(t *testing.T) {
118-
err := fooReturnsError()
119-
target := testTypeBar1.NewWithNoMessage()
120-
require.True(t, errors.As(err, &target))
121-
require.EqualValues(t, "whoops", target.Message())
122-
require.True(t, IsOfType(target, testType))
123-
require.False(t, IsOfType(target, testTypeBar1))
124-
})
125-
126-
t.Run("Negative", func(t *testing.T) {
127-
err := io.EOF
128-
target := testTypeBar1.NewWithNoMessage()
129-
require.False(t, errors.As(err, &target))
130-
})
131-
132-
t.Run("DecorateForeign", func(t *testing.T) {
133-
err := Decorate(myErr("test"),"")
134-
var target myErr
135-
require.True(t, errors.As(err, &target))
136-
require.EqualValues(t, "test", target.Error())
137-
})
138-
}
139-
140-
func fooReturnsError() error {
141-
return testType.New("whoops")
142-
}
143-
144-
type myErr string
145-
146-
func (e myErr) Error() string {
147-
return string(e)
148-
}

0 commit comments

Comments
 (0)