feat(config): 增强配置解析的语法校验与错误处理
- 重构 ParseConfigToKVMap 使其在解析失败时返回 error 而非静默忽略 - 新增 ValidateConfig 方法,在保存和更新配置前校验语法正确性 - 将 XML 解析器从简单字符串匹配改为标准库 xml.NewDecoder,支持嵌套标签和实体转义 - 在配置保存和更新接口中集成语法校验,校验失败返回 400 错误 - API 拉取配置时解析失败的条目记录日志并跳过,避免整体请求中断 - 新增 config_parse_test.go 覆盖各配置类型的正常与异常场景
This commit is contained in:
@@ -231,6 +231,11 @@ func (h *ConfigHandler) Add(c *gin.Context) {
|
||||
}
|
||||
}
|
||||
|
||||
if err := h.parseService.ValidateConfig(&config); err != nil {
|
||||
c.JSON(http.StatusOK, model.WrapperResponse{Code: 400, Msg: err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
if _, err := dao.Engine.Insert(&config); err != nil {
|
||||
c.JSON(http.StatusOK, model.WrapperResponse{Code: 500, Msg: "保存失败"})
|
||||
return
|
||||
@@ -264,6 +269,15 @@ func (h *ConfigHandler) Update(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
// 校验新内容语法,避免保存后客户端解析失败
|
||||
newConfig := config
|
||||
newConfig.Contents = req.Contents
|
||||
newConfig.Version = req.Version
|
||||
if err := h.parseService.ValidateConfig(&newConfig); err != nil {
|
||||
c.JSON(http.StatusOK, model.WrapperResponse{Code: 400, Msg: err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
// save history
|
||||
h.saveHistory(&config, userInfo.Name, "update")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user