You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
78 lines
1.8 KiB
Go
78 lines
1.8 KiB
Go
package merr
|
|
|
|
import (
|
|
"context"
|
|
"joybuywork/internal/app"
|
|
"joybuywork/internal/config"
|
|
"joybuywork/internal/consts"
|
|
"joybuywork/proto/go_proto/workerr"
|
|
"errors"
|
|
"fmt"
|
|
)
|
|
|
|
var ProdErr = errors.New("有一些错误在处理中,请稍后重试~")
|
|
|
|
// BusinessError 业务相关的错误,自己定义错误码的错误
|
|
type BusinessError struct {
|
|
ctx context.Context
|
|
}
|
|
|
|
func NewBE(ctx context.Context) *BusinessError {
|
|
return &BusinessError{
|
|
ctx: ctx,
|
|
}
|
|
}
|
|
|
|
// MysqlError mysql 组件错误
|
|
func (be *BusinessError) MysqlError(err error) error {
|
|
if config.Config.Server.Env == consts.Prod {
|
|
return workerr.MysqlError(ProdErr)
|
|
}
|
|
|
|
return workerr.MysqlError(err)
|
|
}
|
|
|
|
// RedisError redis 组件错误
|
|
func (be *BusinessError) RedisError(err error, warnLv ...bool) error {
|
|
if len(warnLv) > 0 && warnLv[0] {
|
|
app.Logger.Warn(be.ctx, fmt.Sprintf("redis err: %s", err.Error()))
|
|
} else {
|
|
app.Logger.Error(be.ctx, fmt.Sprintf("redis err: %s", err.Error()))
|
|
}
|
|
|
|
if config.Config.Server.Env == consts.Prod {
|
|
return workerr.RedisError(ProdErr)
|
|
}
|
|
|
|
return workerr.RedisError(err)
|
|
}
|
|
|
|
// CacheError cache 组件错误
|
|
func (be *BusinessError) CacheError(err error, warnLv ...bool) error {
|
|
if len(warnLv) > 0 && warnLv[0] {
|
|
app.Logger.Warn(be.ctx, fmt.Sprintf("cache err: %s", err.Error()))
|
|
} else {
|
|
app.Logger.Error(be.ctx, fmt.Sprintf("cache err: %s", err.Error()))
|
|
}
|
|
|
|
if config.Config.Server.Env == consts.Prod {
|
|
return workerr.CacheError(ProdErr)
|
|
}
|
|
|
|
return workerr.CacheError(err)
|
|
}
|
|
|
|
// VerifyError 参数错误
|
|
func (be *BusinessError) VerifyError(tip string) error {
|
|
if tip == "" {
|
|
return workerr.VerifyError("参数错误")
|
|
}
|
|
|
|
return workerr.VerifyError(tip)
|
|
}
|
|
|
|
// CChanceNoneError 抽奖次数不足
|
|
func (be *BusinessError) CChanceNoneError() error {
|
|
return workerr.CChanceNoneError
|
|
}
|