fix(backend): harden write registrar boundaries
This commit is contained in:
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user