From f674702ad28d68027f14ce4482f8d2fbf0abfd32 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Sat, 19 Aug 2023 10:42:37 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A7=A3=E5=86=B3=E8=87=AA=E5=AE=9A=E4=B9=89?= =?UTF-8?q?=E5=BC=82=E5=B8=B8=E6=B6=88=E6=81=AF=E6=97=A0=E6=B3=95=E9=80=8F?= =?UTF-8?q?=E5=87=BA=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 解决自定义异常消息无法透出问题 --- code.go | 11 +++++++---- exception.go | 14 +++++++------- 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/code.go b/code.go index 012e717..b26f0f8 100644 --- a/code.go +++ b/code.go @@ -102,9 +102,12 @@ func MessageWithoutCode() { // Author : go_developer@163.com<白茶清欢> // // Date : 21:16 2022/6/25 -func getMessage(code interface{}) string { +func getMessage(code interface{}, defaultMessage ...string) string { inputCodeInfo, exist := codeTable[code] if !exist { + if len(defaultMessage) > 0 && len(defaultMessage[0]) > 0 { + return defaultMessage[0] + } // 无论是否开启 messageWithCode , 未知错误强行带 code return fmt.Sprintf("未知错误【%v】", code) } @@ -123,8 +126,8 @@ func getMessage(code interface{}) string { // Author : go_developer@163.com<白茶清欢> // // Date : 12:58 2022/6/26 -func GetMessage(code interface{}) string { - return getMessage(code) +func GetMessage(code interface{}, defaultMessage ...string) string { + return getMessage(code, defaultMessage...) } // GetCodeList ... @@ -153,7 +156,7 @@ func GetCodeTable() map[interface{}]Code { func getReason(code interface{}) string { inputCodeInfo, exist := codeTable[code] if !exist { - return getMessage(code) + return getMessage(code, "") } return inputCodeInfo.Reason[defaultLanguage] } diff --git a/exception.go b/exception.go index fc9af23..b52b2a3 100644 --- a/exception.go +++ b/exception.go @@ -65,7 +65,7 @@ func (e *Exception) ToError() error { // // Date : 21:22 2022/6/25 func NewWithCode(code interface{}) IException { - return New(code, defaultHttpCode, nil) + return New(code, defaultHttpCode, nil, "") } // NewWithCodeAndHttpCode 使用 code + http_code 获取实例 @@ -74,7 +74,7 @@ func NewWithCode(code interface{}) IException { // // Date : 21:25 2022/6/25 func NewWithCodeAndHttpCode(code interface{}, httpCode int) IException { - return New(code, httpCode, nil) + return New(code, httpCode, nil, "") } // NewWithCodeAndData 使用 code + data 获取异常实例 @@ -83,7 +83,7 @@ func NewWithCodeAndHttpCode(code interface{}, httpCode int) IException { // // Date : 21:28 2022/6/25 func NewWithCodeAndData(code interface{}, data interface{}) IException { - return New(code, defaultHttpCode, data) + return New(code, defaultHttpCode, data, "") } // New 获取异常实例 @@ -91,14 +91,14 @@ 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{}) IException { +func New(code interface{}, httpCode int, data interface{}, defaultMessage ...string) IException { if nil == data { // 保证数据结构的一致性, 同时避免后续使用出现空指针 data = map[string]interface{}{} } return &Exception{ code: code, - message: getMessage(code), + message: getMessage(code, defaultMessage...), httpCode: httpCode, data: data, } @@ -115,7 +115,7 @@ func NewFromError(code interface{}, err error) IException { } return New(code, defaultHttpCode, map[string]interface{}{ "error": err.Error(), - }) + }, err.Error()) } // NewFromMessage 从 code message 生成exception @@ -157,5 +157,5 @@ func IsSuccess(e *Exception) bool { // // Date : 22:35 2022/6/25 func NewSuccess(data interface{}) IException { - return New(defaultSuccessCode, defaultHttpCode, data) + return New(defaultSuccessCode, defaultHttpCode, data, "") }