fish2018/pansou · error

marshal request failed

Error message

marshal request failed (page %d): %w

What it means

In the hunhepan plugin's paginated request builder, json.Marshal(reqBody) for a page failed. The marshal of a plain map/struct should rarely fail, so this usually indicates an unsupported value (e.g. a channel, func, or cyclic structure) snuck into reqBody.

Solutions

  1. Inspect the wrapped error message for the unsupported type name
  2. Check recently changed fields in reqBody for non-JSON-serializable values
  3. Replace unsupported values with serializable equivalents (string/number)
  4. Pre-validate reqBody types in tests to catch regressions early

Example fix

// before
jsonData, err := json.Marshal(reqBody)
if err != nil {
	errChan <- fmt.Errorf("marshal request failed (page %d): %w", pageNum, err)
	return
}
// after
jsonData, err := json.Marshal(reqBody)
if err != nil {
	errChan <- fmt.Errorf("marshal request failed (page %d): %w (reqBody=%+v)", pageNum, err, reqBody)
	return
}
Defensive patterns

Strategy: validation

Validate before calling

if err := json.Valid(mustMarshal(reqBody)); !err {
	// request body not serializable; fix reqBody types before calling
}
func mustMarshal(v interface{}) []byte {
	b, _ := json.Marshal(v)
	return b
}

Try / catch

// caller side
if err != nil && strings.Contains(err.Error(), "marshal request failed") {
	var page int
	fmt.Sscanf(err.Error(), "marshal request failed (page %d)", &page)
	log.Printf("page %d body unserializable: %v", page, err)
}

Prevention

When it happens

Trigger: json.Marshal(reqBody) returns an error while building the POST body for pageNum of a paged search; the error is pushed to errChan with the page number.

Common situations: A field of unsupported type (chan/func/complex) added to reqBody, a cyclic reference, or a custom json.Marshaler returning an error — rare, typically a code regression rather than runtime data.

Understand the failure class

Background: json.Marshal / "failed to marshal" errors in Go: why "unsupported type" happens and how to fix it — this error's family across 22 libraries.

Related errors


AI-assisted analysis of fish2018/pansou@beaa561337 (2026-09-07). Data as JSON: /api/errors/07ed0672b03919cc. Report an issue: GitHub.

Appendix: source

Thrown at plugin/hunhepan/hunhepan.go:202

				"page":         pageNum,
				"q":            keyword,
				"user":         "",
				"exact":        false,
				"format":       []string{},
				"share_time":   "",
				"size":         DefaultPageSize,
				"type":         "",
				"exclude_user": []string{},
				"adv_params": map[string]interface{}{
					"wechat_pwd": "",
					"platform":   "pc",
				},
			}

			jsonData, err := json.Marshal(reqBody)
			if err != nil {
				debugLog("序列化请求失败 (page %d): %v", pageNum, err)
				errChan <- fmt.Errorf("marshal request failed (page %d): %w", pageNum, err)
				return
			}

			debugLog("发送请求到 %s (page %d): %s", apiURL, pageNum, string(jsonData))

			req, err := http.NewRequest("POST", apiURL, bytes.NewBuffer(jsonData))
			if err != nil {
				debugLog("创建请求失败 (page %d): %v", pageNum, err)
				errChan <- fmt.Errorf("create request failed (page %d): %w", pageNum, err)
				return
			}

			req.Header.Set("Content-Type", "application/json")
			req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36")
			req.Header.Set("Accept", "application/json, text/plain, */*")
			req.Header.Set("Accept-Language", "zh-CN,zh;q=0.9,en;q=0.8")

			// 根据不同的API设置不同的Referer

View on GitHub (pinned to beaa561337)