flipped-aurora/gin-vue-admin · error

cliId不能为空

Error message

cliId不能为空

What it means

getSysCliByID is the shared loader for a SysCli row used by UpdateCli, DeleteCli, AddCliApis, RemoveCliApis, PreviewApiCommand and getCliAndBindings; it rejects id == 0 with this error because no CLI can have primary key 0. Any public operation that receives a zero CliID funnels into this error.

Source

Thrown at server/plugin/ai/service/sys_cli.go:269

	return res, nil
}

func (s *cliService) getCliAndBindings(ctx context.Context, id uint) (autoModel.SysCli, []sysCliManifestBinding, error) {
	cli, err := s.getSysCliByID(ctx, id)
	if err != nil {
		return autoModel.SysCli{}, nil, err
	}
	bindings, err := s.listCliBindings(ctx, id)
	if err != nil {
		return autoModel.SysCli{}, nil, err
	}
	return cli, bindings, nil
}

func (s *cliService) getSysCliByID(ctx context.Context, id uint) (autoModel.SysCli, error) {
	var cli autoModel.SysCli
	if id == 0 {
		return cli, errors.New("cliId不能为空")
	}
	if err := global.GVA_DB.WithContext(ctx).First(&cli, "id = ?", id).Error; err != nil {
		return cli, err
	}
	return cli, nil
}

func (s *cliService) sysCliNameExists(ctx context.Context, name string, excludeID uint) (bool, error) {
	var existing autoModel.SysCli
	query := global.GVA_DB.WithContext(ctx).Where("name = ?", name)
	if excludeID > 0 {
		query = query.Where("id <> ?", excludeID)
	}
	if err := query.First(&existing).Error; err != nil {
		if errors.Is(err, gorm.ErrRecordNotFound) {
			return false, nil
		}
		return false, err

View on GitHub (pinned to 3136500ef3)

Solutions

  1. Populate CliID/ID with the actual CLI record id before calling
  2. Verify the JSON field names match the Go request struct tags
  3. Check on the caller side that the selected CLI exists and its id is non-zero
  4. Refresh the selection state in the UI so a stale/empty selection cannot be submitted

Example fix

// before
req := autoReq.DeleteSysCliRequest{ID: 0} // field never set
cliService.DeleteCli(ctx, req)
// after
if req.ID == 0 { return errors.New("no CLI selected") }
cliService.DeleteCli(ctx, req)
Defensive patterns

Strategy: validation

Validate before calling

if cliID == 0 {
    return errors.New("a CLI must be selected before performing this operation")
}

Type guard

func cliSelected(req autoReq.DeleteSysCliRequest) bool {
    return req.ID != 0
}

Try / catch

err := cliService.DeleteCli(ctx, req)
if err != nil {
    if err.Error() == "cliId不能为空" {
        return fmt.Errorf("no CLI id supplied; select a CLI first")
    }
    return err
}

Prevention

When it happens

Trigger: Calling any of the CLI management operations (update/delete/add-apis/remove-apis/preview) with CliID or ID set to 0, typically because the request field was never populated.

Common situations: Frontend action fired before a CLI row was selected; JSON body key mismatch (e.g. "cli_id" vs the Go field) so CliID unmarshals to 0; scripted bulk calls where the id lookup failed upstream and the zero value was propagated.

Related errors


AI-assisted analysis of flipped-aurora/gin-vue-admin@3136500ef3 (2026-08-31). Data as JSON: /api/errors/062c773975c57670. Report an issue: GitHub.