2022-06-25 21:40:09 +08:00
|
|
|
// Package exception ...
|
|
|
|
//
|
|
|
|
// Description : exception ...
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 2022-06-25 21:03
|
|
|
|
package exception
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2022-07-03 00:12:02 +08:00
|
|
|
// CodeTable 响应状态码和文案的映射表, 文案信心 语言类型 => 对应语言的描述
|
2022-06-25 21:40:09 +08:00
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 20:53 2022/6/25
|
2023-06-09 16:10:27 +08:00
|
|
|
codeTable map[interface{}]Code
|
2023-06-09 18:12:14 +08:00
|
|
|
codeList []Code
|
2022-06-25 21:40:09 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
// 规避没调用 InitCodeTable 导致空指针
|
2023-06-09 16:10:27 +08:00
|
|
|
codeTable = make(map[interface{}]Code)
|
2023-06-09 18:12:14 +08:00
|
|
|
codeList = make([]Code, 0)
|
2022-06-25 21:40:09 +08:00
|
|
|
}
|
|
|
|
|
2023-06-09 16:20:14 +08:00
|
|
|
// InitCodeTableWithMessage 初始化码表, 同时只指定代表业务成功的状态码以及使用的语言
|
2022-06-25 21:40:09 +08:00
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 20:55 2022/6/25
|
2023-06-09 16:20:14 +08:00
|
|
|
func InitCodeTableWithMessage(table map[interface{}]map[string]string, convertDefaultSuccessCode interface{}, convertDefaultLanguage string) {
|
2023-06-09 16:10:27 +08:00
|
|
|
for code, message := range table {
|
2023-06-09 18:12:14 +08:00
|
|
|
c := Code{
|
2023-06-09 16:10:27 +08:00
|
|
|
Value: code,
|
|
|
|
Message: message,
|
2023-06-09 16:22:55 +08:00
|
|
|
Reason: message,
|
2023-06-09 16:10:27 +08:00
|
|
|
Solution: map[string]interface{}{},
|
|
|
|
}
|
2023-06-09 18:12:14 +08:00
|
|
|
codeTable[code] = c
|
|
|
|
codeList = append(codeList, c)
|
2023-06-09 16:10:27 +08:00
|
|
|
}
|
2022-06-25 21:40:09 +08:00
|
|
|
if nil == convertDefaultSuccessCode {
|
|
|
|
convertDefaultSuccessCode = 0
|
|
|
|
}
|
2022-07-03 00:12:02 +08:00
|
|
|
if len(convertDefaultLanguage) == 0 {
|
|
|
|
convertDefaultLanguage = defaultLanguage
|
|
|
|
}
|
2022-06-25 21:40:09 +08:00
|
|
|
defaultSuccessCode = convertDefaultSuccessCode
|
2022-07-03 00:12:02 +08:00
|
|
|
defaultLanguage = convertDefaultLanguage
|
2022-06-25 21:40:09 +08:00
|
|
|
}
|
|
|
|
|
2023-06-09 16:20:14 +08:00
|
|
|
// InitCodeTableWithCodeList 按照code列表初始化
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 16:19 2023/6/9
|
|
|
|
func InitCodeTableWithCodeList(list []Code, convertDefaultSuccessCode interface{}, convertDefaultLanguage string) {
|
2023-06-09 18:12:14 +08:00
|
|
|
codeList = list
|
2023-06-09 16:20:14 +08:00
|
|
|
for _, itemCode := range list {
|
2023-06-09 16:24:15 +08:00
|
|
|
if itemCode.Reason == nil || len(itemCode.Reason) == 0 {
|
|
|
|
itemCode.Reason = itemCode.Message
|
|
|
|
}
|
2023-06-09 16:20:14 +08:00
|
|
|
codeTable[itemCode.Value] = itemCode
|
|
|
|
}
|
|
|
|
if nil == convertDefaultSuccessCode {
|
|
|
|
convertDefaultSuccessCode = 0
|
|
|
|
}
|
|
|
|
if len(convertDefaultLanguage) == 0 {
|
|
|
|
convertDefaultLanguage = defaultLanguage
|
|
|
|
}
|
|
|
|
defaultSuccessCode = convertDefaultSuccessCode
|
|
|
|
defaultLanguage = convertDefaultLanguage
|
|
|
|
}
|
|
|
|
|
2022-06-25 21:40:09 +08:00
|
|
|
var (
|
|
|
|
// messageWithCode 自动在message文案后追加状态码
|
|
|
|
messageWithCode = true
|
|
|
|
// defaultHttpCode 默认的http状态码
|
|
|
|
defaultHttpCode = http.StatusOK
|
|
|
|
// defaultSuccessCode 默认代表成功的状态码
|
|
|
|
defaultSuccessCode interface{}
|
2022-07-03 00:12:02 +08:00
|
|
|
// defaultLanguage 默认的语言
|
|
|
|
defaultLanguage = "zh"
|
2022-06-25 21:40:09 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// MessageWithoutCode 关闭在文案后追加状态码
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 21:15 2022/6/25
|
|
|
|
func MessageWithoutCode() {
|
|
|
|
messageWithCode = false
|
|
|
|
}
|
|
|
|
|
|
|
|
// getMessage 根据code获取文案
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 21:16 2022/6/25
|
2023-08-19 10:42:37 +08:00
|
|
|
func getMessage(code interface{}, defaultMessage ...string) string {
|
2023-06-09 16:10:27 +08:00
|
|
|
inputCodeInfo, exist := codeTable[code]
|
2022-06-25 21:40:09 +08:00
|
|
|
if !exist {
|
2023-08-19 10:42:37 +08:00
|
|
|
if len(defaultMessage) > 0 && len(defaultMessage[0]) > 0 {
|
|
|
|
return defaultMessage[0]
|
|
|
|
}
|
2022-06-25 21:40:09 +08:00
|
|
|
// 无论是否开启 messageWithCode , 未知错误强行带 code
|
2023-02-11 23:35:43 +08:00
|
|
|
return fmt.Sprintf("未知错误【%v】", code)
|
2022-06-25 21:40:09 +08:00
|
|
|
}
|
|
|
|
if messageWithCode {
|
2022-07-03 01:12:32 +08:00
|
|
|
if code == defaultSuccessCode {
|
|
|
|
// 请求成功, 一直不带状态码后缀
|
2023-06-09 16:10:27 +08:00
|
|
|
return inputCodeInfo.Message[defaultLanguage]
|
2022-07-03 01:12:32 +08:00
|
|
|
}
|
2023-06-09 16:10:27 +08:00
|
|
|
return fmt.Sprintf(inputCodeInfo.Message[defaultLanguage]+"【%v】", code)
|
2022-06-25 21:40:09 +08:00
|
|
|
}
|
2023-06-09 16:10:27 +08:00
|
|
|
return inputCodeInfo.Message[defaultLanguage]
|
2022-06-25 21:40:09 +08:00
|
|
|
}
|
2022-06-26 13:00:13 +08:00
|
|
|
|
|
|
|
// GetMessage 获取消息信息
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 12:58 2022/6/26
|
2023-08-19 10:42:37 +08:00
|
|
|
func GetMessage(code interface{}, defaultMessage ...string) string {
|
|
|
|
return getMessage(code, defaultMessage...)
|
2022-06-26 13:00:13 +08:00
|
|
|
}
|
2023-06-09 18:12:14 +08:00
|
|
|
|
|
|
|
// GetCodeList ...
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 18:04 2023/6/9
|
|
|
|
func GetCodeList() []Code {
|
|
|
|
return codeList
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetCodeTable ...
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 18:05 2023/6/9
|
|
|
|
func GetCodeTable() map[interface{}]Code {
|
|
|
|
return codeTable
|
|
|
|
}
|
|
|
|
|
|
|
|
// getReason 获取真实失败原因描述
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 18:09 2023/6/9
|
|
|
|
func getReason(code interface{}) string {
|
|
|
|
inputCodeInfo, exist := codeTable[code]
|
|
|
|
if !exist {
|
2023-08-19 10:42:37 +08:00
|
|
|
return getMessage(code, "")
|
2023-06-09 18:12:14 +08:00
|
|
|
}
|
|
|
|
return inputCodeInfo.Reason[defaultLanguage]
|
|
|
|
}
|
|
|
|
|
|
|
|
// getSolution 获取解决方案
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 18:10 2023/6/9
|
|
|
|
func getSolution(code interface{}) map[string]interface{} {
|
|
|
|
inputCodeInfo, exist := codeTable[code]
|
|
|
|
if !exist {
|
|
|
|
return map[string]interface{}{}
|
|
|
|
}
|
|
|
|
return inputCodeInfo.Solution
|
|
|
|
}
|