升级exception, 简化逻辑
This commit is contained in:
parent
e8561a060c
commit
be3556e23e
16
abstrace.go
16
abstrace.go
@ -15,16 +15,14 @@ package exception
|
|||||||
type IException interface {
|
type IException interface {
|
||||||
// Error 兼容 go 内置 error
|
// Error 兼容 go 内置 error
|
||||||
Error() string
|
Error() string
|
||||||
// GetCode 获取错误码
|
// Code 获取错误码
|
||||||
GetCode() interface{}
|
Code() any
|
||||||
// GetMessage *获取错误信息
|
// Message 获取错误信息
|
||||||
GetMessage() string
|
Message() string
|
||||||
// GetData 获取异常时的返回数据
|
// Data 获取异常时的返回数据
|
||||||
GetData() interface{}
|
Data() map[string]any
|
||||||
// GetHttpCode 获取当前异常要返回的http状态码, 不设置则 默认 200
|
|
||||||
GetHttpCode() int
|
|
||||||
// ToError 转换为内置error类型
|
// ToError 转换为内置error类型
|
||||||
ToError() error
|
ToError() error
|
||||||
// IsCode 是否为指定code
|
// IsCode 是否为指定code
|
||||||
IsCode(code interface{}) bool
|
IsCode(code any) bool
|
||||||
}
|
}
|
||||||
|
63
code.go
63
code.go
@ -9,7 +9,6 @@ package exception
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@ -18,13 +17,13 @@ var (
|
|||||||
// Author : go_developer@163.com<白茶清欢>
|
// Author : go_developer@163.com<白茶清欢>
|
||||||
//
|
//
|
||||||
// Date : 20:53 2022/6/25
|
// Date : 20:53 2022/6/25
|
||||||
codeTable map[interface{}]Code
|
codeTable map[any]Code
|
||||||
codeList []Code
|
codeList []Code
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
// 规避没调用 InitCodeTable 导致空指针
|
// 规避没调用 InitCodeTable 导致空指针
|
||||||
codeTable = make(map[interface{}]Code)
|
codeTable = make(map[any]Code)
|
||||||
codeList = make([]Code, 0)
|
codeList = make([]Code, 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -33,12 +32,13 @@ func init() {
|
|||||||
// Author : go_developer@163.com<白茶清欢>
|
// Author : go_developer@163.com<白茶清欢>
|
||||||
//
|
//
|
||||||
// Date : 20:55 2022/6/25
|
// Date : 20:55 2022/6/25
|
||||||
func InitCodeTableWithMessage(table map[interface{}]map[string]string, convertDefaultSuccessCode interface{}, convertDefaultLanguage string) {
|
func InitCodeTableWithMessage(table map[any]map[string]string, convertDefaultSuccessCode any, convertDefaultLanguage string) {
|
||||||
for code, message := range table {
|
for code, message := range table {
|
||||||
c := Code{
|
c := Code{
|
||||||
Value: code,
|
Value: code,
|
||||||
Message: message,
|
Message: message,
|
||||||
Reason: make([]*CodeReason, 0),
|
Reason: make(map[string]string),
|
||||||
|
Resolve: make(map[string]string),
|
||||||
}
|
}
|
||||||
|
|
||||||
codeTable[code] = c
|
codeTable[code] = c
|
||||||
@ -59,23 +59,11 @@ func InitCodeTableWithMessage(table map[interface{}]map[string]string, convertDe
|
|||||||
// Author : go_developer@163.com<白茶清欢>
|
// Author : go_developer@163.com<白茶清欢>
|
||||||
//
|
//
|
||||||
// Date : 16:19 2023/6/9
|
// Date : 16:19 2023/6/9
|
||||||
func InitCodeTableWithCodeList(list []Code, convertDefaultSuccessCode interface{}, convertDefaultLanguage string) {
|
func InitCodeTableWithCodeList(list []Code, convertDefaultSuccessCode any, convertDefaultLanguage string) {
|
||||||
codeList = list
|
codeList = list
|
||||||
for _, itemCode := range list {
|
for _, itemCode := range list {
|
||||||
if nil == itemCode.Reason {
|
if nil == itemCode.Reason {
|
||||||
itemCode.Reason = make([]*CodeReason, 0)
|
itemCode.Reason = make(map[string]string)
|
||||||
}
|
|
||||||
|
|
||||||
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)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
codeTable[itemCode.Value] = itemCode
|
codeTable[itemCode.Value] = itemCode
|
||||||
@ -93,10 +81,8 @@ func InitCodeTableWithCodeList(list []Code, convertDefaultSuccessCode interface{
|
|||||||
var (
|
var (
|
||||||
// messageWithCode 自动在message文案后追加状态码
|
// messageWithCode 自动在message文案后追加状态码
|
||||||
messageWithCode = true
|
messageWithCode = true
|
||||||
// defaultHttpCode 默认的http状态码
|
|
||||||
defaultHttpCode = http.StatusOK
|
|
||||||
// defaultSuccessCode 默认代表成功的状态码
|
// defaultSuccessCode 默认代表成功的状态码
|
||||||
defaultSuccessCode interface{}
|
defaultSuccessCode any
|
||||||
// defaultLanguage 默认的语言
|
// defaultLanguage 默认的语言
|
||||||
defaultLanguage = "zh"
|
defaultLanguage = "zh"
|
||||||
)
|
)
|
||||||
@ -110,39 +96,34 @@ func MessageWithoutCode() {
|
|||||||
messageWithCode = false
|
messageWithCode = false
|
||||||
}
|
}
|
||||||
|
|
||||||
// getMessage 根据code获取文案
|
// GetMessage 根据code获取文案
|
||||||
//
|
//
|
||||||
// Author : go_developer@163.com<白茶清欢>
|
// Author : go_developer@163.com<白茶清欢>
|
||||||
//
|
//
|
||||||
// Date : 21:16 2022/6/25
|
// Date : 21:16 2022/6/25
|
||||||
func getMessage(code interface{}, defaultMessage ...string) string {
|
func GetMessage(code any, defaultMessage ...string) string {
|
||||||
inputCodeInfo, exist := codeTable[code]
|
var (
|
||||||
if !exist {
|
inputCodeInfo Code
|
||||||
|
exist bool
|
||||||
|
)
|
||||||
|
if inputCodeInfo, exist = codeTable[code]; !exist {
|
||||||
|
// 错误码不存在
|
||||||
if len(defaultMessage) > 0 && len(defaultMessage[0]) > 0 {
|
if len(defaultMessage) > 0 && len(defaultMessage[0]) > 0 {
|
||||||
return defaultMessage[0]
|
return defaultMessage[0]
|
||||||
}
|
}
|
||||||
// 无论是否开启 messageWithCode , 未知错误强行带 code
|
// 无论是否开启 messageWithCode , 未知错误强行带 code
|
||||||
return fmt.Sprintf("未知错误【%v】", code)
|
return fmt.Sprintf("unknown error【%v】", code)
|
||||||
|
}
|
||||||
|
if code == defaultSuccessCode {
|
||||||
|
// 请求成功, 一直不带状态码后缀
|
||||||
|
return inputCodeInfo.Message[defaultLanguage]
|
||||||
}
|
}
|
||||||
if messageWithCode {
|
if messageWithCode {
|
||||||
if code == defaultSuccessCode {
|
|
||||||
// 请求成功, 一直不带状态码后缀
|
|
||||||
return inputCodeInfo.Message[defaultLanguage]
|
|
||||||
}
|
|
||||||
return fmt.Sprintf(inputCodeInfo.Message[defaultLanguage]+"【%v】", code)
|
return fmt.Sprintf(inputCodeInfo.Message[defaultLanguage]+"【%v】", code)
|
||||||
}
|
}
|
||||||
return inputCodeInfo.Message[defaultLanguage]
|
return inputCodeInfo.Message[defaultLanguage]
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetMessage 获取消息信息
|
|
||||||
//
|
|
||||||
// Author : go_developer@163.com<白茶清欢>
|
|
||||||
//
|
|
||||||
// Date : 12:58 2022/6/26
|
|
||||||
func GetMessage(code interface{}, defaultMessage ...string) string {
|
|
||||||
return getMessage(code, defaultMessage...)
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetCodeList ...
|
// GetCodeList ...
|
||||||
//
|
//
|
||||||
// Author : go_developer@163.com<白茶清欢>
|
// Author : go_developer@163.com<白茶清欢>
|
||||||
@ -157,6 +138,6 @@ func GetCodeList() []Code {
|
|||||||
// Author : go_developer@163.com<白茶清欢>
|
// Author : go_developer@163.com<白茶清欢>
|
||||||
//
|
//
|
||||||
// Date : 18:05 2023/6/9
|
// Date : 18:05 2023/6/9
|
||||||
func GetCodeTable() map[interface{}]Code {
|
func GetCodeTable() map[any]Code {
|
||||||
return codeTable
|
return codeTable
|
||||||
}
|
}
|
||||||
|
15
define.go
15
define.go
@ -13,17 +13,8 @@ package exception
|
|||||||
//
|
//
|
||||||
// Date : 15:55 2023/6/9
|
// Date : 15:55 2023/6/9
|
||||||
type Code struct {
|
type Code struct {
|
||||||
Value interface{} `json:"value"` // 状态码的值
|
Value any `json:"value"` // 状态码的值
|
||||||
Message map[string]string `json:"message"` // 状态码对应的文案(key -> 语言 , value -> 对应语言的描述)
|
Message map[string]string `json:"message"` // 状态码对应的文案(key -> 语言 , value -> 对应语言的描述)
|
||||||
Reason []*CodeReason `json:"reason"` // 产生此错误码的原因描述(key -> 语言 , value -> 对应语言的原因列表)
|
Reason map[string]string `json:"reason"` // 产生此错误码的原因描述(key -> 语言 , value -> 对应语言的原因列表)
|
||||||
}
|
Resolve map[string]string `json:"resolve"` // 解决此错误码的方案描述(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"` // 解决步骤. 语言 => 解决步骤
|
|
||||||
}
|
}
|
||||||
|
79
exception.go
79
exception.go
@ -18,32 +18,27 @@ import (
|
|||||||
//
|
//
|
||||||
// Date *: 21:09 2022/6/25
|
// Date *: 21:09 2022/6/25
|
||||||
type Exception struct {
|
type Exception struct {
|
||||||
code interface{}
|
code any
|
||||||
message string
|
message string
|
||||||
httpCode int
|
data map[string]any
|
||||||
data interface{}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *Exception) Error() string {
|
func (e *Exception) Error() string {
|
||||||
return e.GetMessage()
|
return e.Message()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *Exception) GetCode() interface{} {
|
func (e *Exception) Code() any {
|
||||||
return e.code
|
return e.code
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *Exception) GetMessage() string {
|
func (e *Exception) Message() string {
|
||||||
return e.message
|
return e.message
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *Exception) GetData() interface{} {
|
func (e *Exception) Data() map[string]any {
|
||||||
return e.data
|
return e.data
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *Exception) GetHttpCode() int {
|
|
||||||
return e.httpCode
|
|
||||||
}
|
|
||||||
|
|
||||||
func (e *Exception) ToError() error {
|
func (e *Exception) ToError() error {
|
||||||
if nil == e {
|
if nil == e {
|
||||||
return nil
|
return nil
|
||||||
@ -56,8 +51,8 @@ func (e *Exception) ToError() error {
|
|||||||
// Author : go_developer@163.com<白茶清欢>
|
// Author : go_developer@163.com<白茶清欢>
|
||||||
//
|
//
|
||||||
// Date : 00:39 2023/9/28
|
// Date : 00:39 2023/9/28
|
||||||
func (e *Exception) IsCode(inputCode interface{}) bool {
|
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 仅使用错误码实例化异常
|
// NewWithCode 仅使用错误码实例化异常
|
||||||
@ -65,17 +60,8 @@ func (e *Exception) IsCode(inputCode interface{}) bool {
|
|||||||
// Author : go_developer@163.com<白茶清欢>
|
// Author : go_developer@163.com<白茶清欢>
|
||||||
//
|
//
|
||||||
// Date : 21:22 2022/6/25
|
// Date : 21:22 2022/6/25
|
||||||
func NewWithCode(code interface{}) IException {
|
func NewWithCode(code any) IException {
|
||||||
return New(code, defaultHttpCode, nil, "")
|
return New(code, map[string]any{}, "")
|
||||||
}
|
|
||||||
|
|
||||||
// NewWithCodeAndHttpCode 使用 code + http_code 获取实例
|
|
||||||
//
|
|
||||||
// Author : go_developer@163.com<白茶清欢>
|
|
||||||
//
|
|
||||||
// Date : 21:25 2022/6/25
|
|
||||||
func NewWithCodeAndHttpCode(code interface{}, httpCode int) IException {
|
|
||||||
return New(code, httpCode, nil, "")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewWithCodeAndData 使用 code + data 获取异常实例
|
// NewWithCodeAndData 使用 code + data 获取异常实例
|
||||||
@ -83,8 +69,8 @@ func NewWithCodeAndHttpCode(code interface{}, httpCode int) IException {
|
|||||||
// Author : go_developer@163.com<白茶清欢>
|
// Author : go_developer@163.com<白茶清欢>
|
||||||
//
|
//
|
||||||
// Date : 21:28 2022/6/25
|
// Date : 21:28 2022/6/25
|
||||||
func NewWithCodeAndData(code interface{}, data interface{}) IException {
|
func NewWithCodeAndData(code any, data map[string]any) IException {
|
||||||
return New(code, defaultHttpCode, data, "")
|
return New(code, data, "")
|
||||||
}
|
}
|
||||||
|
|
||||||
// New 获取异常实例
|
// New 获取异常实例
|
||||||
@ -92,16 +78,15 @@ func NewWithCodeAndData(code interface{}, data interface{}) IException {
|
|||||||
// Author : go_developer@163.com<白茶清欢>
|
// Author : go_developer@163.com<白茶清欢>
|
||||||
//
|
//
|
||||||
// Date : 21:28 2022/6/25
|
// Date : 21:28 2022/6/25
|
||||||
func New(code interface{}, httpCode int, data interface{}, defaultMessage ...string) IException {
|
func New(code any, data map[string]any, defaultMessage ...string) IException {
|
||||||
if nil == data {
|
if nil == data {
|
||||||
// 保证数据结构的一致性, 同时避免后续使用出现空指针
|
// 保证数据结构的一致性, 同时避免后续使用出现空指针
|
||||||
data = map[string]interface{}{}
|
data = map[string]any{}
|
||||||
}
|
}
|
||||||
return &Exception{
|
return &Exception{
|
||||||
code: code,
|
code: code,
|
||||||
message: getMessage(code, defaultMessage...),
|
message: GetMessage(code, defaultMessage...),
|
||||||
httpCode: httpCode,
|
data: data,
|
||||||
data: data,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -110,12 +95,12 @@ func New(code interface{}, httpCode int, data interface{}, defaultMessage ...str
|
|||||||
// Author : go_developer@163.com<白茶清欢>
|
// Author : go_developer@163.com<白茶清欢>
|
||||||
//
|
//
|
||||||
// Date : 21:36 2022/6/25
|
// Date : 21:36 2022/6/25
|
||||||
func NewFromError(code interface{}, err error) IException {
|
func NewFromError(code any, err error) IException {
|
||||||
if nil == err {
|
if nil == err {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
return New(code, defaultHttpCode, map[string]interface{}{
|
return New(code, map[string]any{
|
||||||
"error": err.Error(),
|
"err_msg": err.Error(),
|
||||||
}, err.Error())
|
}, err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -124,32 +109,20 @@ func NewFromError(code interface{}, err error) IException {
|
|||||||
// Author : go_developer@163.com<白茶清欢>
|
// Author : go_developer@163.com<白茶清欢>
|
||||||
//
|
//
|
||||||
// Date : 22:25 2023/2/11
|
// Date : 22:25 2023/2/11
|
||||||
func NewFromMessage(code interface{}, message string) IException {
|
func NewFromMessage(code any, message string) IException {
|
||||||
if len(message) == 0 {
|
if len(message) == 0 {
|
||||||
message = getMessage(code, fmt.Sprintf("%v -> 未知异常信息", code))
|
message = GetMessage(code, "unknown error")
|
||||||
}
|
}
|
||||||
return NewFromError(code, errors.New(message))
|
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 判断一个异常是否为成功
|
// IsSuccess 判断一个异常是否为成功
|
||||||
//
|
//
|
||||||
// Author : go_developer@163.com<白茶清欢>
|
// Author : go_developer@163.com<白茶清欢>
|
||||||
//
|
//
|
||||||
// Date : 21:34 2022/6/25
|
// Date : 21:34 2022/6/25
|
||||||
func IsSuccess(e *Exception) bool {
|
func IsSuccess(e *Exception) bool {
|
||||||
return nil == e || e.GetCode() == defaultSuccessCode
|
return nil == e || e.IsCode(defaultSuccessCode)
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewSuccess 代表Success的异常
|
// NewSuccess 代表Success的异常
|
||||||
@ -157,6 +130,6 @@ func IsSuccess(e *Exception) bool {
|
|||||||
// Author : go_developer@163.com<白茶清欢>
|
// Author : go_developer@163.com<白茶清欢>
|
||||||
//
|
//
|
||||||
// Date : 22:35 2022/6/25
|
// Date : 22:35 2022/6/25
|
||||||
func NewSuccess(data interface{}) IException {
|
func NewSuccess(data map[string]any) IException {
|
||||||
return New(defaultSuccessCode, defaultHttpCode, data, "")
|
return New(defaultSuccessCode, data, "")
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user