升级异常接口约束

This commit is contained in:
zhangdeman001 2023-06-09 18:12:14 +08:00
parent 62d3723566
commit 16920b714d
3 changed files with 62 additions and 1 deletions

View File

@ -19,6 +19,10 @@ type IException interface {
GetCode() interface{} GetCode() interface{}
// GetMessage *获取错误信息 // GetMessage *获取错误信息
GetMessage() string GetMessage() string
// GetRealReason 获取真实失败原因
GetRealReason() string
// GetSolution 获取解决方案
GetSolution() map[string]interface{}
// GetData 获取异常时的返回数据 // GetData 获取异常时的返回数据
GetData() interface{} GetData() interface{}
// GetHttpCode *获取当前异常要返回的http状态码, 不设置则 默认 200 // GetHttpCode *获取当前异常要返回的http状态码, 不设置则 默认 200

51
code.go
View File

@ -19,11 +19,13 @@ var (
// //
// Date : 20:53 2022/6/25 // Date : 20:53 2022/6/25
codeTable map[interface{}]Code codeTable map[interface{}]Code
codeList []Code
) )
func init() { func init() {
// 规避没调用 InitCodeTable 导致空指针 // 规避没调用 InitCodeTable 导致空指针
codeTable = make(map[interface{}]Code) codeTable = make(map[interface{}]Code)
codeList = make([]Code, 0)
} }
// InitCodeTableWithMessage 初始化码表, 同时只指定代表业务成功的状态码以及使用的语言 // InitCodeTableWithMessage 初始化码表, 同时只指定代表业务成功的状态码以及使用的语言
@ -33,12 +35,14 @@ func init() {
// Date : 20:55 2022/6/25 // Date : 20:55 2022/6/25
func InitCodeTableWithMessage(table map[interface{}]map[string]string, convertDefaultSuccessCode interface{}, convertDefaultLanguage string) { func InitCodeTableWithMessage(table map[interface{}]map[string]string, convertDefaultSuccessCode interface{}, convertDefaultLanguage string) {
for code, message := range table { for code, message := range table {
codeTable[code] = Code{ c := Code{
Value: code, Value: code,
Message: message, Message: message,
Reason: message, Reason: message,
Solution: map[string]interface{}{}, Solution: map[string]interface{}{},
} }
codeTable[code] = c
codeList = append(codeList, c)
} }
if nil == convertDefaultSuccessCode { if nil == convertDefaultSuccessCode {
convertDefaultSuccessCode = 0 convertDefaultSuccessCode = 0
@ -56,6 +60,7 @@ func InitCodeTableWithMessage(table map[interface{}]map[string]string, convertDe
// //
// Date : 16:19 2023/6/9 // Date : 16:19 2023/6/9
func InitCodeTableWithCodeList(list []Code, convertDefaultSuccessCode interface{}, convertDefaultLanguage string) { func InitCodeTableWithCodeList(list []Code, convertDefaultSuccessCode interface{}, convertDefaultLanguage string) {
codeList = list
for _, itemCode := range list { for _, itemCode := range list {
if itemCode.Reason == nil || len(itemCode.Reason) == 0 { if itemCode.Reason == nil || len(itemCode.Reason) == 0 {
itemCode.Reason = itemCode.Message itemCode.Reason = itemCode.Message
@ -121,3 +126,47 @@ func getMessage(code interface{}) string {
func GetMessage(code interface{}) string { func GetMessage(code interface{}) string {
return getMessage(code) return getMessage(code)
} }
// 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 {
return getMessage(code)
}
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
}

View File

@ -36,6 +36,14 @@ func (e *Exception) GetMessage() string {
return e.message return e.message
} }
func (e *Exception) GetRealReason() string {
return getReason(e.GetCode())
}
func (e *Exception) GetSolution() map[string]interface{} {
return getSolution(e.GetCode())
}
func (e *Exception) GetData() interface{} { func (e *Exception) GetData() interface{} {
return e.data return e.data
} }