From be993da6db6d46a129db99cbff368fca12fc7d4e 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 00:57:23 +0800 Subject: [PATCH] update code define --- abstrace.go | 4 ---- code.go | 67 +++++++++++++++++++++++++++------------------------- define.go | 6 ++--- exception.go | 8 ------- 4 files changed, 38 insertions(+), 47 deletions(-) diff --git a/abstrace.go b/abstrace.go index fb67c37..087499e 100644 --- a/abstrace.go +++ b/abstrace.go @@ -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 diff --git a/code.go b/code.go index b26f0f8..5e78d30 100644 --- a/code.go +++ b/code.go @@ -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 -} diff --git a/define.go b/define.go index 330a463..3348b69 100644 --- a/define.go +++ b/define.go @@ -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"` // 解决步骤. 语言 => 解决步骤 } diff --git a/exception.go b/exception.go index d868655..644d9e7 100644 --- a/exception.go +++ b/exception.go @@ -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 }