From c293dcfa7f35c13655d27ad34dbad0cf901e8de5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Sun, 5 Nov 2023 01:15:16 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96code=20reason?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- abstrace.go | 4 ++-- code.go | 32 +++++--------------------------- define.go | 10 +++++----- 3 files changed, 12 insertions(+), 34 deletions(-) diff --git a/abstrace.go b/abstrace.go index 087499e..a266b4a 100644 --- a/abstrace.go +++ b/abstrace.go @@ -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 diff --git a/code.go b/code.go index 5e78d30..b8c3c35 100644 --- a/code.go +++ b/code.go @@ -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 { diff --git a/define.go b/define.go index 3348b69..dd80387 100644 --- a/define.go +++ b/define.go @@ -13,9 +13,9 @@ package exception // // Date : 15:55 2023/6/9 type Code struct { - Value interface{} `json:"value"` // 状态码的值 - Message map[string]string `json:"message"` // 状态码对应的文案(key -> 语言 , value -> 对应语言的描述) - Reason []*CodeReason `json:"reason"` // 产生此错误码的原因描述(key -> 语言 , value -> 对应语言的描述) + Value interface{} `json:"value"` // 状态码的值 + Message map[string]string `json:"message"` // 状态码对应的文案(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"` // 解决步骤. 语言 => 解决步骤 }