升级exception, 简化逻辑
This commit is contained in:
79
exception.go
79
exception.go
@@ -18,32 +18,27 @@ import (
|
||||
//
|
||||
// Date *: 21:09 2022/6/25
|
||||
type Exception struct {
|
||||
code interface{}
|
||||
message string
|
||||
httpCode int
|
||||
data interface{}
|
||||
code any
|
||||
message string
|
||||
data map[string]any
|
||||
}
|
||||
|
||||
func (e *Exception) Error() string {
|
||||
return e.GetMessage()
|
||||
return e.Message()
|
||||
}
|
||||
|
||||
func (e *Exception) GetCode() interface{} {
|
||||
func (e *Exception) Code() any {
|
||||
return e.code
|
||||
}
|
||||
|
||||
func (e *Exception) GetMessage() string {
|
||||
func (e *Exception) Message() string {
|
||||
return e.message
|
||||
}
|
||||
|
||||
func (e *Exception) GetData() interface{} {
|
||||
func (e *Exception) Data() map[string]any {
|
||||
return e.data
|
||||
}
|
||||
|
||||
func (e *Exception) GetHttpCode() int {
|
||||
return e.httpCode
|
||||
}
|
||||
|
||||
func (e *Exception) ToError() error {
|
||||
if nil == e {
|
||||
return nil
|
||||
@@ -56,8 +51,8 @@ func (e *Exception) ToError() error {
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 00:39 2023/9/28
|
||||
func (e *Exception) IsCode(inputCode interface{}) bool {
|
||||
return fmt.Sprintf("%v", inputCode) == fmt.Sprintf("%v", e.GetCode())
|
||||
func (e *Exception) IsCode(inputCode any) bool {
|
||||
return fmt.Sprintf("%v", inputCode) == fmt.Sprintf("%v", e.Code())
|
||||
}
|
||||
|
||||
// NewWithCode 仅使用错误码实例化异常
|
||||
@@ -65,17 +60,8 @@ func (e *Exception) IsCode(inputCode interface{}) bool {
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 21:22 2022/6/25
|
||||
func NewWithCode(code interface{}) IException {
|
||||
return New(code, defaultHttpCode, nil, "")
|
||||
}
|
||||
|
||||
// NewWithCodeAndHttpCode 使用 code + http_code 获取实例
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 21:25 2022/6/25
|
||||
func NewWithCodeAndHttpCode(code interface{}, httpCode int) IException {
|
||||
return New(code, httpCode, nil, "")
|
||||
func NewWithCode(code any) IException {
|
||||
return New(code, map[string]any{}, "")
|
||||
}
|
||||
|
||||
// NewWithCodeAndData 使用 code + data 获取异常实例
|
||||
@@ -83,8 +69,8 @@ func NewWithCodeAndHttpCode(code interface{}, httpCode int) IException {
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 21:28 2022/6/25
|
||||
func NewWithCodeAndData(code interface{}, data interface{}) IException {
|
||||
return New(code, defaultHttpCode, data, "")
|
||||
func NewWithCodeAndData(code any, data map[string]any) IException {
|
||||
return New(code, data, "")
|
||||
}
|
||||
|
||||
// New 获取异常实例
|
||||
@@ -92,16 +78,15 @@ func NewWithCodeAndData(code interface{}, data interface{}) IException {
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 21:28 2022/6/25
|
||||
func New(code interface{}, httpCode int, data interface{}, defaultMessage ...string) IException {
|
||||
func New(code any, data map[string]any, defaultMessage ...string) IException {
|
||||
if nil == data {
|
||||
// 保证数据结构的一致性, 同时避免后续使用出现空指针
|
||||
data = map[string]interface{}{}
|
||||
data = map[string]any{}
|
||||
}
|
||||
return &Exception{
|
||||
code: code,
|
||||
message: getMessage(code, defaultMessage...),
|
||||
httpCode: httpCode,
|
||||
data: data,
|
||||
code: code,
|
||||
message: GetMessage(code, defaultMessage...),
|
||||
data: data,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -110,12 +95,12 @@ func New(code interface{}, httpCode int, data interface{}, defaultMessage ...str
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 21:36 2022/6/25
|
||||
func NewFromError(code interface{}, err error) IException {
|
||||
func NewFromError(code any, err error) IException {
|
||||
if nil == err {
|
||||
return nil
|
||||
}
|
||||
return New(code, defaultHttpCode, map[string]interface{}{
|
||||
"error": err.Error(),
|
||||
return New(code, map[string]any{
|
||||
"err_msg": err.Error(),
|
||||
}, err.Error())
|
||||
}
|
||||
|
||||
@@ -124,32 +109,20 @@ func NewFromError(code interface{}, err error) IException {
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 22:25 2023/2/11
|
||||
func NewFromMessage(code interface{}, message string) IException {
|
||||
func NewFromMessage(code any, message string) IException {
|
||||
if len(message) == 0 {
|
||||
message = getMessage(code, fmt.Sprintf("%v -> 未知异常信息", code))
|
||||
message = GetMessage(code, "unknown error")
|
||||
}
|
||||
return NewFromError(code, errors.New(message))
|
||||
}
|
||||
|
||||
// ToError 转换成内置error
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 21:37 2022/6/25
|
||||
func ToError(e *Exception) error {
|
||||
if nil == e {
|
||||
return nil
|
||||
}
|
||||
return errors.New(e.GetMessage())
|
||||
}
|
||||
|
||||
// IsSuccess 判断一个异常是否为成功
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 21:34 2022/6/25
|
||||
func IsSuccess(e *Exception) bool {
|
||||
return nil == e || e.GetCode() == defaultSuccessCode
|
||||
return nil == e || e.IsCode(defaultSuccessCode)
|
||||
}
|
||||
|
||||
// NewSuccess 代表Success的异常
|
||||
@@ -157,6 +130,6 @@ func IsSuccess(e *Exception) bool {
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 22:35 2022/6/25
|
||||
func NewSuccess(data interface{}) IException {
|
||||
return New(defaultSuccessCode, defaultHttpCode, data, "")
|
||||
func NewSuccess(data map[string]any) IException {
|
||||
return New(defaultSuccessCode, data, "")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user