exception/code.go

157 lines
3.9 KiB
Go
Raw Normal View History

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-11-05 00:57:23 +08:00
Value: code,
Message: message,
2023-11-05 01:15:16 +08:00
Reason: make(map[string][]*CodeReason),
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
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-11-05 00:57:23 +08:00
if nil == itemCode.Reason {
2023-11-05 01:15:16 +08:00
itemCode.Reason = make(map[string][]*CodeReason)
2023-11-05 00:57:23 +08:00
}
2023-11-05 01:15:16 +08:00
for lang, _ := range itemCode.Message {
if _, exist := itemCode.Reason[lang]; !exist {
itemCode.Reason[lang] = make([]*CodeReason, 0)
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 默认代表成功的状态码
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
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 {
if len(defaultMessage) > 0 && len(defaultMessage[0]) > 0 {
return defaultMessage[0]
}
2022-06-25 21:40:09 +08:00
// 无论是否开启 messageWithCode , 未知错误强行带 code
return fmt.Sprintf("未知错误【%v】", code)
2022-06-25 21:40:09 +08:00
}
if messageWithCode {
if code == defaultSuccessCode {
// 请求成功, 一直不带状态码后缀
2023-06-09 16:10:27 +08:00
return inputCodeInfo.Message[defaultLanguage]
}
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
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
}