xpzouying/xiaohongshu-mcp · error

解析通知列表失败: %w

Error message

解析通知列表失败: %w

What it means

readTab 拿到分区 JSON 字符串后 json.Unmarshal 到 notificationPayload 失败:原始数据结构与结构体定义不符(前端改版)或 JSON 截断。

Source

Thrown at xiaohongshu/notification.go:303

// readTab 读取指定分区的原始数据,字段映射由结构体承担。
func (n *NotificationAction) readTab(page *rod.Page, tab NotificationTab) (*notificationPayload, error) {
	res, err := page.Eval(`(tab) => {
		const s = window.__INITIAL_STATE__;
		const m = s && s.notification && s.notification.notificationMap;
		return m && m[tab] ? JSON.stringify(m[tab]) : "";
	}`, string(tab))
	if err != nil {
		return nil, fmt.Errorf("读取通知列表失败: %w", err)
	}

	raw := res.Value.String()
	if raw == "" {
		return nil, fmt.Errorf("页面状态里没有分区 %s,可能未登录或页面结构已变化", tab)
	}

	var payload notificationPayload
	if err := json.Unmarshal([]byte(raw), &payload); err != nil {
		return nil, fmt.Errorf("解析通知列表失败: %w", err)
	}
	return &payload, nil
}

// visible 判断一条通知的内容是否仍然正常可见。
func (r rawNotification) visible() bool {
	if r.Comment.ID != "" && r.Comment.Illegal.Status != "" && r.Comment.Illegal.Status != statusNormal {
		return false
	}
	if r.Item.ID != "" && r.Item.Illegal.Status != "" && r.Item.Illegal.Status != statusNormal {
		return false
	}
	return true
}

// convertNotifications 过滤掉不可见条目并转成对外结构,返回条目和被过滤的数量。
func convertNotifications(raw []rawNotification, limit int) ([]NotificationItem, int) {
	items := make([]NotificationItem, 0, len(raw))

View on GitHub (pinned to 332d196854)

Solutions

  1. 打印 raw 内容对比 notificationPayload 结构体定义,修正 JSON tag
  2. 检查小红书前端是否改版,更新结构体字段映射
  3. 给结构体字段加上兼容类型(如 json.Number 或自定义 UnmarshalJSON)

Example fix

// before
likes  int    `json:"likes"`
// after
likes  json.Number `json:"likes"`
func (r *rawNotification) UnmarshalJSON(b []byte) error { /* 兼容新旧字段 */ }
Defensive patterns

Strategy: try-catch

Try / catch

items, err := n.List(ctx)
if err != nil && strings.Contains(err.Error(), "解析通知列表失败") {
    // 记录原始返回,升级库或上报结构变更
    log.Printf("payload schema changed: %v", err)
    return fallbackLegacyPath()
}

Prevention

When it happens

Trigger: readTab 反序列化 notificationMap[tab] 序列化结果失败:字段类型变化(如数字变字符串)、结构从对象变数组、负载中包含无法解析的数据形态。

Common situations: 小红书前端升级后通知条目 JSON 字段改名/换类型;历史缓存的旧结构数据;JSON 中混入了非常规嵌套结构。

Understand the failure class

Background: Schema validation failed / invalid input schema: payload rejected because its shape doesn't match the expected schema — this error's family across 28 libraries.

Related errors


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