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
|
2024-09-30 16:13:43 +08:00
|
|
|
codeTable map[any]Code
|
2023-06-09 18:12:14 +08:00
|
|
|
codeList []Code
|
2022-06-25 21:40:09 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
// 规避没调用 InitCodeTable 导致空指针
|
2024-09-30 16:13:43 +08:00
|
|
|
codeTable = make(map[any]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
|
2024-09-30 16:13:43 +08:00
|
|
|
func InitCodeTableWithMessage(table map[any]map[string]string, convertDefaultSuccessCode any, 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-11-05 00:57:23 +08:00
|
|
|
Value: code,
|
|
|
|
Message: message,
|
2023-11-05 23:38:15 +08:00
|
|
|
Reason: make([]*CodeReason, 0),
|
2023-06-09 16:10:27 +08:00
|
|
|
}
|
2023-11-05 00:57:23 +08:00
|
|
|
|
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
|
2024-09-30 16:13:43 +08:00
|
|
|
func InitCodeTableWithCodeList(list []Code, convertDefaultSuccessCode any, 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-11-05 00:57:23 +08:00
|
|
|
if nil == itemCode.Reason {
|
2023-11-05 23:38:15 +08:00
|
|
|
itemCode.Reason = make([]*CodeReason, 0)
|
2023-11-05 00:57:23 +08:00
|
|
|
}
|
|
|
|
|
2023-11-05 01:15:16 +08:00
|
|
|
for lang, _ := range itemCode.Message {
|
2023-11-05 23:38:15 +08:00
|
|
|
for _, itemReason := range itemCode.Reason {
|
|
|
|
if _, exist := itemReason.Reason[lang]; !exist {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if _, exist := itemReason.Solution[lang]; !exist {
|
|
|
|
itemReason.Solution[lang] = make([]string, 0)
|
|
|
|
}
|
2023-11-05 00:57:23 +08:00
|
|
|
}
|
2023-11-05 23:38:15 +08:00
|
|
|
|
2023-11-05 00:57:23 +08:00
|
|
|
}
|
|
|
|
|
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 默认代表成功的状态码
|
2024-09-30 16:13:43 +08:00
|
|
|
defaultSuccessCode any
|
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
|
2024-09-30 16:13:43 +08:00
|
|
|
func getMessage(code any, 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
|
2024-09-30 16:13:43 +08:00
|
|
|
func GetMessage(code any, defaultMessage ...string) string {
|
2023-08-19 10:42:37 +08:00
|
|
|
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
|
2024-09-30 16:13:43 +08:00
|
|
|
func GetCodeTable() map[any]Code {
|
2023-06-09 18:12:14 +08:00
|
|
|
return codeTable
|
|
|
|
}
|