升级exception, 简化逻辑 #1

Merged
zhangdeman merged 3 commits from feature/upgrade_exception into master 2025-05-10 19:37:46 +08:00
4 changed files with 53 additions and 106 deletions

View File

@ -15,14 +15,12 @@ package exception
type IException interface {
// Error 兼容 go 内置 error
Error() string
// GetCode 获取错误码
GetCode() any
// GetMessage *获取错误信息
GetMessage() string
// GetData 获取异常时的返回数据
GetData() any
// GetHttpCode 获取当前异常要返回的http状态码, 不设置则 默认 200
GetHttpCode() int
// Code 获取错误码
Code() any
// Message 获取错误信息
Message() string
// Data 获取异常时的返回数据
Data() map[string]any
// ToError 转换为内置error类型
ToError() error
// IsCode 是否为指定code

54
code.go
View File

@ -9,7 +9,6 @@ package exception
import (
"fmt"
"net/http"
)
var (
@ -38,7 +37,8 @@ func InitCodeTableWithMessage(table map[any]map[string]string, convertDefaultSuc
c := Code{
Value: code,
Message: message,
Reason: make([]*CodeReason, 0),
Reason: make(map[string]string),
Resolve: make(map[string]string),
}
codeTable[code] = c
@ -63,19 +63,7 @@ func InitCodeTableWithCodeList(list []Code, convertDefaultSuccessCode any, conve
codeList = list
for _, itemCode := range list {
if nil == itemCode.Reason {
itemCode.Reason = make([]*CodeReason, 0)
}
for lang, _ := range itemCode.Message {
for _, itemReason := range itemCode.Reason {
if _, exist := itemReason.Reason[lang]; !exist {
continue
}
if _, exist := itemReason.Solution[lang]; !exist {
itemReason.Solution[lang] = make([]string, 0)
}
}
itemCode.Reason = make(map[string]string)
}
codeTable[itemCode.Value] = itemCode
@ -93,8 +81,6 @@ func InitCodeTableWithCodeList(list []Code, convertDefaultSuccessCode any, conve
var (
// messageWithCode 自动在message文案后追加状态码
messageWithCode = true
// defaultHttpCode 默认的http状态码
defaultHttpCode = http.StatusOK
// defaultSuccessCode 默认代表成功的状态码
defaultSuccessCode any = 0
// defaultLanguage 默认的语言
@ -110,42 +96,34 @@ func MessageWithoutCode() {
messageWithCode = false
}
// getMessage 根据code获取文案
// GetMessage 根据code获取文案
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 21:16 2022/6/25
func getMessage(code any, defaultMessage ...string) string {
inputCodeInfo, exist := codeTable[code]
if !exist {
if fmt.Sprintf("%v", code) == fmt.Sprintf("%v", defaultSuccessCode) {
return "请求成功"
}
func GetMessage(code any, defaultMessage ...string) string {
var (
inputCodeInfo Code
exist bool
)
if inputCodeInfo, exist = codeTable[code]; !exist {
// 错误码不存在
if len(defaultMessage) > 0 && len(defaultMessage[0]) > 0 {
return defaultMessage[0]
}
// 无论是否开启 messageWithCode , 未知错误强行带 code
return fmt.Sprintf("未知错误【%v】", code)
return fmt.Sprintf("unknown error【%v】", code)
}
if code == defaultSuccessCode {
// 请求成功, 一直不带状态码后缀
return inputCodeInfo.Message[defaultLanguage]
}
if messageWithCode {
if code == defaultSuccessCode {
// 请求成功, 一直不带状态码后缀
return inputCodeInfo.Message[defaultLanguage]
}
return fmt.Sprintf(inputCodeInfo.Message[defaultLanguage]+"【%v】", code)
}
return inputCodeInfo.Message[defaultLanguage]
}
// GetMessage 获取消息信息
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 12:58 2022/6/26
func GetMessage(code any, defaultMessage ...string) string {
return getMessage(code, defaultMessage...)
}
// GetCodeList ...
//
// Author : go_developer@163.com<白茶清欢>

View File

@ -15,15 +15,6 @@ package exception
type Code struct {
Value any `json:"value"` // 状态码的值
Message map[string]string `json:"message"` // 状态码对应的文案(key -> 语言 , value -> 对应语言的描述)
Reason []*CodeReason `json:"reason"` // 产生此错误码的原因描述(key -> 语言 , value -> 对应语言的原因列表)
}
// CodeReason 错误码的原因
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 00:31 2023/11/5
type CodeReason struct {
Reason map[string]string `json:"reason"` // 错误原因: 语言 => 原因
Solution map[string][]string `json:"solution"` // 解决步骤. 语言 => 解决步骤
Reason map[string]string `json:"reason"` // 产生此错误码的原因描述(key -> 语言 , value -> 对应语言的原因列表)
Resolve map[string]string `json:"resolve"` // 解决此错误码的方案描述(key -> 语言 , value -> 对应语言的方案列表)
}

View File

@ -8,6 +8,7 @@
package exception
import (
"encoding/json"
"errors"
"fmt"
"runtime"
@ -21,37 +22,38 @@ import (
//
// Date *: 21:09 2022/6/25
type Exception struct {
code any
message string
httpCode int
data any
stack string
code any
message string
data map[string]any
stack string
}
func (e *Exception) Error() string {
return e.GetMessage()
mapData := map[string]any{
"code": e.Code(),
"msg": e.Message(),
"data": e.Data(),
}
byteData, _ := json.Marshal(mapData)
return string(byteData)
}
func (e *Exception) GetStack() string {
return e.stack
}
func (e *Exception) GetCode() any {
func (e *Exception) Code() any {
return e.code
}
func (e *Exception) GetMessage() string {
func (e *Exception) Message() string {
return e.message
}
func (e *Exception) GetData() any {
func (e *Exception) Data() map[string]any {
return e.data
}
func (e *Exception) GetHttpCode() int {
return e.httpCode
}
func (e *Exception) ToError() error {
if nil == e {
return nil
@ -65,7 +67,7 @@ func (e *Exception) ToError() error {
//
// Date : 00:39 2023/9/28
func (e *Exception) IsCode(inputCode any) bool {
return fmt.Sprintf("%v", inputCode) == fmt.Sprintf("%v", e.GetCode())
return fmt.Sprintf("%v", inputCode) == fmt.Sprintf("%v", e.Code())
}
// NewWithCode 仅使用错误码实例化异常
@ -74,16 +76,7 @@ func (e *Exception) IsCode(inputCode any) bool {
//
// Date : 21:22 2022/6/25
func NewWithCode(code any) IException {
return New(code, defaultHttpCode, nil, "")
}
// NewWithCodeAndHttpCode 使用 code + http_code 获取实例
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 21:25 2022/6/25
func NewWithCodeAndHttpCode(code any, httpCode int) IException {
return New(code, httpCode, nil, "")
return New(code, map[string]any{}, "")
}
// NewWithCodeAndData 使用 code + data 获取异常实例
@ -92,7 +85,7 @@ func NewWithCodeAndHttpCode(code any, httpCode int) IException {
//
// Date : 21:28 2022/6/25
func NewWithCodeAndData(code any, data map[string]any) IException {
return New(code, defaultHttpCode, data, "")
return New(code, data, "")
}
// New 获取异常实例
@ -100,7 +93,7 @@ func NewWithCodeAndData(code any, data map[string]any) IException {
// Author : go_developer@163.com<白茶清欢>
//
// Date : 21:28 2022/6/25
func New(code any, httpCode int, data any, defaultMessage ...string) IException {
func New(code any, data map[string]any, defaultMessage ...string) IException {
if nil == data {
// 保证数据结构的一致性, 同时避免后续使用出现空指针
data = map[string]any{}
@ -124,11 +117,10 @@ func New(code any, httpCode int, data any, defaultMessage ...string) IException
}
}
return &Exception{
code: code,
message: getMessage(code, defaultMessage...),
httpCode: httpCode,
data: data,
stack: sb.String(),
code: code,
message: GetMessage(code, defaultMessage...),
data: data,
stack: sb.String(),
}
}
@ -141,8 +133,8 @@ func NewFromError(code any, err error) IException {
if nil == err {
return nil
}
return New(code, defaultHttpCode, map[string]any{
"error": err.Error(),
return New(code, map[string]any{
"err_msg": err.Error(),
}, err.Error())
}
@ -153,30 +145,18 @@ func NewFromError(code any, err error) IException {
// Date : 22:25 2023/2/11
func NewFromMessage(code any, message string) IException {
if len(message) == 0 {
message = getMessage(code, fmt.Sprintf("%v -> 未知异常信息", code))
message = GetMessage(code, "unknown error")
}
return NewFromError(code, errors.New(message))
}
// ToError 转换成内置error
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 21:37 2022/6/25
func ToError(e *Exception) error {
if nil == e {
return nil
}
return errors.New(e.GetMessage())
}
// IsSuccess 判断一个异常是否为成功
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 21:34 2022/6/25
func IsSuccess(e *Exception) bool {
return nil == e || e.GetCode() == defaultSuccessCode
return nil == e || e.IsCode(defaultSuccessCode)
}
// NewSuccess 代表Success的异常
@ -184,6 +164,6 @@ func IsSuccess(e *Exception) bool {
// Author : go_developer@163.com<白茶清欢>
//
// Date : 22:35 2022/6/25
func NewSuccess(data any) IException {
return New(defaultSuccessCode, defaultHttpCode, data, "")
func NewSuccess(data map[string]any) IException {
return New(defaultSuccessCode, data, "")
}