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
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
2022-06-26 12:57:36 +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 {
|
2022-06-25 21:40:09 +08:00
|
|
|
return New(code, httpCode, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
2022-06-25 21:40:09 +08:00
|
|
|
return New(code, defaultHttpCode, data)
|
|
|
|
}
|
|
|
|
|
|
|
|
// New 获取异常实例
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 21:28 2022/6/25
|
2022-07-03 13:08:24 +08:00
|
|
|
func New(code interface{}, httpCode int, data interface{}) IException {
|
2022-06-25 21:40:09 +08:00
|
|
|
if nil == data {
|
|
|
|
// 保证数据结构的一致性, 同时避免后续使用出现空指针
|
|
|
|
data = map[string]interface{}{}
|
|
|
|
}
|
|
|
|
return &Exception{
|
|
|
|
code: code,
|
|
|
|
message: getMessage(code),
|
|
|
|
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-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 {
|
|
|
|
message = fmt.Sprintf("%v", code)
|
|
|
|
}
|
|
|
|
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 {
|
2022-06-25 22:36:11 +08:00
|
|
|
return New(defaultSuccessCode, defaultHttpCode, data)
|
|
|
|
}
|