Skip to content

Commit 1436037

Browse files
committed
feat: add default errors for panic.
1 parent 1823b6a commit 1436037

File tree

3 files changed

+20
-4
lines changed

3 files changed

+20
-4
lines changed

assertion.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ func New(t *testing.T) *Assertion {
2121
a := new(Assertion)
2222

2323
if t == nil {
24-
panic("parameter t is required")
24+
panic(ErrRequireT)
2525
}
2626
a.T = t
2727

error.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package assert
22

3-
import "fmt"
3+
import (
4+
"errors"
5+
"fmt"
6+
)
47

58
const (
69
defaultErrMessageEqual string = "%v == %v"
@@ -27,6 +30,19 @@ const (
2730
defaultErrMessageNotMapHasValue string = "expect map has no value %v"
2831
)
2932

33+
var (
34+
// ErrNotArray indicates that the value must be a slice or an array.
35+
ErrNotArray error = errors.New("the value must be a slice or an array")
36+
// ErrNotMap indicates that the value must be a map.
37+
ErrNotMap error = errors.New("the value must be a map")
38+
// ErrNotOrderable indicates that the value must be orderable.
39+
ErrNotOrderable error = errors.New("the value must be orderable")
40+
// ErrNotSameType indicates that both values must be the same type.
41+
ErrNotSameType error = errors.New("the values must be the same type")
42+
// ErrRequireT indicates that the instance of testing.T is a required parameter.
43+
ErrRequireT error = errors.New("testing.T is required")
44+
)
45+
3046
// AssertionError indicates the failure of an assertion.
3147
type AssertionError struct {
3248
message string

slice.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,10 @@ func isContainsElement(source, elem any) bool {
107107
st = st.Elem()
108108
}
109109
if st.Kind() != reflect.Array && st.Kind() != reflect.Slice {
110-
panic("require array or slice")
110+
panic(ErrNotArray)
111111
}
112112
if ok := isSameType(st.Type().Elem(), reflect.TypeOf(elem)); !ok {
113-
panic("require same type")
113+
panic(ErrNotSameType)
114114
}
115115

116116
if st.Len() == 0 {

0 commit comments

Comments
 (0)