xpzouying/xiaohongshu-mcp · error

缺少 comment_id

Error message

缺少 comment_id

What it means

Reply 方法的参数校验守卫:commentID trim 后为空字符串,调用方未提供要回复的评论 ID,属必填参数缺失。

Source

Thrown at xiaohongshu/notification_reply.go:25

	"time"

	"github.com/go-rod/rod"
	"github.com/sirupsen/logrus"
	"github.com/xpzouying/xiaohongshu-mcp/humanize"
)

// NotificationReplyResult 通知回复的结果。
type NotificationReplyResult struct {
	CommentID string `json:"comment_id"`
	Nickname  string `json:"nickname"`
	FeedID    string `json:"feed_id,omitempty"`
	Content   string `json:"content"`
}

// Reply 回复一条评论。
func (n *NotificationAction) Reply(ctx context.Context, commentID, content string) (*NotificationReplyResult, error) {
	if strings.TrimSpace(commentID) == "" {
		return nil, fmt.Errorf("缺少 comment_id")
	}
	if strings.TrimSpace(content) == "" {
		return nil, fmt.Errorf("回复内容不能为空")
	}

	page := n.page.Timeout(3 * time.Minute)

	page.MustNavigate("https://www.xiaohongshu.com/notification").MustWaitLoad()
	humanize.Delay(ctx, humanize.AfterNavigate)

	target, index, err := n.locate(ctx, page, commentID)
	if err != nil {
		return nil, err
	}

	items, err := page.Elements(`.tabs-content-container > .container`)
	if err != nil {
		return nil, fmt.Errorf("未找到通知条目: %w", err)

View on GitHub (pinned to 332d196854)

Solutions

  1. 调用前用 strings.TrimSpace 校验 commentID 非空
  2. 同时校验 content 非空(库同样会拒绝空内容)
  3. 在调用方对空值提前返回,不进入自动化流程

Example fix

// before
n.Reply(ctx, commentID, content) // commentID 可能为 ""
// after
if strings.TrimSpace(commentID) == "" { return errors.New("comment_id required") }
n.Reply(ctx, strings.TrimSpace(commentID), content)
Defensive patterns

Strategy: validation

Validate before calling

func validReplyParams(id, content string) bool {
    return strings.TrimSpace(id) != "" && strings.TrimSpace(content) != ""
}
if !validReplyParams(commentID, content) { return errors.New("comment_id and content required") }

Type guard

func hasCommentID(raw map[string]string) (string, bool) {
    id, ok := raw["comment_id"]
    return id, ok && strings.TrimSpace(id) != ""
}

Try / catch

if _, err := n.Reply(ctx, commentID, content); err != nil && strings.Contains(err.Error(), "缺少 comment_id") {
    return fmt.Errorf("caller bug: empty comment_id: %w", err)
}

Prevention

When it happens

Trigger: 调用 Reply(ctx, "", content) 或传入只含空格的 commentID。

Common situations: 上游通知列表数据字段缺失透传;JSON 解码得到空串;调用方拼接 commentID 时模板变量为空。

Understand the failure class

Background: Missing required parameter errors: what 'X is required' and 'the required X param is missing' mean, and how to fix them — this error's family across 27 libraries.

Related errors


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