179 lines
4.4 KiB
Go
179 lines
4.4 KiB
Go
// Package exception ...
|
|
//
|
|
// Description : exception ...
|
|
//
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
//
|
|
// Date : 2022-06-25 21:03
|
|
package exception
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
)
|
|
|
|
var (
|
|
// CodeTable 响应状态码和文案的映射表, 文案信心 语言类型 => 对应语言的描述
|
|
//
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
//
|
|
// Date : 20:53 2022/6/25
|
|
codeTable map[interface{}]Code
|
|
codeList []Code
|
|
)
|
|
|
|
func init() {
|
|
// 规避没调用 InitCodeTable 导致空指针
|
|
codeTable = make(map[interface{}]Code)
|
|
codeList = make([]Code, 0)
|
|
}
|
|
|
|
// InitCodeTableWithMessage 初始化码表, 同时只指定代表业务成功的状态码以及使用的语言
|
|
//
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
//
|
|
// Date : 20:55 2022/6/25
|
|
func InitCodeTableWithMessage(table map[interface{}]map[string]string, convertDefaultSuccessCode interface{}, convertDefaultLanguage string) {
|
|
for code, message := range table {
|
|
c := Code{
|
|
Value: code,
|
|
Message: message,
|
|
Reason: []*CodeReason{&CodeReason{
|
|
Reason: message,
|
|
Solution: map[string][]string{},
|
|
}},
|
|
}
|
|
|
|
codeTable[code] = c
|
|
codeList = append(codeList, c)
|
|
}
|
|
if nil == convertDefaultSuccessCode {
|
|
convertDefaultSuccessCode = 0
|
|
}
|
|
if len(convertDefaultLanguage) == 0 {
|
|
convertDefaultLanguage = defaultLanguage
|
|
}
|
|
defaultSuccessCode = convertDefaultSuccessCode
|
|
defaultLanguage = convertDefaultLanguage
|
|
}
|
|
|
|
// InitCodeTableWithCodeList 按照code列表初始化
|
|
//
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
//
|
|
// Date : 16:19 2023/6/9
|
|
func InitCodeTableWithCodeList(list []Code, convertDefaultSuccessCode interface{}, convertDefaultLanguage string) {
|
|
codeList = list
|
|
for _, itemCode := range list {
|
|
if nil == itemCode.Reason {
|
|
itemCode.Reason = []*CodeReason{
|
|
{
|
|
Reason: map[string]string{},
|
|
Solution: map[string][]string{},
|
|
},
|
|
}
|
|
}
|
|
if nil == itemCode.Reason {
|
|
itemCode.Reason = make([]*CodeReason, 0)
|
|
}
|
|
|
|
for _, itemReason := range itemCode.Reason {
|
|
if nil == itemReason.Solution {
|
|
itemReason.Solution = make(map[string][]string)
|
|
}
|
|
}
|
|
|
|
for _, itemReason := range itemCode.Reason {
|
|
for lang, _ := range itemReason.Reason {
|
|
if _, exist := itemReason.Solution[lang]; !exist {
|
|
itemReason.Solution[lang] = make([]string, 0)
|
|
}
|
|
if nil == itemReason.Solution[lang] {
|
|
itemReason.Solution[lang] = make([]string, 0)
|
|
}
|
|
}
|
|
|
|
}
|
|
codeTable[itemCode.Value] = itemCode
|
|
}
|
|
if nil == convertDefaultSuccessCode {
|
|
convertDefaultSuccessCode = 0
|
|
}
|
|
if len(convertDefaultLanguage) == 0 {
|
|
convertDefaultLanguage = defaultLanguage
|
|
}
|
|
defaultSuccessCode = convertDefaultSuccessCode
|
|
defaultLanguage = convertDefaultLanguage
|
|
}
|
|
|
|
var (
|
|
// messageWithCode 自动在message文案后追加状态码
|
|
messageWithCode = true
|
|
// defaultHttpCode 默认的http状态码
|
|
defaultHttpCode = http.StatusOK
|
|
// defaultSuccessCode 默认代表成功的状态码
|
|
defaultSuccessCode interface{}
|
|
// defaultLanguage 默认的语言
|
|
defaultLanguage = "zh"
|
|
)
|
|
|
|
// 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 {
|
|
inputCodeInfo, exist := codeTable[code]
|
|
if !exist {
|
|
if len(defaultMessage) > 0 && len(defaultMessage[0]) > 0 {
|
|
return defaultMessage[0]
|
|
}
|
|
// 无论是否开启 messageWithCode , 未知错误强行带 code
|
|
return fmt.Sprintf("未知错误【%v】", code)
|
|
}
|
|
if messageWithCode {
|
|
if code == defaultSuccessCode {
|
|
// 请求成功, 一直不带状态码后缀
|
|
return inputCodeInfo.Message[defaultLanguage]
|
|
}
|
|
return fmt.Sprintf(inputCodeInfo.Message[defaultLanguage]+"【%v】", code)
|
|
}
|
|
return inputCodeInfo.Message[defaultLanguage]
|
|
}
|
|
|
|
// GetMessage 获取消息信息
|
|
//
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
//
|
|
// Date : 12:58 2022/6/26
|
|
func GetMessage(code interface{}, defaultMessage ...string) string {
|
|
return getMessage(code, defaultMessage...)
|
|
}
|
|
|
|
// 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
|
|
}
|