fix(backend): harden write registrar boundaries

This commit is contained in:
2026-07-21 16:12:20 +08:00
parent 1fcbb31301
commit 80bec26839
17 changed files with 639 additions and 42 deletions

View File

@@ -1,11 +1,13 @@
package projects
import (
"errors"
"net/http"
"strings"
"time"
"github.com/gin-gonic/gin"
"gorm.io/gorm"
"senlinai-agent/backend/internal/httpx"
"senlinai-agent/backend/internal/models"
)
@@ -62,12 +64,23 @@ func (h *CronHandler) create(c *gin.Context) {
Title: input.Title, Schedule: input.Schedule, Enabled: input.Enabled, NextRunAt: nextRunAt,
})
if err != nil {
httpx.Error(c, http.StatusBadRequest, "invalid_request", "计划任务参数无效")
writeCronError(c, err)
return
}
c.JSON(http.StatusCreated, cronPlanDTO(*plan))
}
func writeCronError(c *gin.Context, err error) {
switch {
case errors.Is(err, ErrCronTitleRequired), errors.Is(err, ErrCronScheduleRequired):
httpx.Error(c, http.StatusBadRequest, "invalid_request", "计划任务参数无效")
case errors.Is(err, gorm.ErrRecordNotFound):
httpx.Error(c, http.StatusNotFound, "not_found", "项目不存在")
default:
httpx.Error(c, http.StatusInternalServerError, "internal_error", "计划任务创建失败")
}
}
func cronPlanDTO(plan models.SenlinAgentCronPlan) CronPlanDTO {
return CronPlanDTO{
ID: plan.Identity, ProjectID: plan.ProjectIdentity, Title: plan.Title, Schedule: plan.Schedule,