update code define

This commit is contained in:
白茶清欢 2023-11-05 00:57:23 +08:00
parent cd1454d34d
commit be993da6db
4 changed files with 38 additions and 47 deletions

View File

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

67
code.go
View File

@ -36,11 +36,14 @@ func init() {
func InitCodeTableWithMessage(table map[interface{}]map[string]string, convertDefaultSuccessCode interface{}, convertDefaultLanguage string) {
for code, message := range table {
c := Code{
Value: code,
Message: message,
Reason: message,
Solution: map[string]interface{}{},
Value: code,
Message: message,
Reason: []*CodeReason{&CodeReason{
Reason: message,
Solution: map[string][]string{},
}},
}
codeTable[code] = c
codeList = append(codeList, c)
}
@ -62,8 +65,34 @@ func InitCodeTableWithMessage(table map[interface{}]map[string]string, convertDe
func InitCodeTableWithCodeList(list []Code, convertDefaultSuccessCode interface{}, convertDefaultLanguage string) {
codeList = list
for _, itemCode := range list {
if itemCode.Reason == nil || len(itemCode.Reason) == 0 {
itemCode.Reason = itemCode.Message
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
}
@ -147,29 +176,3 @@ func GetCodeList() []Code {
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

@ -15,7 +15,7 @@ package exception
type Code struct {
Value interface{} `json:"value"` // 状态码的值
Message map[string]string `json:"message"` // 状态码对应的文案(key -> 语言 , value -> 对应语言的描述)
Reason *CodeReason `json:"reason"` // 产生此错误码的原因描述(key -> 语言 , value -> 对应语言的描述)
Reason []*CodeReason `json:"reason"` // 产生此错误码的原因描述(key -> 语言 , value -> 对应语言的描述)
}
// CodeReason 错误码的原因
@ -24,6 +24,6 @@ type Code struct {
//
// Date : 00:31 2023/11/5
type CodeReason struct {
Reason string `json:"reason"` // 错误原因
Solution []string `json:"solution"` // 解决步骤
Reason map[string]string `json:"reason"` // 错误原因: 语言 => 原因
Solution map[string][]string `json:"solution"` // 解决步骤. 语言 => 解决步骤
}

View File

@ -36,14 +36,6 @@ func (e *Exception) GetMessage() string {
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{} {
return e.data
}