升级异常接口约束
This commit is contained in:
parent
62d3723566
commit
16920b714d
@ -19,6 +19,10 @@ type IException interface {
|
||||
GetCode() interface{}
|
||||
// GetMessage *获取错误信息
|
||||
GetMessage() string
|
||||
// GetRealReason 获取真实失败原因
|
||||
GetRealReason() string
|
||||
// GetSolution 获取解决方案
|
||||
GetSolution() map[string]interface{}
|
||||
// GetData 获取异常时的返回数据
|
||||
GetData() interface{}
|
||||
// GetHttpCode *获取当前异常要返回的http状态码, 不设置则 默认 200
|
||||
|
51
code.go
51
code.go
@ -19,11 +19,13 @@ var (
|
||||
//
|
||||
// Date : 20:53 2022/6/25
|
||||
codeTable map[interface{}]Code
|
||||
codeList []Code
|
||||
)
|
||||
|
||||
func init() {
|
||||
// 规避没调用 InitCodeTable 导致空指针
|
||||
codeTable = make(map[interface{}]Code)
|
||||
codeList = make([]Code, 0)
|
||||
}
|
||||
|
||||
// InitCodeTableWithMessage 初始化码表, 同时只指定代表业务成功的状态码以及使用的语言
|
||||
@ -33,12 +35,14 @@ func init() {
|
||||
// Date : 20:55 2022/6/25
|
||||
func InitCodeTableWithMessage(table map[interface{}]map[string]string, convertDefaultSuccessCode interface{}, convertDefaultLanguage string) {
|
||||
for code, message := range table {
|
||||
codeTable[code] = Code{
|
||||
c := Code{
|
||||
Value: code,
|
||||
Message: message,
|
||||
Reason: message,
|
||||
Solution: map[string]interface{}{},
|
||||
}
|
||||
codeTable[code] = c
|
||||
codeList = append(codeList, c)
|
||||
}
|
||||
if nil == convertDefaultSuccessCode {
|
||||
convertDefaultSuccessCode = 0
|
||||
@ -56,6 +60,7 @@ func InitCodeTableWithMessage(table map[interface{}]map[string]string, convertDe
|
||||
//
|
||||
// Date : 16:19 2023/6/9
|
||||
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
|
||||
@ -121,3 +126,47 @@ func getMessage(code interface{}) string {
|
||||
func GetMessage(code interface{}) string {
|
||||
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
|
||||
}
|
||||
|
@ -36,6 +36,14 @@ 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
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user