flipped-aurora/gin-vue-admin · error
任务名不能为空
Error message
任务名不能为空
What it means
validateTask in TimedTaskService performs pre-persist validation shared by CreateTimedTask and UpdateTimedTask. It throws "任务名不能为空" when SysTimedTask.Name is the empty string, because the task name is required for uniqueness checks and display.
Source
Thrown at server/service/system/sys_timed_task.go:47
// ValidateSpec 服务端校验 cron 表达式(含 @daily/@hourly 等描述符)
func (s *TimedTaskService) ValidateSpec(spec string, withSeconds bool) error {
var err error
if withSeconds {
_, err = cron.NewParser(cron.Second | cron.Minute | cron.Hour | cron.Dom | cron.Month | cron.Dow | cron.Descriptor).Parse(spec)
} else {
_, err = cron.ParseStandard(spec)
}
if err != nil {
return fmt.Errorf("cron 表达式非法: %w", err)
}
return nil
}
// validateTask 落库前统一校验(创建/更新共用)
func (s *TimedTaskService) validateTask(t *system.SysTimedTask) error {
if t.Name == "" {
return errors.New("任务名不能为空")
}
if err := s.ValidateSpec(t.Spec, t.WithSeconds); err != nil {
return err
}
switch t.ExecutorType {
case system.TimedTaskExecutorMethod:
if _, ok := task.Get(t.MethodName); !ok {
return fmt.Errorf("方法 %s 未注册", t.MethodName)
}
if len(t.Params) > 0 && !json.Valid(t.Params) {
return errors.New("params 必须是合法 JSON")
}
case system.TimedTaskExecutorHTTP:
u, err := url.Parse(t.HttpUrl)
if err != nil || (u.Scheme != "http" && u.Scheme != "https") || u.Host == "" {
return errors.New("httpUrl 必须是合法的 http/https 地址")
}
if len(t.HttpHeader) > 0 {View on GitHub (pinned to 3136500ef3)
Solutions
- Include a non-empty "name" field in the create/update request payload.
- Validate name in the frontend form as required before submitting.
- Verify the JSON binding tags match the client's field names so Name is actually populated.
Example fix
// before
svc.CreateTimedTask(ctx, &system.SysTimedTask{Spec: "*/5 * * * *"})
// after
svc.CreateTimedTask(ctx, &system.SysTimedTask{Name: "cleanup-logs", Spec: "*/5 * * * *"}) Defensive patterns
Strategy: validation
Validate before calling
if strings.TrimSpace(task.Name) == "" {
return errors.New("任务名不能为空")
}
// proceed with svc.CreateTimedTask / svc.UpdateTimedTask Type guard
func hasName(t system.SysTimedTask) bool { return strings.TrimSpace(t.Name) != "" } Try / catch
if err := svc.CreateTimedTask(ctx, task); err != nil {
if strings.Contains(err.Error(), "任务名不能为空") {
http.Error(w, "name is required", http.StatusBadRequest)
return
}
http.Error(w, err.Error(), http.StatusInternalServerError)
} Prevention
- Mark the name field required in the frontend form
- Verify JSON field names match binding tags
- Validate payloads before API calls
- Send full objects on update, not sparse maps
When it happens
Trigger: POST/PUT to the timed-task API with a body missing "name" or with name:""; programmatically constructing SysTimedTask without setting Name before CreateTimedTask/UpdateTimedTask.
Common situations: Frontend form submitted with only cron/spec filled; API clients using partial update payloads where name is omitted; JSON field name mismatch (e.g. "taskName") so Name decodes to empty.
Related errors
AI-assisted analysis of flipped-aurora/gin-vue-admin@3136500ef3 (2026-08-31).
Data as JSON: /api/errors/5d15998801337c52.
Report an issue: GitHub.