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"
|
2023-10-26 17:15:49 +08:00
|
|
|
|
"mayfly-go/pkg/errorx"
|
2024-11-20 22:43:53 +08:00
|
|
|
|
"mayfly-go/pkg/i18n"
|
2023-07-21 17:07:04 +08:00
|
|
|
|
"mayfly-go/pkg/utils/anyx"
|
2021-03-24 17:18:39 +08:00
|
|
|
|
|
2020-09-01 10:34:11 +08:00
|
|
|
|
"reflect"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
2023-10-26 17:15:49 +08:00
|
|
|
|
// 断言错误为ni
|
|
|
|
|
|
// @param msgAndParams 消息与参数占位符,第一位为错误消息可包含%s等格式化标识。其余为Sprintf格式化值内容
|
|
|
|
|
|
//
|
2024-11-20 22:43:53 +08:00
|
|
|
|
// ErrIsNil(err)
|
|
|
|
|
|
// ErrIsNil(err, "xxxx")
|
|
|
|
|
|
// ErrIsNil(err, "xxxx: %s", "yyyy")
|
2023-10-26 17:15:49 +08:00
|
|
|
|
func ErrIsNil(err error, msgAndParams ...any) {
|
2020-09-01 10:34:11 +08:00
|
|
|
|
if err != nil {
|
2023-10-26 17:15:49 +08:00
|
|
|
|
if len(msgAndParams) == 0 {
|
2024-01-05 08:55:34 +08:00
|
|
|
|
panic(errorx.NewBiz(err.Error()))
|
2023-10-26 17:15:49 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-18 11:21:33 +08:00
|
|
|
|
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...)))
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-02-06 07:16:56 +00:00
|
|
|
|
func ErrNotNil(err error, msg string, params ...any) {
|
|
|
|
|
|
if err == nil {
|
2025-10-18 11:21:33 +08:00
|
|
|
|
panic(errorx.NewBizf(msg, params...))
|
2024-02-06 07:16:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-11-20 22:43:53 +08:00
|
|
|
|
// ErrIsNilAppendErr 断言错误为nil,否则抛出业务异常,异常消息可包含‘%s’进行错误信息提示
|
|
|
|
|
|
//
|
|
|
|
|
|
// // -> xxxx: err.Error()
|
|
|
|
|
|
// biz.ErrIsNilAppendErr(err, "xxxx: %s")
|
2021-05-08 18:00:33 +08:00
|
|
|
|
func ErrIsNilAppendErr(err error, msg string) {
|
2020-09-01 10:34:11 +08:00
|
|
|
|
if err != nil {
|
2025-10-18 11:21:33 +08:00
|
|
|
|
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 {
|
2025-10-18 11:21:33 +08:00
|
|
|
|
panic(errorx.NewBizf(i18n.TC(ctx, msgId), err.Error()))
|
2024-11-20 22:43:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-03-06 16:59:57 +08:00
|
|
|
|
func IsTrue(exp bool, msg string, params ...any) {
|
2020-09-01 10:34:11 +08:00
|
|
|
|
if !exp {
|
2025-10-18 11:21:33 +08:00
|
|
|
|
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)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-03-06 16:59:57 +08:00
|
|
|
|
func NotEmpty(str string, msg string, params ...any) {
|
2020-09-01 10:34:11 +08:00
|
|
|
|
if str == "" {
|
2025-10-18 11:21:33 +08:00
|
|
|
|
panic(errorx.NewBizf(msg, params...))
|
2020-09-01 10:34:11 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-06-01 12:31:32 +08:00
|
|
|
|
func NotNil(data any, msg string, params ...any) {
|
2020-09-01 10:34:11 +08:00
|
|
|
|
if reflect.ValueOf(data).IsNil() {
|
2025-10-18 11:21:33 +08:00
|
|
|
|
panic(errorx.NewBizf(msg, params...))
|
2020-09-01 10:34:11 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-06-01 12:31:32 +08:00
|
|
|
|
func NotBlank(data any, msg string, params ...any) {
|
2023-07-21 17:07:04 +08:00
|
|
|
|
if anyx.IsBlank(data) {
|
2025-10-18 11:21:33 +08:00
|
|
|
|
panic(errorx.NewBizf(msg, params...))
|
2021-04-16 15:10:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-06-01 12:31:32 +08:00
|
|
|
|
func IsEquals(data any, data1 any, msg string) {
|
2021-04-16 15:10:07 +08:00
|
|
|
|
if data != data1 {
|
2023-10-26 17:15:49 +08:00
|
|
|
|
panic(errorx.NewBiz(msg))
|
2021-04-16 15:10:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-06-01 12:31:32 +08:00
|
|
|
|
func Nil(data any, msg string) {
|
2020-09-01 10:34:11 +08:00
|
|
|
|
if !reflect.ValueOf(data).IsNil() {
|
2023-10-26 17:15:49 +08:00
|
|
|
|
panic(errorx.NewBiz(msg))
|
2020-09-01 10:34:11 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|