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

41 lines
891 B
Go
Raw Normal View History

2021-03-24 17:18:39 +08:00
package biz
import "fmt"
2021-03-24 17:18:39 +08:00
// 业务错误
type BizError struct {
code int16
err string
}
var (
Success BizError = NewBizErrCode(200, "success")
BizErr BizError = NewBizErrCode(400, "biz error")
ServerError BizError = NewBizErrCode(500, "server error")
PermissionErr BizError = NewBizErrCode(501, "token error")
2021-03-24 17:18:39 +08:00
)
// 错误消息
func (e BizError) Error() string {
2021-03-24 17:18:39 +08:00
return e.err
}
// 错误码
func (e BizError) Code() int16 {
2021-03-24 17:18:39 +08:00
return e.code
}
func (e BizError) String() string {
return fmt.Sprintf("errCode: %d, errMsg: %s", e.Code(), e.Error())
}
2021-03-24 17:18:39 +08:00
// 创建业务逻辑错误结构体,默认为业务逻辑错误
func NewBizErr(msg string) BizError {
return BizError{code: BizErr.code, err: msg}
2021-03-24 17:18:39 +08:00
}
// 创建业务逻辑错误结构体可设置指定错误code
func NewBizErrCode(code int16, msg string) BizError {
return BizError{code: code, err: msg}
2021-03-24 17:18:39 +08:00
}