解决自定义异常消息无法透出问题
解决自定义异常消息无法透出问题
This commit is contained in:
parent
16920b714d
commit
f674702ad2
11
code.go
11
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]
|
||||
}
|
||||
|
14
exception.go
14
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, "")
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user