优化异常定义
This commit is contained in:
parent
290d1a7bd3
commit
a038f90ed8
@ -3,7 +3,6 @@
|
||||
<component name="ProjectTasksOptions">
|
||||
<enabled-global>
|
||||
<option value="go fmt" />
|
||||
<option value="goimports" />
|
||||
</enabled-global>
|
||||
</component>
|
||||
</project>
|
21
code.go
21
code.go
@ -18,12 +18,12 @@ var (
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 20:53 2022/6/25
|
||||
codeTable map[interface{}]map[string]string
|
||||
codeTable map[interface{}]Code
|
||||
)
|
||||
|
||||
func init() {
|
||||
// 规避没调用 InitCodeTable 导致空指针
|
||||
codeTable = make(map[interface{}]map[string]string)
|
||||
codeTable = make(map[interface{}]Code)
|
||||
}
|
||||
|
||||
// InitCodeTable 初始化码表, 同时只指定代表业务成功的状态码以及使用的语言
|
||||
@ -32,7 +32,14 @@ func init() {
|
||||
//
|
||||
// Date : 20:55 2022/6/25
|
||||
func InitCodeTable(table map[interface{}]map[string]string, convertDefaultSuccessCode interface{}, convertDefaultLanguage string) {
|
||||
codeTable = table
|
||||
for code, message := range table {
|
||||
codeTable[code] = Code{
|
||||
Value: code,
|
||||
Message: message,
|
||||
Reason: "",
|
||||
Solution: map[string]interface{}{},
|
||||
}
|
||||
}
|
||||
if nil == convertDefaultSuccessCode {
|
||||
convertDefaultSuccessCode = 0
|
||||
}
|
||||
@ -69,7 +76,7 @@ func MessageWithoutCode() {
|
||||
//
|
||||
// Date : 21:16 2022/6/25
|
||||
func getMessage(code interface{}) string {
|
||||
message, exist := codeTable[code]
|
||||
inputCodeInfo, exist := codeTable[code]
|
||||
if !exist {
|
||||
// 无论是否开启 messageWithCode , 未知错误强行带 code
|
||||
return fmt.Sprintf("未知错误【%v】", code)
|
||||
@ -77,11 +84,11 @@ func getMessage(code interface{}) string {
|
||||
if messageWithCode {
|
||||
if code == defaultSuccessCode {
|
||||
// 请求成功, 一直不带状态码后缀
|
||||
return message[defaultLanguage]
|
||||
return inputCodeInfo.Message[defaultLanguage]
|
||||
}
|
||||
return fmt.Sprintf(message[defaultLanguage]+"【%v】", code)
|
||||
return fmt.Sprintf(inputCodeInfo.Message[defaultLanguage]+"【%v】", code)
|
||||
}
|
||||
return message[defaultLanguage]
|
||||
return inputCodeInfo.Message[defaultLanguage]
|
||||
}
|
||||
|
||||
// GetMessage 获取消息信息
|
||||
|
20
define.go
Normal file
20
define.go
Normal file
@ -0,0 +1,20 @@
|
||||
// Package exception ...
|
||||
//
|
||||
// Description : exception ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 2023-06-09 15:54
|
||||
package exception
|
||||
|
||||
// Code 状态码的结构
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 15:55 2023/6/9
|
||||
type Code struct {
|
||||
Value interface{} `json:"value"` // 状态码的值
|
||||
Message map[string]string `json:"message"` // 状态码对应的文案(key -> 语言 , value -> 对应语言的描述)
|
||||
Reason string `json:"reason"` // 产生此错误码的原因描述
|
||||
Solution map[string]interface{} `json:"solution"` // 解决方案
|
||||
}
|
Loading…
Reference in New Issue
Block a user