Files
mayfly-go/server/pkg/biz/assert.go

117 lines
2.6 KiB
Go
Raw Normal View History

2021-03-24 17:18:39 +08:00
package biz
2020-09-01 10:34:11 +08:00
import (
2024-11-20 22:43:53 +08:00
"context"
"mayfly-go/pkg/errorx"
2024-11-20 22:43:53 +08:00
"mayfly-go/pkg/i18n"
"mayfly-go/pkg/utils/anyx"
2021-03-24 17:18:39 +08:00
2020-09-01 10:34:11 +08:00
"reflect"
)
// 断言错误为ni
// @param msgAndParams 消息与参数占位符,第一位为错误消息可包含%s等格式化标识。其余为Sprintf格式化值内容
//
2024-11-20 22:43:53 +08:00
// ErrIsNil(err)
// ErrIsNil(err, "xxxx")
// ErrIsNil(err, "xxxx: %s", "yyyy")
func ErrIsNil(err error, msgAndParams ...any) {
2020-09-01 10:34:11 +08:00
if err != nil {
if len(msgAndParams) == 0 {
panic(errorx.NewBiz(err.Error()))
}
panic(errorx.NewBizf(msgAndParams[0].(string), msgAndParams[1:]...))
2020-09-01 10:34:11 +08:00
}
}
2024-11-20 22:43:53 +08:00
// 断言错误为ni
// @param msgId i18n消息id
//
// ErrIsNil(err)
// ErrIsNil(err, "xxxx")
// ErrIsNil(err, "xxxx: %s", "yyyy")
func ErrIsNilI(ctx context.Context, err error, msgId i18n.MsgId, attrs ...any) {
if err != nil {
if len(attrs) == 0 {
panic(errorx.NewBiz(err.Error()))
}
panic(errorx.NewBiz(i18n.TC(ctx, msgId, attrs...)))
}
}
func ErrNotNil(err error, msg string, params ...any) {
if err == nil {
panic(errorx.NewBizf(msg, params...))
}
}
2024-11-20 22:43:53 +08:00
// ErrIsNilAppendErr 断言错误为nil否则抛出业务异常异常消息可包含%s进行错误信息提示
//
// // -> xxxx: err.Error()
// biz.ErrIsNilAppendErr(err, "xxxx: %s")
func ErrIsNilAppendErr(err error, msg string) {
2020-09-01 10:34:11 +08:00
if err != nil {
panic(errorx.NewBizf(msg, err.Error()))
2020-09-01 10:34:11 +08:00
}
}
2024-11-20 22:43:53 +08:00
// ErrIsNilAppendErr 断言错误为nil否则抛出业务异常异常消息可包含%s进行错误信息提示
//
// // -> xxxx: err.Error()
// biz.ErrIsNilAppendErr(err, "xxxx: %s")
func ErrIsNilAppendErrI(ctx context.Context, err error, msgId i18n.MsgId) {
if err != nil {
panic(errorx.NewBizf(i18n.TC(ctx, msgId), err.Error()))
2024-11-20 22:43:53 +08:00
}
}
func IsTrue(exp bool, msg string, params ...any) {
2020-09-01 10:34:11 +08:00
if !exp {
panic(errorx.NewBizf(msg, params...))
2020-09-01 10:34:11 +08:00
}
}
2024-11-20 22:43:53 +08:00
func IsTrueI(ctx context.Context, exp bool, msgId i18n.MsgId, attrs ...any) {
if !exp {
panic(errorx.NewBiz(i18n.TC(ctx, msgId, attrs...)))
}
}
2024-10-23 17:30:05 +08:00
func IsTrueBy(exp bool, err *errorx.BizError) {
2021-03-24 17:18:39 +08:00
if !exp {
panic(err)
}
}
func NotEmpty(str string, msg string, params ...any) {
2020-09-01 10:34:11 +08:00
if str == "" {
panic(errorx.NewBizf(msg, params...))
2020-09-01 10:34:11 +08:00
}
}
func NotNil(data any, msg string, params ...any) {
2020-09-01 10:34:11 +08:00
if reflect.ValueOf(data).IsNil() {
panic(errorx.NewBizf(msg, params...))
2020-09-01 10:34:11 +08:00
}
}
func NotBlank(data any, msg string, params ...any) {
if anyx.IsBlank(data) {
panic(errorx.NewBizf(msg, params...))
2021-04-16 15:10:07 +08:00
}
}
func IsEquals(data any, data1 any, msg string) {
2021-04-16 15:10:07 +08:00
if data != data1 {
panic(errorx.NewBiz(msg))
2021-04-16 15:10:07 +08:00
}
}
func Nil(data any, msg string) {
2020-09-01 10:34:11 +08:00
if !reflect.ValueOf(data).IsNil() {
panic(errorx.NewBiz(msg))
2020-09-01 10:34:11 +08:00
}
}