Compare commits
0
Commits
master
..
290d1a7bd3
No files matched your search
Generated
+1
@@ -3,6 +3,7 @@
|
||||
<component name="ProjectTasksOptions">
|
||||
<enabled-global>
|
||||
<option value="go fmt" />
|
||||
<option value="goimports" />
|
||||
</enabled-global>
|
||||
</component>
|
||||
</project>
|
||||
+8
-10
@@ -15,16 +15,14 @@ package exception
|
||||
type IException interface {
|
||||
// Error 兼容 go 内置 error
|
||||
Error() string
|
||||
// Code 获取错误码
|
||||
Code() any
|
||||
// Message 获取错误信息
|
||||
Message() string
|
||||
// Data 获取异常时的返回数据
|
||||
Data() any
|
||||
// GetCode *获取错误码
|
||||
GetCode() interface{}
|
||||
// GetMessage *获取错误信息
|
||||
GetMessage() string
|
||||
// GetData 获取异常时的返回数据
|
||||
GetData() interface{}
|
||||
// GetHttpCode *获取当前异常要返回的http状态码, 不设置则 默认 200
|
||||
GetHttpCode() int
|
||||
// ToError 转换为内置error类型
|
||||
ToError() error
|
||||
// IsCode 是否为指定code
|
||||
IsCode(code any) bool
|
||||
// GetStack 获取调用堆栈
|
||||
GetStack() string
|
||||
}
|
||||
@@ -9,6 +9,7 @@ package exception
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -17,57 +18,21 @@ var (
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 20:53 2022/6/25
|
||||
codeTable map[any]Code
|
||||
codeList []Code
|
||||
codeTable map[interface{}]map[string]string
|
||||
)
|
||||
|
||||
func init() {
|
||||
// 规避没调用 InitCodeTable 导致空指针
|
||||
codeTable = make(map[any]Code)
|
||||
codeList = make([]Code, 0)
|
||||
codeTable = make(map[interface{}]map[string]string)
|
||||
}
|
||||
|
||||
// InitCodeTableWithMessage 初始化码表, 同时只指定代表业务成功的状态码以及使用的语言
|
||||
// InitCodeTable 初始化码表, 同时只指定代表业务成功的状态码以及使用的语言
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 20:55 2022/6/25
|
||||
func InitCodeTableWithMessage(table map[any]map[string]string, convertDefaultSuccessCode any, convertDefaultLanguage string) {
|
||||
for code, message := range table {
|
||||
c := Code{
|
||||
Value: code,
|
||||
Message: message,
|
||||
Reason: make(map[string]string),
|
||||
Resolve: make(map[string]string),
|
||||
}
|
||||
|
||||
codeTable[code] = c
|
||||
codeList = append(codeList, c)
|
||||
}
|
||||
if nil == convertDefaultSuccessCode {
|
||||
convertDefaultSuccessCode = 0
|
||||
}
|
||||
if len(convertDefaultLanguage) == 0 {
|
||||
convertDefaultLanguage = defaultLanguage
|
||||
}
|
||||
defaultSuccessCode = convertDefaultSuccessCode
|
||||
defaultLanguage = convertDefaultLanguage
|
||||
}
|
||||
|
||||
// InitCodeTableWithCodeList 按照code列表初始化
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 16:19 2023/6/9
|
||||
func InitCodeTableWithCodeList(list []Code, convertDefaultSuccessCode any, convertDefaultLanguage string) {
|
||||
codeList = list
|
||||
for _, itemCode := range list {
|
||||
if nil == itemCode.Reason {
|
||||
itemCode.Reason = make(map[string]string)
|
||||
}
|
||||
|
||||
codeTable[itemCode.Value] = itemCode
|
||||
}
|
||||
func InitCodeTable(table map[interface{}]map[string]string, convertDefaultSuccessCode interface{}, convertDefaultLanguage string) {
|
||||
codeTable = table
|
||||
if nil == convertDefaultSuccessCode {
|
||||
convertDefaultSuccessCode = 0
|
||||
}
|
||||
@@ -81,8 +46,10 @@ func InitCodeTableWithCodeList(list []Code, convertDefaultSuccessCode any, conve
|
||||
var (
|
||||
// messageWithCode 自动在message文案后追加状态码
|
||||
messageWithCode = true
|
||||
// defaultHttpCode 默认的http状态码
|
||||
defaultHttpCode = http.StatusOK
|
||||
// defaultSuccessCode 默认代表成功的状态码
|
||||
defaultSuccessCode any = 0
|
||||
defaultSuccessCode interface{}
|
||||
// defaultLanguage 默认的语言
|
||||
defaultLanguage = "zh"
|
||||
)
|
||||
@@ -96,54 +63,32 @@ 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 {
|
||||
if nil == code {
|
||||
return ""
|
||||
}
|
||||
if eCode, ok := code.(Code); ok {
|
||||
code = eCode.Value
|
||||
}
|
||||
var (
|
||||
inputCodeInfo Code
|
||||
exist bool
|
||||
)
|
||||
if inputCodeInfo, exist = codeTable[code]; !exist {
|
||||
// 错误码不存在
|
||||
if len(defaultMessage) > 0 && len(defaultMessage[0]) > 0 {
|
||||
return defaultMessage[0]
|
||||
}
|
||||
func getMessage(code interface{}) string {
|
||||
message, exist := codeTable[code]
|
||||
if !exist {
|
||||
// 无论是否开启 messageWithCode , 未知错误强行带 code
|
||||
return fmt.Sprintf("unknown error【%v】", code)
|
||||
}
|
||||
if code == defaultSuccessCode {
|
||||
// 请求成功, 一直不带状态码后缀
|
||||
return inputCodeInfo.Message[defaultLanguage]
|
||||
return fmt.Sprintf("未知错误【%v】", code)
|
||||
}
|
||||
if messageWithCode {
|
||||
return fmt.Sprintf(inputCodeInfo.Message[defaultLanguage]+"【%v】", code)
|
||||
if code == defaultSuccessCode {
|
||||
// 请求成功, 一直不带状态码后缀
|
||||
return message[defaultLanguage]
|
||||
}
|
||||
return fmt.Sprintf(message[defaultLanguage]+"【%v】", code)
|
||||
}
|
||||
return inputCodeInfo.Message[defaultLanguage]
|
||||
return message[defaultLanguage]
|
||||
}
|
||||
|
||||
// GetCodeList ...
|
||||
// GetMessage 获取消息信息
|
||||
//
|
||||
// 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[any]Code {
|
||||
return codeTable
|
||||
// Date : 12:58 2022/6/26
|
||||
func GetMessage(code interface{}) string {
|
||||
return getMessage(code)
|
||||
}
|
||||
@@ -1,20 +0,0 @@
|
||||
// Package exception ...
|
||||
//
|
||||
// Description : exception ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 2023-06-09 15:54
|
||||
package exception
|
||||
|
||||
// Code 状态码的结构
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 15:55 2023/6/9
|
||||
type Code struct {
|
||||
Value any `json:"value"` // 状态码的值
|
||||
Message map[string]string `json:"message"` // 状态码对应的文案(key -> 语言 , value -> 对应语言的描述)
|
||||
Reason map[string]string `json:"reason"` // 产生此错误码的原因描述(key -> 语言 , value -> 对应语言的原因列表)
|
||||
Resolve map[string]string `json:"resolve"` // 解决此错误码的方案描述(key -> 语言 , value -> 对应语言的方案列表)
|
||||
}
|
||||
+52
-68
@@ -8,12 +8,8 @@
|
||||
package exception
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"runtime"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Exception 异常接口的具体实现
|
||||
@@ -22,38 +18,32 @@ import (
|
||||
//
|
||||
// Date *: 21:09 2022/6/25
|
||||
type Exception struct {
|
||||
code any
|
||||
message string
|
||||
data any
|
||||
stack string
|
||||
code interface{}
|
||||
message string
|
||||
httpCode int
|
||||
data interface{}
|
||||
}
|
||||
|
||||
func (e *Exception) Error() string {
|
||||
mapData := map[string]any{
|
||||
"code": e.Code(),
|
||||
"msg": e.Message(),
|
||||
"data": e.Data(),
|
||||
}
|
||||
byteData, _ := json.Marshal(mapData)
|
||||
return string(byteData)
|
||||
return e.GetMessage()
|
||||
}
|
||||
|
||||
func (e *Exception) GetStack() string {
|
||||
return e.stack
|
||||
}
|
||||
|
||||
func (e *Exception) Code() any {
|
||||
func (e *Exception) GetCode() interface{} {
|
||||
return e.code
|
||||
}
|
||||
|
||||
func (e *Exception) Message() string {
|
||||
func (e *Exception) GetMessage() string {
|
||||
return e.message
|
||||
}
|
||||
|
||||
func (e *Exception) Data() any {
|
||||
func (e *Exception) GetData() interface{} {
|
||||
return e.data
|
||||
}
|
||||
|
||||
func (e *Exception) GetHttpCode() int {
|
||||
return e.httpCode
|
||||
}
|
||||
|
||||
func (e *Exception) ToError() error {
|
||||
if nil == e {
|
||||
return nil
|
||||
@@ -61,22 +51,22 @@ func (e *Exception) ToError() error {
|
||||
return errors.New(e.Error())
|
||||
}
|
||||
|
||||
// IsCode 判断是否为指定错误码
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 00:39 2023/9/28
|
||||
func (e *Exception) IsCode(inputCode any) bool {
|
||||
return fmt.Sprintf("%v", inputCode) == fmt.Sprintf("%v", e.Code())
|
||||
}
|
||||
|
||||
// NewWithCode 仅使用错误码实例化异常
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 21:22 2022/6/25
|
||||
func NewWithCode(code any) IException {
|
||||
return New(code, map[string]any{}, "")
|
||||
func NewWithCode(code interface{}) IException {
|
||||
return New(code, defaultHttpCode, nil)
|
||||
}
|
||||
|
||||
// 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 获取异常实例
|
||||
@@ -84,8 +74,8 @@ func NewWithCode(code any) IException {
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 21:28 2022/6/25
|
||||
func NewWithCodeAndData(code any, data map[string]any) IException {
|
||||
return New(code, data, "")
|
||||
func NewWithCodeAndData(code interface{}, data interface{}) IException {
|
||||
return New(code, defaultHttpCode, data)
|
||||
}
|
||||
|
||||
// New 获取异常实例
|
||||
@@ -93,34 +83,16 @@ func NewWithCodeAndData(code any, data map[string]any) IException {
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 21:28 2022/6/25
|
||||
func New(code any, data any, defaultMessage ...string) IException {
|
||||
func New(code interface{}, httpCode int, data interface{}) IException {
|
||||
if nil == data {
|
||||
// 保证数据结构的一致性, 同时避免后续使用出现空指针
|
||||
data = map[string]any{}
|
||||
}
|
||||
pcs := make([]uintptr, 128)
|
||||
n := runtime.Callers(2, pcs)
|
||||
frames := runtime.CallersFrames(pcs[:n])
|
||||
var sb strings.Builder
|
||||
for {
|
||||
frame, more := frames.Next()
|
||||
sb.WriteString(frame.Function)
|
||||
sb.WriteByte('\n')
|
||||
sb.WriteByte('\t')
|
||||
sb.WriteString(frame.File)
|
||||
sb.WriteByte(':')
|
||||
sb.WriteString(strconv.Itoa(frame.Line))
|
||||
sb.WriteByte('\n')
|
||||
|
||||
if !more {
|
||||
break
|
||||
}
|
||||
data = map[string]interface{}{}
|
||||
}
|
||||
return &Exception{
|
||||
code: code,
|
||||
message: GetMessage(code, defaultMessage...),
|
||||
data: data,
|
||||
stack: sb.String(),
|
||||
code: code,
|
||||
message: getMessage(code),
|
||||
httpCode: httpCode,
|
||||
data: data,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -129,13 +101,13 @@ func New(code any, data any, defaultMessage ...string) IException {
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 21:36 2022/6/25
|
||||
func NewFromError(code any, err error) IException {
|
||||
func NewFromError(code interface{}, err error) IException {
|
||||
if nil == err {
|
||||
return nil
|
||||
}
|
||||
return New(code, map[string]any{
|
||||
"err_msg": err.Error(),
|
||||
}, err.Error())
|
||||
return New(code, defaultHttpCode, map[string]interface{}{
|
||||
"error": err.Error(),
|
||||
})
|
||||
}
|
||||
|
||||
// NewFromMessage 从 code message 生成exception
|
||||
@@ -143,20 +115,32 @@ func NewFromError(code any, err error) IException {
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 22:25 2023/2/11
|
||||
func NewFromMessage(code any, message string) IException {
|
||||
func NewFromMessage(code interface{}, message string) IException {
|
||||
if len(message) == 0 {
|
||||
message = GetMessage(code, "unknown error")
|
||||
message = fmt.Sprintf("%v", code)
|
||||
}
|
||||
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.IsCode(defaultSuccessCode)
|
||||
return nil == e || e.GetCode() == defaultSuccessCode
|
||||
}
|
||||
|
||||
// NewSuccess 代表Success的异常
|
||||
@@ -164,6 +148,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, data, "")
|
||||
func NewSuccess(data interface{}) IException {
|
||||
return New(defaultSuccessCode, defaultHttpCode, data)
|
||||
}
|
||||