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"
|
2024-09-30 16:13:43 +08:00
|
|
|
"runtime"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
2023-02-11 22:29:35 +08:00
|
|
|
)
|
2022-06-25 21:40:09 +08:00
|
|
|
|
|
|
|
// Exception 异常接口的具体实现
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date *: 21:09 2022/6/25
|
|
|
|
type Exception struct {
|
2024-09-30 16:13:43 +08:00
|
|
|
code any
|
2022-06-25 21:40:09 +08:00
|
|
|
message string
|
|
|
|
httpCode int
|
2024-09-30 16:13:43 +08:00
|
|
|
data any
|
|
|
|
stack string
|
2022-06-25 21:40:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (e *Exception) Error() string {
|
|
|
|
return e.GetMessage()
|
|
|
|
}
|
|
|
|
|
2024-09-30 16:13:43 +08:00
|
|
|
func (e *Exception) GetStack() string {
|
|
|
|
return e.stack
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *Exception) GetCode() any {
|
2022-06-25 21:40:09 +08:00
|
|
|
return e.code
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *Exception) GetMessage() string {
|
|
|
|
return e.message
|
|
|
|
}
|
|
|
|
|
2024-09-30 16:13:43 +08:00
|
|
|
func (e *Exception) GetData() any {
|
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
|
2024-09-30 16:13:43 +08:00
|
|
|
func (e *Exception) IsCode(inputCode any) bool {
|
2023-09-28 00:40:57 +08:00
|
|
|
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
|
2024-09-30 16:13:43 +08:00
|
|
|
func NewWithCode(code any) 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
|
2024-09-30 16:13:43 +08:00
|
|
|
func NewWithCodeAndHttpCode(code any, 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
|
2024-09-30 16:13:43 +08:00
|
|
|
func NewWithCodeAndData(code any, data map[string]any) 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
|
2024-09-30 16:13:43 +08:00
|
|
|
func New(code any, httpCode int, data any, defaultMessage ...string) IException {
|
2022-06-25 21:40:09 +08:00
|
|
|
if nil == data {
|
|
|
|
// 保证数据结构的一致性, 同时避免后续使用出现空指针
|
2024-09-30 16:13:43 +08:00
|
|
|
data = map[string]any{}
|
|
|
|
}
|
|
|
|
pcs := make([]uintptr, 128)
|
|
|
|
n := runtime.Callers(2, pcs)
|
|
|
|
frames := runtime.CallersFrames(pcs[:n])
|
|
|
|
var sb strings.Builder
|
|
|
|
for {
|
|
|
|
frame, more := frames.Next()
|
|
|
|
sb.WriteString(frame.Function)
|
|
|
|
sb.WriteByte('\n')
|
|
|
|
sb.WriteByte('\t')
|
|
|
|
sb.WriteString(frame.File)
|
|
|
|
sb.WriteByte(':')
|
|
|
|
sb.WriteString(strconv.Itoa(frame.Line))
|
|
|
|
sb.WriteByte('\n')
|
|
|
|
|
|
|
|
if !more {
|
|
|
|
break
|
|
|
|
}
|
2022-06-25 21:40:09 +08:00
|
|
|
}
|
|
|
|
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,
|
2024-09-30 16:13:43 +08:00
|
|
|
stack: sb.String(),
|
2022-06-25 21:40:09 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewFromError 从error转换为Exception
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 21:36 2022/6/25
|
2024-09-30 16:13:43 +08:00
|
|
|
func NewFromError(code any, err error) IException {
|
2022-06-25 21:40:09 +08:00
|
|
|
if nil == err {
|
|
|
|
return nil
|
|
|
|
}
|
2024-09-30 16:13:43 +08:00
|
|
|
return New(code, defaultHttpCode, map[string]any{
|
2022-06-25 21:40:09 +08:00
|
|
|
"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
|
2024-09-30 16:13:43 +08:00
|
|
|
func NewFromMessage(code any, message string) IException {
|
2023-02-11 22:29:35 +08:00
|
|
|
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
|
2024-09-30 16:13:43 +08:00
|
|
|
func NewSuccess(data any) IException {
|
2023-08-19 10:42:37 +08:00
|
|
|
return New(defaultSuccessCode, defaultHttpCode, data, "")
|
2022-06-25 22:36:11 +08:00
|
|
|
}
|