-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
error_test.go
68 lines (51 loc) · 1.41 KB
/
error_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package oops
import (
"errors"
"io/fs"
"testing"
"github.com/stretchr/testify/assert"
)
func TestErrorsIs(t *testing.T) {
is := assert.New(t)
err := Errorf("Error: %w", fs.ErrExist)
is.True(errors.Is(err, fs.ErrExist))
err = Wrap(fs.ErrExist)
is.True(errors.Is(err, fs.ErrExist))
err = Wrapf(fs.ErrExist, "Error: %w", assert.AnError)
is.True(errors.Is(err, fs.ErrExist))
err = Join(fs.ErrExist, assert.AnError)
is.True(errors.Is(err, fs.ErrExist))
err = Join(assert.AnError, fs.ErrExist)
is.True(errors.Is(err, fs.ErrExist))
err = Recover(func() {
panic(fs.ErrExist)
})
is.True(errors.Is(err, fs.ErrExist))
err = Recoverf(func() {
panic(fs.ErrExist)
}, "Error: %w", assert.AnError)
is.True(errors.Is(err, fs.ErrExist))
}
func TestErrorsAs(t *testing.T) {
is := assert.New(t)
var anError error = &fs.PathError{Err: fs.ErrExist}
var target *fs.PathError
err := Errorf("error: %w", anError)
is.True(errors.As(err, &target))
err = Wrap(anError)
is.True(errors.As(err, &target))
err = Wrapf(anError, "Error: %w", assert.AnError)
is.True(errors.As(err, &target))
err = Join(anError, assert.AnError)
is.True(errors.As(err, &target))
err = Join(assert.AnError, anError)
is.True(errors.As(err, &target))
err = Recover(func() {
panic(anError)
})
is.True(errors.As(err, &target))
err = Recoverf(func() {
panic(anError)
}, "Error: %w", assert.AnError)
is.True(errors.As(err, &target))
}