From 1e7f84ed8465b6627b969d70c3eb4c82a14bb58b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Mon, 30 Sep 2024 16:13:43 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E6=8C=81=E6=89=93=E5=8D=B0=E5=A0=86?= =?UTF-8?q?=E6=A0=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- abstrace.go | 8 +++++--- code.go | 16 +++++++-------- define.go | 2 +- exception.go | 55 +++++++++++++++++++++++++++++++++++++++------------- 4 files changed, 55 insertions(+), 26 deletions(-) diff --git a/abstrace.go b/abstrace.go index a266b4a..94efdef 100644 --- a/abstrace.go +++ b/abstrace.go @@ -16,15 +16,17 @@ type IException interface { // Error 兼容 go 内置 error Error() string // GetCode 获取错误码 - GetCode() interface{} + GetCode() any // GetMessage *获取错误信息 GetMessage() string // GetData 获取异常时的返回数据 - GetData() interface{} + GetData() any // GetHttpCode 获取当前异常要返回的http状态码, 不设置则 默认 200 GetHttpCode() int // ToError 转换为内置error类型 ToError() error // IsCode 是否为指定code - IsCode(code interface{}) bool + IsCode(code any) bool + // GetStack 获取调用堆栈 + GetStack() string } diff --git a/code.go b/code.go index 1c0f6e0..d3232bd 100644 --- a/code.go +++ b/code.go @@ -18,13 +18,13 @@ var ( // Author : go_developer@163.com<白茶清欢> // // Date : 20:53 2022/6/25 - codeTable map[interface{}]Code + codeTable map[any]Code codeList []Code ) func init() { // 规避没调用 InitCodeTable 导致空指针 - codeTable = make(map[interface{}]Code) + codeTable = make(map[any]Code) codeList = make([]Code, 0) } @@ -33,7 +33,7 @@ func init() { // Author : go_developer@163.com<白茶清欢> // // Date : 20:55 2022/6/25 -func InitCodeTableWithMessage(table map[interface{}]map[string]string, convertDefaultSuccessCode interface{}, convertDefaultLanguage string) { +func InitCodeTableWithMessage(table map[any]map[string]string, convertDefaultSuccessCode any, convertDefaultLanguage string) { for code, message := range table { c := Code{ Value: code, @@ -59,7 +59,7 @@ func InitCodeTableWithMessage(table map[interface{}]map[string]string, convertDe // Author : go_developer@163.com<白茶清欢> // // Date : 16:19 2023/6/9 -func InitCodeTableWithCodeList(list []Code, convertDefaultSuccessCode interface{}, convertDefaultLanguage string) { +func InitCodeTableWithCodeList(list []Code, convertDefaultSuccessCode any, convertDefaultLanguage string) { codeList = list for _, itemCode := range list { if nil == itemCode.Reason { @@ -96,7 +96,7 @@ var ( // defaultHttpCode 默认的http状态码 defaultHttpCode = http.StatusOK // defaultSuccessCode 默认代表成功的状态码 - defaultSuccessCode interface{} + defaultSuccessCode any // defaultLanguage 默认的语言 defaultLanguage = "zh" ) @@ -115,7 +115,7 @@ func MessageWithoutCode() { // Author : go_developer@163.com<白茶清欢> // // Date : 21:16 2022/6/25 -func getMessage(code interface{}, defaultMessage ...string) string { +func getMessage(code any, defaultMessage ...string) string { inputCodeInfo, exist := codeTable[code] if !exist { if len(defaultMessage) > 0 && len(defaultMessage[0]) > 0 { @@ -139,7 +139,7 @@ func getMessage(code interface{}, defaultMessage ...string) string { // Author : go_developer@163.com<白茶清欢> // // Date : 12:58 2022/6/26 -func GetMessage(code interface{}, defaultMessage ...string) string { +func GetMessage(code any, defaultMessage ...string) string { return getMessage(code, defaultMessage...) } @@ -157,6 +157,6 @@ func GetCodeList() []Code { // Author : go_developer@163.com<白茶清欢> // // Date : 18:05 2023/6/9 -func GetCodeTable() map[interface{}]Code { +func GetCodeTable() map[any]Code { return codeTable } diff --git a/define.go b/define.go index f22d366..304e0e9 100644 --- a/define.go +++ b/define.go @@ -13,7 +13,7 @@ package exception // // Date : 15:55 2023/6/9 type Code struct { - Value interface{} `json:"value"` // 状态码的值 + Value any `json:"value"` // 状态码的值 Message map[string]string `json:"message"` // 状态码对应的文案(key -> 语言 , value -> 对应语言的描述) Reason []*CodeReason `json:"reason"` // 产生此错误码的原因描述(key -> 语言 , value -> 对应语言的原因列表) } diff --git a/exception.go b/exception.go index 644d9e7..ff30e16 100644 --- a/exception.go +++ b/exception.go @@ -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, "") }