支持打印堆栈

This commit is contained in:
2024-09-30 16:13:43 +08:00
parent e8561a060c
commit 1e7f84ed84
4 changed files with 55 additions and 26 deletions

View File

@ -10,6 +10,9 @@ package exception
import (
"errors"
"fmt"
"runtime"
"strconv"
"strings"
)
// Exception 异常接口的具体实现
@ -18,17 +21,22 @@ import (
//
// Date *: 21:09 2022/6/25
type Exception struct {
code interface{}
code any
message string
httpCode int
data interface{}
data any
stack string
}
func (e *Exception) Error() string {
return e.GetMessage()
}
func (e *Exception) GetCode() interface{} {
func (e *Exception) GetStack() string {
return e.stack
}
func (e *Exception) GetCode() any {
return e.code
}
@ -36,7 +44,7 @@ func (e *Exception) GetMessage() string {
return e.message
}
func (e *Exception) GetData() interface{} {
func (e *Exception) GetData() any {
return e.data
}
@ -56,7 +64,7 @@ func (e *Exception) ToError() error {
// Author : go_developer@163.com<白茶清欢>
//
// Date : 00:39 2023/9/28
func (e *Exception) IsCode(inputCode interface{}) bool {
func (e *Exception) IsCode(inputCode any) bool {
return fmt.Sprintf("%v", inputCode) == fmt.Sprintf("%v", e.GetCode())
}
@ -65,7 +73,7 @@ func (e *Exception) IsCode(inputCode interface{}) bool {
// Author : go_developer@163.com<白茶清欢>
//
// Date : 21:22 2022/6/25
func NewWithCode(code interface{}) IException {
func NewWithCode(code any) IException {
return New(code, defaultHttpCode, nil, "")
}
@ -74,7 +82,7 @@ func NewWithCode(code interface{}) IException {
// Author : go_developer@163.com<白茶清欢>
//
// Date : 21:25 2022/6/25
func NewWithCodeAndHttpCode(code interface{}, httpCode int) IException {
func NewWithCodeAndHttpCode(code any, httpCode int) IException {
return New(code, httpCode, nil, "")
}
@ -83,7 +91,7 @@ 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 {
func NewWithCodeAndData(code any, data map[string]any) IException {
return New(code, defaultHttpCode, data, "")
}
@ -92,16 +100,35 @@ 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, httpCode int, data any, defaultMessage ...string) IException {
if nil == data {
// 保证数据结构的一致性, 同时避免后续使用出现空指针
data = map[string]interface{}{}
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
}
}
return &Exception{
code: code,
message: getMessage(code, defaultMessage...),
httpCode: httpCode,
data: data,
stack: sb.String(),
}
}
@ -110,11 +137,11 @@ 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{}{
return New(code, defaultHttpCode, map[string]any{
"error": err.Error(),
}, err.Error())
}
@ -124,7 +151,7 @@ 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))
}
@ -157,6 +184,6 @@ func IsSuccess(e *Exception) bool {
// Author : go_developer@163.com<白茶清欢>
//
// Date : 22:35 2022/6/25
func NewSuccess(data interface{}) IException {
func NewSuccess(data any) IException {
return New(defaultSuccessCode, defaultHttpCode, data, "")
}