Compare commits
0
Commits
master
..
be3556e23e
No files matched your search
+1
-3
@@ -20,11 +20,9 @@ type IException interface {
|
|||||||
// Message 获取错误信息
|
// Message 获取错误信息
|
||||||
Message() string
|
Message() string
|
||||||
// Data 获取异常时的返回数据
|
// Data 获取异常时的返回数据
|
||||||
Data() any
|
Data() map[string]any
|
||||||
// ToError 转换为内置error类型
|
// ToError 转换为内置error类型
|
||||||
ToError() error
|
ToError() error
|
||||||
// IsCode 是否为指定code
|
// IsCode 是否为指定code
|
||||||
IsCode(code any) bool
|
IsCode(code any) bool
|
||||||
// GetStack 获取调用堆栈
|
|
||||||
GetStack() string
|
|
||||||
}
|
}
|
||||||
@@ -82,7 +82,7 @@ var (
|
|||||||
// messageWithCode 自动在message文案后追加状态码
|
// messageWithCode 自动在message文案后追加状态码
|
||||||
messageWithCode = true
|
messageWithCode = true
|
||||||
// defaultSuccessCode 默认代表成功的状态码
|
// defaultSuccessCode 默认代表成功的状态码
|
||||||
defaultSuccessCode any = 0
|
defaultSuccessCode any
|
||||||
// defaultLanguage 默认的语言
|
// defaultLanguage 默认的语言
|
||||||
defaultLanguage = "zh"
|
defaultLanguage = "zh"
|
||||||
)
|
)
|
||||||
@@ -102,12 +102,6 @@ func MessageWithoutCode() {
|
|||||||
//
|
//
|
||||||
// Date : 21:16 2022/6/25
|
// Date : 21:16 2022/6/25
|
||||||
func GetMessage(code any, defaultMessage ...string) string {
|
func GetMessage(code any, defaultMessage ...string) string {
|
||||||
if nil == code {
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
if eCode, ok := code.(Code); ok {
|
|
||||||
code = eCode.Value
|
|
||||||
}
|
|
||||||
var (
|
var (
|
||||||
inputCodeInfo Code
|
inputCodeInfo Code
|
||||||
exist bool
|
exist bool
|
||||||
|
|||||||
+5
-39
@@ -8,12 +8,8 @@
|
|||||||
package exception
|
package exception
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"runtime"
|
|
||||||
"strconv"
|
|
||||||
"strings"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Exception 异常接口的具体实现
|
// Exception 异常接口的具体实现
|
||||||
@@ -24,22 +20,11 @@ import (
|
|||||||
type Exception struct {
|
type Exception struct {
|
||||||
code any
|
code any
|
||||||
message string
|
message string
|
||||||
data any
|
data map[string]any
|
||||||
stack string
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *Exception) Error() string {
|
func (e *Exception) Error() string {
|
||||||
mapData := map[string]any{
|
return e.Message()
|
||||||
"code": e.Code(),
|
|
||||||
"msg": e.Message(),
|
|
||||||
"data": e.Data(),
|
|
||||||
}
|
|
||||||
byteData, _ := json.Marshal(mapData)
|
|
||||||
return string(byteData)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (e *Exception) GetStack() string {
|
|
||||||
return e.stack
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *Exception) Code() any {
|
func (e *Exception) Code() any {
|
||||||
@@ -50,7 +35,7 @@ func (e *Exception) Message() string {
|
|||||||
return e.message
|
return e.message
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *Exception) Data() any {
|
func (e *Exception) Data() map[string]any {
|
||||||
return e.data
|
return e.data
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -93,34 +78,15 @@ func NewWithCodeAndData(code any, data map[string]any) IException {
|
|||||||
// Author : go_developer@163.com<白茶清欢>
|
// Author : go_developer@163.com<白茶清欢>
|
||||||
//
|
//
|
||||||
// Date : 21:28 2022/6/25
|
// Date : 21:28 2022/6/25
|
||||||
func New(code any, data any, defaultMessage ...string) IException {
|
func New(code any, data map[string]any, defaultMessage ...string) IException {
|
||||||
if nil == data {
|
if nil == data {
|
||||||
// 保证数据结构的一致性, 同时避免后续使用出现空指针
|
// 保证数据结构的一致性, 同时避免后续使用出现空指针
|
||||||
data = map[string]any{}
|
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{
|
return &Exception{
|
||||||
code: code,
|
code: code,
|
||||||
message: GetMessage(code, defaultMessage...),
|
message: GetMessage(code, defaultMessage...),
|
||||||
data: data,
|
data: data,
|
||||||
stack: sb.String(),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -164,6 +130,6 @@ func IsSuccess(e *Exception) bool {
|
|||||||
// Author : go_developer@163.com<白茶清欢>
|
// Author : go_developer@163.com<白茶清欢>
|
||||||
//
|
//
|
||||||
// Date : 22:35 2022/6/25
|
// Date : 22:35 2022/6/25
|
||||||
func NewSuccess(data any) IException {
|
func NewSuccess(data map[string]any) IException {
|
||||||
return New(defaultSuccessCode, data, "")
|
return New(defaultSuccessCode, data, "")
|
||||||
}
|
}
|
||||||