优化code reason

This commit is contained in:
白茶清欢 2023-11-05 01:15:16 +08:00
parent be993da6db
commit c293dcfa7f
3 changed files with 12 additions and 34 deletions

View File

@ -15,13 +15,13 @@ package exception
type IException interface {
// Error 兼容 go 内置 error
Error() string
// GetCode *获取错误码
// GetCode 获取错误码
GetCode() interface{}
// GetMessage *获取错误信息
GetMessage() string
// GetData 获取异常时的返回数据
GetData() interface{}
// GetHttpCode *获取当前异常要返回的http状态码, 不设置则 默认 200
// GetHttpCode 获取当前异常要返回的http状态码, 不设置则 默认 200
GetHttpCode() int
// ToError 转换为内置error类型
ToError() error

32
code.go
View File

@ -38,10 +38,7 @@ func InitCodeTableWithMessage(table map[interface{}]map[string]string, convertDe
c := Code{
Value: code,
Message: message,
Reason: []*CodeReason{&CodeReason{
Reason: message,
Solution: map[string][]string{},
}},
Reason: make(map[string][]*CodeReason),
}
codeTable[code] = c
@ -66,34 +63,15 @@ func InitCodeTableWithCodeList(list []Code, convertDefaultSuccessCode interface{
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)
itemCode.Reason = make(map[string][]*CodeReason)
}
for _, itemReason := range itemCode.Reason {
if nil == itemReason.Solution {
itemReason.Solution = make(map[string][]string)
for lang, _ := range itemCode.Message {
if _, exist := itemCode.Reason[lang]; !exist {
itemCode.Reason[lang] = make([]*CodeReason, 0)
}
}
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 {

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 map[string][]*CodeReason `json:"reason"` // 产生此错误码的原因描述(key -> 语言 , value -> 对应语言的原因列表)
}
// CodeReason 错误码的原因
@ -24,6 +24,6 @@ type Code struct {
//
// Date : 00:31 2023/11/5
type CodeReason struct {
Reason map[string]string `json:"reason"` // 错误原因: 语言 => 原因
Solution map[string][]string `json:"solution"` // 解决步骤. 语言 => 解决步骤
Reason string `json:"reason"` // 错误原因: 语言 => 原因
Solution []string `json:"solution"` // 解决步骤. 语言 => 解决步骤
}