xpzouying/xiaohongshu-mcp · error

长度超过限制: %s

Error message

长度超过限制: %s

What it means

This is a fallback error from makeMaxLengthError: the length indicator text retrieved from the page did not contain exactly one '/' separator (expected format like '1002/1000'), so the library cannot parse current vs max length and reports the raw text instead. It signals the site's length-error UI changed format or contained unexpected text.

Source

Thrown at xiaohongshu/publish.go:638

		return errors.Wrap(err, "检查正文长度元素失败")
	}

	if !has {
		return nil
	}

	contentLength, err := elem.Text()
	if err != nil {
		return errors.Wrap(err, "获取正文长度文本失败")
	}

	return makeMaxLengthError(contentLength)
}

func makeMaxLengthError(elemText string) error {
	parts := strings.Split(elemText, "/")
	if len(parts) != 2 {
		return errors.Errorf("长度超过限制: %s", elemText)
	}

	currLen, maxLen := parts[0], parts[1]

	return errors.Errorf("当前输入长度为%s,最大长度为%s", currLen, maxLen)
}

// contentElemSelectors 正文输入框的候选选择器,按先后顺序尝试。
var contentElemSelectors = []string{
	`div[role="textbox"][contenteditable="true"]`,
	`div.tiptap[contenteditable="true"]`,
	`div.ql-editor`,
}

// getContentElement 在 timeout 内轮询查找正文输入框,全部落空返回错误。
func getContentElement(page *rod.Page, timeout time.Duration) (*rod.Element, error) {
	deadline := time.Now().Add(timeout)

View on GitHub (pinned to 332d196854)

Solutions

  1. Log/inspect the raw elemText in the error to see the actual page text and adapt expectations.
  2. Check whether the site changed the indicator format and update makeMaxLengthError parsing accordingly.
  3. Reduce content/title length before publishing so the indicator never appears.
  4. If the text merely has whitespace around '/', trim parts before validation.

Example fix

// before
parts := strings.Split(elemText, "/")
if len(parts) != 2 {
    return errors.Errorf("长度超过限制: %s", elemText)
}
// after
parts := strings.Split(strings.TrimSpace(elemText), "/")
parts[0] = strings.TrimSpace(parts[0])
if len(parts) == 2 {
    parts[1] = strings.TrimSpace(parts[1])
}
if len(parts) != 2 {
    return errors.Errorf("长度超过限制: %s", elemText)
}
Defensive patterns

Strategy: validation

Validate before calling

// ensure content stays under the limit so the indicator never needs parsing
if utf8.RuneCountInString(content) > 1000 || utf8.RuneCountInString(title) > 20 {
    return errors.New("内容或标题超长,请先截断")
}

Type guard

func parseLengthIndicator(text string) (curr, max string, ok bool) {
    parts := strings.Split(strings.TrimSpace(text), "/")
    if len(parts) != 2 {
        return "", "", false
    }
    return strings.TrimSpace(parts[0]), strings.TrimSpace(parts[1]), true
}

Try / catch

err := publisher.Publish(ctx, opts)
if err != nil && strings.Contains(err.Error(), "长度超过限制: ") {
    raw := strings.TrimPrefix(err.Error(), "长度超过限制: ")
    log.Printf("未识别的长度指示格式: %q,请检查站点 DOM 是否变更", raw)
}

Prevention

When it happens

Trigger: checkTitleMaxLength or checkContentMaxLength found the over-length indicator element, its text was read successfully, but strings.Split(text, "/") did not yield exactly 2 parts — e.g. text is '超出字数限制' or contains multiple '/' characters.

Common situations: Xiaohongshu front-end redesign changed the indicator format (e.g. no slash, or '1002 / 1000' with spaces handled, or extra characters); a localized or partial-render message; scraping a stale DOM snapshot.

Related errors


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