2022-06-25 21:40:09 +08:00
|
|
|
// Package exception ...
|
|
|
|
//
|
|
|
|
// Description : exception ...
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date *: 2022-06-25 21:04
|
|
|
|
package exception
|
|
|
|
|
2023-02-11 22:29:35 +08:00
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
)
|
2022-06-25 21:40:09 +08:00
|
|
|
|
|
|
|
// Exception 异常接口的具体实现
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date *: 21:09 2022/6/25
|
|
|
|
type Exception struct {
|
|
|
|
code interface{}
|
|
|
|
message string
|
|
|
|
httpCode int
|
2022-06-26 12:55:09 +08:00
|
|
|
data interface{}
|
2022-06-25 21:40:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (e *Exception) Error() string {
|
|
|
|
return e.GetMessage()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *Exception) GetCode() interface{} {
|
|
|
|
return e.code
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *Exception) GetMessage() string {
|
|
|
|
return e.message
|
|
|
|
}
|
|
|
|
|
2023-06-09 18:12:14 +08:00
|
|
|
func (e *Exception) GetRealReason() string {
|
|
|
|
return getReason(e.GetCode())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *Exception) GetSolution() map[string]interface{} {
|
|
|
|
return getSolution(e.GetCode())
|
|
|
|
}
|
|
|
|
|
2022-06-26 12:55:09 +08:00
|
|
|
func (e *Exception) GetData() interface{} {
|
2022-06-25 21:40:09 +08:00
|
|
|
return e.data
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *Exception) GetHttpCode() int {
|
|
|
|
return e.httpCode
|
|
|
|
}
|
|
|
|
|
2023-05-11 18:20:56 +08:00
|
|
|
func (e *Exception) ToError() error {
|
|
|
|
if nil == e {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return errors.New(e.Error())
|
|
|
|
}
|
|
|
|
|
2023-09-28 00:40:57 +08:00
|
|
|
// IsCode 判断是否为指定错误码
|
|
|
|
//
|
|
|
|
// 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())
|
|
|
|
}
|
|
|
|
|
2022-06-25 21:40:09 +08:00
|
|
|
// NewWithCode 仅使用错误码实例化异常
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 21:22 2022/6/25
|
2022-07-03 13:08:24 +08:00
|
|
|
func NewWithCode(code interface{}) IException {
|
2023-08-19 10:42:37 +08:00
|
|
|
return New(code, defaultHttpCode, nil, "")
|
2022-06-25 21:40:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewWithCodeAndHttpCode 使用 code + http_code 获取实例
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 21:25 2022/6/25
|
2022-07-03 13:08:24 +08:00
|
|
|
func NewWithCodeAndHttpCode(code interface{}, httpCode int) IException {
|
2023-08-19 10:42:37 +08:00
|
|
|
return New(code, httpCode, nil, "")
|
2022-06-25 21:40:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewWithCodeAndData 使用 code + data 获取异常实例
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 21:28 2022/6/25
|
2022-07-03 13:08:24 +08:00
|
|
|
func NewWithCodeAndData(code interface{}, data interface{}) IException {
|
2023-08-19 10:42:37 +08:00
|
|
|
return New(code, defaultHttpCode, data, "")
|
2022-06-25 21:40:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// New 获取异常实例
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 21:28 2022/6/25
|
2023-08-19 10:42:37 +08:00
|
|
|
func New(code interface{}, httpCode int, data interface{}, defaultMessage ...string) IException {
|
2022-06-25 21:40:09 +08:00
|
|
|
if nil == data {
|
|
|
|
// 保证数据结构的一致性, 同时避免后续使用出现空指针
|
|
|
|
data = map[string]interface{}{}
|
|
|
|
}
|
|
|
|
return &Exception{
|
|
|
|
code: code,
|
2023-08-19 10:42:37 +08:00
|
|
|
message: getMessage(code, defaultMessage...),
|
2022-06-25 21:40:09 +08:00
|
|
|
httpCode: httpCode,
|
|
|
|
data: data,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewFromError 从error转换为Exception
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 21:36 2022/6/25
|
2022-07-03 13:08:24 +08:00
|
|
|
func NewFromError(code interface{}, err error) IException {
|
2022-06-25 21:40:09 +08:00
|
|
|
if nil == err {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return New(code, defaultHttpCode, map[string]interface{}{
|
|
|
|
"error": err.Error(),
|
2023-08-19 10:42:37 +08:00
|
|
|
}, err.Error())
|
2022-06-25 21:40:09 +08:00
|
|
|
}
|
|
|
|
|
2023-02-11 22:29:35 +08:00
|
|
|
// NewFromMessage 从 code message 生成exception
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 22:25 2023/2/11
|
|
|
|
func NewFromMessage(code interface{}, message string) IException {
|
|
|
|
if len(message) == 0 {
|
2023-09-25 18:49:37 +08:00
|
|
|
message = getMessage(code, fmt.Sprintf("%v -> 未知异常信息", code))
|
2023-02-11 22:29:35 +08:00
|
|
|
}
|
|
|
|
return NewFromError(code, errors.New(message))
|
|
|
|
}
|
|
|
|
|
2022-06-25 21:40:09 +08:00
|
|
|
// 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
|
|
|
|
}
|
2022-06-25 22:36:11 +08:00
|
|
|
|
|
|
|
// NewSuccess 代表Success的异常
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 22:35 2022/6/25
|
2022-07-03 13:08:24 +08:00
|
|
|
func NewSuccess(data interface{}) IException {
|
2023-08-19 10:42:37 +08:00
|
|
|
return New(defaultSuccessCode, defaultHttpCode, data, "")
|
2022-06-25 22:36:11 +08:00
|
|
|
}
|