fix: check errorCodeMap empty

This commit is contained in:
行者
2023-10-26 16:55:44 +08:00
committed by GitHub
parent f6b7ed7019
commit bae4acc480

View File

@@ -16,7 +16,10 @@ func HandleSuccess(ctx *gin.Context, data interface{}) {
if data == nil {
data = map[string]interface{}{}
}
resp := Response{Code: ErrorCodeMap[ErrSuccess], Message: ErrSuccess.Error(), Data: data}
resp := Response{Code: errorCodeMap[ErrSuccess], Message: ErrSuccess.Error(), Data: data}
if _, ok := errorCodeMap[ErrSuccess]; !ok {
resp = Response{Code: 0, Message: "", Data: data}
}
ctx.JSON(http.StatusOK, resp)
}
@@ -24,7 +27,10 @@ func HandleError(ctx *gin.Context, httpCode int, err error, data interface{}) {
if data == nil {
data = map[string]string{}
}
resp := Response{Code: ErrorCodeMap[err], Message: err.Error(), Data: data}
resp := Response{Code: errorCodeMap[err], Message: err.Error(), Data: data}
if _, ok := errorCodeMap[ErrSuccess]; !ok {
resp = Response{Code: 500, Message: "unknown error", Data: data}
}
ctx.JSON(httpCode, resp)
}
@@ -33,11 +39,11 @@ type Error struct {
Message string
}
var ErrorCodeMap = map[error]int{}
var errorCodeMap = map[error]int{}
func newError(code int, msg string) error {
err := errors.New(msg)
ErrorCodeMap[err] = code
errorCodeMap[err] = code
return err
}
func (e Error) Error() string {