library-go/errs/error_code.go

88 lines
2.7 KiB
Go
Raw Normal View History

2024-11-13 04:03:14 +00:00
package errs
2024-11-13 04:06:40 +00:00
import (
"code.30cm.net/digimon/library-go/errs/code"
"fmt"
"github.com/zeromicro/go-zero/core/logx"
"strings"
)
2024-11-13 04:03:14 +00:00
type ErrorCode uint32
func (e ErrorCode) ToUint32() uint32 {
return uint32(e)
}
2024-11-13 04:06:40 +00:00
func ThirdPartyError(scope uint32, ec ErrorCode, s ...string) *LibError {
return NewError(scope, code.ThirdParty, ec.ToUint32(), fmt.Sprintf("thirty error: %s", strings.Join(s, " ")))
}
func ThirdPartyErrorL(scope uint32, ec ErrorCode,
l logx.Logger, filed []logx.LogField, s ...string) *LibError {
e := ThirdPartyError(scope, ec, s...)
l.WithCallerSkip(1).WithFields(filed...).Error(e.Error())
return e
}
2024-11-13 05:10:02 +00:00
2024-11-13 06:58:30 +00:00
func DatabaseErrorWithScope(scope uint32, ec ErrorCode, s ...string) *LibError {
2024-11-13 05:10:02 +00:00
return NewError(scope, code.DBError, ec.ToUint32(), strings.Join(s, " "))
}
func DatabaseErrorWithScopeL(scope uint32,
ec ErrorCode,
2024-11-13 06:58:30 +00:00
l logx.Logger, filed []logx.LogField, s ...string) *LibError {
2024-11-13 05:10:02 +00:00
e := DatabaseErrorWithScope(scope, ec, s...)
l.WithCallerSkip(1).WithFields(filed...).Error(e.Error())
return e
}
func ResourceNotFoundWithScope(scope uint32, ec ErrorCode, s ...string) *LibError {
return NewError(scope, code.ResourceNotFound, ec.ToUint32(), fmt.Sprintf("resource not found: %s", strings.Join(s, " ")))
}
func ResourceNotFoundWithScopeL(scope uint32, ec ErrorCode,
l logx.Logger, filed []logx.LogField, s ...string) *LibError {
e := ResourceNotFoundWithScope(scope, ec, s...)
l.WithCallerSkip(1).WithFields(filed...).Error(e.Error())
return e
}
func InvalidRangeWithScope(scope uint32, ec ErrorCode, s ...string) *LibError {
return NewError(scope, code.CatInput, ec.ToUint32(), fmt.Sprintf("invalid range: %s", strings.Join(s, " ")))
}
2024-11-15 07:25:21 +00:00
func InvalidRangeWithScopeL(scope uint32, ec ErrorCode,
l logx.Logger, filed []logx.LogField, s ...string) *LibError {
e := InvalidRangeWithScope(scope, ec, s...)
l.WithCallerSkip(1).WithFields(filed...).Error(e.Error())
return e
}
2024-11-13 05:10:02 +00:00
func InvalidFormatWithScope(scope uint32, s ...string) *LibError {
return NewError(scope, code.CatInput, code.InvalidFormat, fmt.Sprintf("invalid range: %s", strings.Join(s, " ")))
}
2024-11-15 07:25:21 +00:00
func InvalidFormatWithScopeL(scope uint32,
l logx.Logger, filed []logx.LogField, s ...string) *LibError {
e := InvalidFormatWithScope(scope, s...)
l.WithCallerSkip(1).WithFields(filed...).Error(e.Error())
return e
}
2024-11-13 05:10:02 +00:00
func ForbiddenWithScope(scope uint32, ec ErrorCode, s ...string) *LibError {
return NewError(scope, code.Forbidden, ec.ToUint32(), fmt.Sprintf("forbidden: %s", strings.Join(s, " ")))
}
2024-11-15 07:25:21 +00:00
func ForbiddenWithScopeL(scope uint32, ec ErrorCode,
l logx.Logger, filed []logx.LogField, s ...string) *LibError {
e := ForbiddenWithScope(scope, ec, s...)
l.WithCallerSkip(1).WithFields(filed...).Error(e.Error())
return e
}