xpzouying/xiaohongshu-mcp · error

图片不能为空

Error message

图片不能为空

What it means

Publish requires at least one image: PublishImageContent.ImagePaths must be non-empty. If it is, Publish (xiaohongshu/publish.go:75) returns '图片不能为空' before opening/interacting with the page, because XHS image-note publishing cannot proceed without images.

Source

Thrown at xiaohongshu/publish.go:75

		logrus.Warnf("等待 DOM 稳定出现问题: %v,继续尝试", err)
	}
	time.Sleep(1 * time.Second)

	if err := mustClickPublishTab(pp, "上传图文"); err != nil {
		logrus.Errorf("点击上传图文 TAB 失败: %v", err)
		return nil, err
	}

	time.Sleep(1 * time.Second)

	return &PublishAction{
		page: pp,
	}, nil
}

func (p *PublishAction) Publish(ctx context.Context, content PublishImageContent) error {
	if len(content.ImagePaths) == 0 {
		return errors.New("图片不能为空")
	}

	// 重设超时:.Context(ctx) 会替换掉 NewPublishImageAction 里 Timeout(300s) 的 deadline
	page := p.page.Context(ctx).Timeout(300 * time.Second)

	if err := uploadImages(page, content.ImagePaths); err != nil {
		return errors.Wrap(err, "小红书上传图片失败")
	}

	tags := content.Tags
	if len(tags) >= 10 {
		logrus.Warnf("标签数量超过10,截取前10个标签")
		tags = tags[:10]
	}

	logrus.Infof("发布内容: title=%s, images=%v, tags=%v, schedule=%v, original=%v, visibility=%s, products=%v", content.Title, len(content.ImagePaths), tags, content.ScheduleTime, content.IsOriginal, content.Visibility, content.Products)

	if err := submitPublish(ctx, page, content.Title, content.Content, tags, content.ScheduleTime, content.IsOriginal, content.Visibility, content.Products); err != nil {

View on GitHub (pinned to 332d196854)

Solutions

  1. Validate len(content.ImagePaths) > 0 in the caller before invoking Publish
  2. Check the upstream download/glob step: log how many image paths were collected and abort earlier if zero
  3. Populate ImagePaths with existing local files (verify each file exists) before publishing
  4. Return a clearer upstream error including the source directory so misconfiguration is obvious

Example fix

// before
err := publish.Publish(ctx, content) // content.ImagePaths may be empty
// after
if len(content.ImagePaths) == 0 {
    return fmt.Errorf("no images to publish: check download stage and image dir")
}
err := publish.Publish(ctx, content)
Defensive patterns

Strategy: validation

Validate before calling

if len(content.ImagePaths) == 0 {
    return fmt.Errorf("no images to publish")
}
for _, p := range content.ImagePaths {
    if _, err := os.Stat(p); err != nil {
        return fmt.Errorf("image missing: %s", p)
    }
}

Type guard

func canPublish(c xiaohongshu.PublishImageContent) bool {
    return len(c.ImagePaths) > 0
}

Try / catch

if err := publish.Publish(ctx, content); err != nil {
    if strings.Contains(err.Error(), "图片不能为空") {
        return fmt.Errorf("publish aborted: image list empty, check download stage")
    }
    return err
}

Prevention

When it happens

Trigger: Calling PublishAction.Publish with PublishImageContent{ImagePaths: nil} or an empty slice — typically when the caller built the content struct programmatically from a file list that ended up empty (no downloads succeeded, filter removed all files).

Common situations: Image download stage failed earlier so the local paths slice is empty; glob pattern matched no files; caller reused a PublishTextContent-shaped flow and forgot to set ImagePaths; config lists an empty images directory.

Understand the failure class

Background: "Missing required field" and "field is required" errors: why libraries reject payloads that omit mandatory fields — this error's family across 20 libraries.

Related errors


AI-assisted analysis of xpzouying/xiaohongshu-mcp@332d196854 (2026-09-05). Data as JSON: /api/errors/2d9f71db61a58d3c. Report an issue: GitHub.