Refactor error handling in system and trace controllers
- Updated error response codes for various validation errors from 400 to 422 to better reflect the nature of the errors. - Changed error messages for empty parameters (e.g., userId, menuId, roleId) to use a consistent error code format. - Improved error handling in the IPerf, Ping, and WS controllers to provide more informative error messages. - Ensured that all controllers return appropriate error messages when binding JSON or query parameters fails.
This commit is contained in:
@@ -1,8 +1,9 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"be.ems/src/framework/constants"
|
||||
"be.ems/src/framework/i18n"
|
||||
"be.ems/src/framework/reqctx"
|
||||
"be.ems/src/framework/resp"
|
||||
"be.ems/src/framework/token"
|
||||
@@ -140,7 +141,8 @@ func (s *BootloaderController) Account(c *gin.Context) {
|
||||
Password string `json:"password" binding:"required"`
|
||||
}
|
||||
if err := c.ShouldBindJSON(&body); err != nil {
|
||||
c.JSON(400, resp.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||||
errMsgs := fmt.Sprintf("bind err: %s", resp.FormatBindError(err))
|
||||
c.JSON(422, resp.CodeMsg(422001, errMsgs))
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
@@ -1,8 +1,12 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/mojocn/base64Captcha"
|
||||
|
||||
"be.ems/src/framework/config"
|
||||
"be.ems/src/framework/constants"
|
||||
"be.ems/src/framework/database/redis"
|
||||
@@ -10,42 +14,30 @@ import (
|
||||
"be.ems/src/framework/resp"
|
||||
"be.ems/src/framework/utils/parse"
|
||||
systemService "be.ems/src/modules/system/service"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/mojocn/base64Captcha"
|
||||
)
|
||||
|
||||
// 实例化控制层 CaptchaController 结构体
|
||||
// NewCaptcha 实例化控制层
|
||||
var NewCaptcha = &CaptchaController{
|
||||
sysConfigService: systemService.NewSysConfig,
|
||||
}
|
||||
|
||||
// 验证码操作处理
|
||||
// CaptchaController 验证码操作 控制层处理
|
||||
//
|
||||
// PATH /
|
||||
type CaptchaController struct {
|
||||
sysConfigService *systemService.SysConfig // 参数配置服务
|
||||
}
|
||||
|
||||
// 获取验证码
|
||||
// Image 获取验证码-图片
|
||||
//
|
||||
// GET /captchaImage
|
||||
//
|
||||
// @Tags common
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Success 200 {object} object "Response Results"
|
||||
// @Security TokenAuth
|
||||
// @Summary Get CAPTCHA
|
||||
// @Description Get CAPTCHA
|
||||
// @Router /captchaImage [get]
|
||||
func (s *CaptchaController) Image(c *gin.Context) {
|
||||
// GET /captcha-image
|
||||
func (s CaptchaController) Image(c *gin.Context) {
|
||||
// 从数据库配置获取验证码开关 true开启,false关闭
|
||||
captchaEnabledStr := s.sysConfigService.FindValueByKey("sys.account.captchaEnabled")
|
||||
captchaEnabled := parse.Boolean(captchaEnabledStr)
|
||||
if !captchaEnabled {
|
||||
c.JSON(200, resp.Ok(map[string]any{
|
||||
"captchaEnabled": captchaEnabled,
|
||||
c.JSON(200, resp.OkData(map[string]any{
|
||||
"enabled": captchaEnabled,
|
||||
}))
|
||||
return
|
||||
}
|
||||
@@ -53,14 +45,16 @@ func (s *CaptchaController) Image(c *gin.Context) {
|
||||
// 生成唯一标识
|
||||
verifyKey := ""
|
||||
data := map[string]any{
|
||||
"captchaEnabled": captchaEnabled,
|
||||
"uuid": "",
|
||||
"img": "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7",
|
||||
"enabled": captchaEnabled,
|
||||
"uuid": "",
|
||||
"img": "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7",
|
||||
}
|
||||
|
||||
// 验证码有效期,单位秒
|
||||
captchaExpiration := 2 * 60 * time.Second
|
||||
// 从数据库配置获取验证码类型 math 数值计算 char 字符验证
|
||||
captchaType := s.sysConfigService.FindValueByKey("sys.account.captchaType")
|
||||
if captchaType == constants.CAPTCHA_TYPE_MATH {
|
||||
if captchaType == "math" {
|
||||
math := config.Get("mathCaptcha").(map[string]any)
|
||||
driverCaptcha := &base64Captcha.DriverMath{
|
||||
//Height png height in pixel.
|
||||
@@ -81,16 +75,15 @@ func (s *CaptchaController) Image(c *gin.Context) {
|
||||
// 验证码表达式解析输出
|
||||
item, err := driverCaptcha.DrawCaptcha(question)
|
||||
if err != nil {
|
||||
logger.Infof("Generate Id Question Answer %s %s : %v", captchaType, question, err)
|
||||
logger.Infof("generate id question answer %s %s : %v", captchaType, question, err)
|
||||
} else {
|
||||
data["uuid"] = id
|
||||
data["img"] = item.EncodeB64string()
|
||||
expiration := constants.CAPTCHA_EXPIRATION * time.Second
|
||||
verifyKey = constants.CACHE_CAPTCHA_CODE + ":" + id
|
||||
redis.Set("", verifyKey, answer, expiration)
|
||||
_ = redis.Set("", verifyKey, answer, captchaExpiration)
|
||||
}
|
||||
}
|
||||
if captchaType == constants.CAPTCHA_TYPE_CHAR {
|
||||
if captchaType == "char" {
|
||||
char := config.Get("charCaptcha").(map[string]any)
|
||||
driverCaptcha := &base64Captcha.DriverString{
|
||||
//Height png height in pixel.
|
||||
@@ -115,13 +108,12 @@ func (s *CaptchaController) Image(c *gin.Context) {
|
||||
// 验证码表达式解析输出
|
||||
item, err := driverCaptcha.DrawCaptcha(question)
|
||||
if err != nil {
|
||||
logger.Infof("Generate Id Question Answer %s %s : %v", captchaType, question, err)
|
||||
logger.Infof("generate id question answer %s %s : %v", captchaType, question, err)
|
||||
} else {
|
||||
data["uuid"] = id
|
||||
data["img"] = item.EncodeB64string()
|
||||
expiration := constants.CAPTCHA_EXPIRATION * time.Second
|
||||
verifyKey = constants.CACHE_CAPTCHA_CODE + ":" + id
|
||||
redis.Set("", verifyKey, answer, expiration)
|
||||
_ = redis.Set("", verifyKey, strings.ToLower(answer), captchaExpiration)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -129,8 +121,8 @@ func (s *CaptchaController) Image(c *gin.Context) {
|
||||
if config.Env() == "local" {
|
||||
text, _ := redis.Get("", verifyKey)
|
||||
data["text"] = text
|
||||
c.JSON(200, resp.Ok(data))
|
||||
c.JSON(200, resp.OkData(data))
|
||||
return
|
||||
}
|
||||
c.JSON(200, resp.Ok(data))
|
||||
c.JSON(200, resp.OkData(data))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user