1 Star 0 Fork 0

h79/goutils

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
result.go 3.96 KB
一键复制 编辑 原始数据 按行查看 历史
huqiuyun 提交于 2024-06-20 21:48 . rpc error
package result
import (
"context"
"errors"
"fmt"
"go.uber.org/zap"
"reflect"
)
var DefaultOk = ErrOk
var Type = reflect.TypeOf((*Object)(nil)).Elem()
type Object interface {
ToResult() Result
}
type Result struct {
Errno int32 `json:"code"` //业务错误码=0,表示正确,其它失败
SubCode int32 `json:"subCode"` //业务子码
Desc string `json:"desc"` //错误信息
Msg string `json:"msg"` //提供给界面提示之类
}
func New(code int32, err string) error {
return Error(code, err)
}
func Case(a any) (Result, bool) {
val := reflect.ValueOf(a)
if val.CanInterface() && val.Type().Implements(Type) {
var v = val.Interface().(Object)
return v.ToResult(), true
}
return Result{}, false
}
func Succeed() Result {
return Result{Errno: DefaultOk, Desc: ""}
}
func Error(code int32, err string) Result {
return Result{Errno: code, Desc: err, Msg: err}
}
func ErrCode(code int32) Result {
return Result{Errno: code}
}
func Errorf(code int32, f string, arg ...interface{}) Result {
desc := fmt.Sprintf(f, arg...)
return Result{Errno: code, Desc: desc, Msg: desc}
}
func WithErr(err error) Result {
res := Result{}
return res.WithError(err)
}
func IsOK(code int32) bool {
return code == DefaultOk || code == Success
}
func (r Result) Is(err error) bool {
if err == nil {
return false
}
var res *Result
if errors.As(err, &res) {
return res.Errno == r.Errno && res.SubCode == r.SubCode
}
var res1 Result
if errors.As(err, &res1) {
return res1.Errno == r.Errno && res1.SubCode == r.SubCode
}
return false
}
// error interface
func (r Result) Error() string {
if r.Msg != "" {
return r.Msg
}
return r.Desc
}
func (r Result) Ok() bool {
return IsOK(r.Errno)
}
func (r Result) NotFound() bool {
return r.Equal(ErrNotFound)
}
// NonFound
// 不是NOT FOUND的错误
func (r Result) NonFound() bool {
return !r.Equal(ErrOk) && !r.Equal(ErrNotFound) && !r.Equal(Success)
}
func (r Result) Equal(code int32) bool {
return code == r.Errno
}
func (r Result) WithSubCode(code int32) Result {
r.SubCode = code
return r
}
func (r Result) WithMsg(msg string) Result {
r.Msg = msg
return r
}
func (r Result) AppendMsg(msg string) Result {
r.Msg += msg
return r
}
func (r Result) AppendDesc(desc string) Result {
r.Desc += desc
return r
}
func (r Result) WithError(err error) Result {
if err == nil {
return Succeed()
}
var res *Result
if errors.As(err, &res) {
return (*res).Exception(r.Errno)
}
var res1 Result
if errors.As(err, &res1) {
return res1.Exception(r.Errno)
}
var i18n I18n
if errors.As(err, &i18n) {
r.Msg = i18n.Content
return r.Exception(ErrException)
}
var i18n1 *I18n
if errors.As(err, &i18n1) {
r.Msg = i18n1.Content
return r.Exception(ErrException)
}
if res, ok := Case(err); ok {
return res.Exception(r.Errno)
}
r.Desc = err.Error()
r.Msg = r.Desc
return r.Exception(ErrException)
}
func (r Result) Exception(code int32) Result {
if r.Ok() {
if code == 0 {
code = ErrException
}
r.Errno = code
}
return r
}
func (r Result) Log() Result {
if !r.Ok() {
return r.LogWithContext(context.Background())
}
return r
}
func (r Result) LogWithContext(ctx context.Context) Result {
if !r.Ok() {
zap.L().WithOptions(zap.AddCallerSkip(1)).Error("Result",
zap.Any("traceId", ctx.Value("traceId")),
zap.Int32("errno", r.Errno),
zap.Int32("subCode", r.SubCode),
zap.String("msg", r.Msg),
zap.String("desc", r.Desc))
}
return r
}
func (r Result) LogM(model string, d interface{}) Result {
if !r.Ok() {
return r.LogMWithContext(context.Background(), model, d)
}
return r
}
func (r Result) LogMWithContext(ctx context.Context, model string, d interface{}) Result {
if !r.Ok() {
zap.L().WithOptions(zap.AddCallerSkip(1)).Error("Result",
zap.Any("traceId", ctx.Value("traceId")),
zap.Any("data", d),
zap.String("model", model),
zap.Int32("errno", r.Errno),
zap.Int32("subCode", r.SubCode),
zap.String("msg", r.Msg),
zap.String("desc", r.Desc))
}
return r
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/h79/goutils.git
git@gitee.com:h79/goutils.git
h79
goutils
goutils
v1.20.77

搜索帮助