-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherrors.go
99 lines (90 loc) · 2.74 KB
/
errors.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package errassert
import (
"errors"
"fmt"
"strings"
)
// SomeError returns an assertion that checks if the error is not nil.
func SomeError() ErrorAssertion {
return func(err error) error {
if err == nil {
return errors.New("expected an error but got nil")
}
return nil
}
}
// NilError returns an assertion that checks if the error is nil.
func NilError() ErrorAssertion {
return func(err error) error {
if err != nil {
return fmt.Errorf("expected a nil error but got '%s'", err)
}
return nil
}
}
// Error returns an assertion that checks if the error is equal to the error with the given message.
func Error(msg string) ErrorAssertion {
return func(err error) error {
if err == nil {
return fmt.Errorf("expected error to be '%s' but got '<nil>'", msg)
}
if err.Error() != msg {
return fmt.Errorf("expected error to be '%s' but got '%v'", msg, err)
}
return nil
}
}
// ErrorIs returns an assertion that checks if the error passes errors.Is check.
func ErrorIs(expected error) ErrorAssertion {
return func(err error) error {
if !errors.Is(err, expected) {
return fmt.Errorf("expected error to be '%v' but got '%v'", expected, err)
}
return nil
}
}
// ErrorAs returns an assertion that checks if the error passes errors.As check.
func ErrorAs(target interface{}) ErrorAssertion {
return func(err error) error {
if !errors.As(err, target) {
return fmt.Errorf("expected error to be '%T' but got '%T'", target, err)
}
return nil
}
}
// ErrorContains returns an assertion that checks if the error contains the given substring.
func ErrorContains(substring string) ErrorAssertion {
return func(err error) error {
if err == nil {
return fmt.Errorf("expected an error containing '%s' but got nil", substring)
}
if !strings.Contains(err.Error(), substring) {
return fmt.Errorf("expected an error containing '%s' but got '%v'", substring, err)
}
return nil
}
}
// ErrorStartsWith returns an assertion that checks if the error starts with the given prefix.
func ErrorStartsWith(prefix string) ErrorAssertion {
return func(err error) error {
if err == nil {
return fmt.Errorf("expected an error starting with '%s' but got nil", prefix)
}
if !strings.HasPrefix(err.Error(), prefix) {
return fmt.Errorf("expected an error starting with '%s' but got '%v'", prefix, err)
}
return nil
}
}
// ErrorEndsWith returns an assertion that checks if the error ends with the given suffix.
func ErrorEndsWith(suffix string) ErrorAssertion {
return func(err error) error {
if err == nil {
return fmt.Errorf("expected an error ending with '%s' but got nil", suffix)
}
if !strings.HasSuffix(err.Error(), suffix) {
return fmt.Errorf("expected an error ending with '%s' but got '%v'", suffix, err)
}
return nil
}
}