增加IsErr方法 + 相关单元测试

This commit is contained in:
2025-01-24 15:49:15 +08:00
parent a2a3a2f653
commit a69f3a1e01
3 changed files with 68 additions and 5 deletions

34
v10/define/err.go Normal file
View File

@ -0,0 +1,34 @@
// Package define ...
//
// Description : define ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2025-01-24 15:38
package define
import (
"errors"
"strings"
)
var (
ErrValidatorRuleIsEmpty = errors.New("validator rule is empty")
ErrValidatorRuleParamCntIsTooLess = errors.New("validate rule param count is less than min param cnt")
ErrValidatorRuleParamCntIsTooMore = errors.New("validate rule param count is more than min param cnt")
ErrValidatorRuleParamCntIsNotEven = errors.New("validate rule param count is not even")
ErrValidatorRuleGroupOrSimpleRuleAllEmpty = errors.New("validator rule group or simple rule all empty")
)
// IsErr 是否指定类型Err
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 15:45 2025/1/24
func IsErr(err error, subMsg string) bool {
if nil == err {
return false
}
return strings.Contains(err.Error(), subMsg)
}

29
v10/define/err_test.go Normal file
View File

@ -0,0 +1,29 @@
// Package define ...
//
// Description : define ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2025-01-24 15:46
package define
import (
"errors"
. "github.com/smartystreets/goconvey/convey"
"testing"
)
func TestIsErr(t *testing.T) {
Convey("传入error为nil", t, func() {
isErrRes := IsErr(nil, "")
So(isErrRes, ShouldBeFalse)
})
Convey("传入error为test类型", t, func() {
isErrRes := IsErr(errors.New("aaatestbbb"), "test")
So(isErrRes, ShouldBeTrue)
})
Convey("传入error非test类型", t, func() {
isErrRes := IsErr(errors.New("aaatecccstbbb"), "test")
So(isErrRes, ShouldBeFalse)
})
}