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
|
2022-06-25 21:40:09 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
// 规避没调用 InitCodeTable 导致空指针
|
2023-06-09 16:10:27 +08:00
|
|
|
codeTable = make(map[interface{}]Code)
|
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 {
|
|
|
|
codeTable[code] = Code{
|
|
|
|
Value: code,
|
|
|
|
Message: message,
|
|
|
|
Reason: "",
|
|
|
|
Solution: map[string]interface{}{},
|
|
|
|
}
|
|
|
|
}
|
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) {
|
|
|
|
for _, itemCode := range list {
|
|
|
|
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{}) string {
|
2023-06-09 16:10:27 +08:00
|
|
|
inputCodeInfo, exist := codeTable[code]
|
2022-06-25 21:40:09 +08:00
|
|
|
if !exist {
|
|
|
|
// 无论是否开启 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
|
|
|
|
func GetMessage(code interface{}) string {
|
|
|
|
return getMessage(code)
|
|
|
|
}
|