fish2018/pansou · error
[ ] JSON序列化失败
Error message
[%s] JSON序列化失败: %w
What it means
searchImpl in the xdyh plugin marshals the request struct to JSON with json.Marshal before sending the POST. Marshaling of plain struct fields virtually never fails, so this error indicates an internal problem (e.g. an unsupported type like chan/func or a cyclic reference in requestBody).
Solutions
- Inspect the wrapped error to find the offending field/type
- Remove or change the unsupported field type in the request struct
- Add a unit test marshaling the request body to catch regressions
Example fix
// before
type requestBody struct {
Callback func() `json:"cb"` // unsupported
}
// after
type requestBody struct {
Keyword string `json:"keyword"`
SplitLinks bool `json:"splitLinks"`
} Defensive patterns
Strategy: validation
Validate before calling
if _, err := json.Marshal(requestBody); err != nil {
return fmt.Errorf("请求体不可序列化: %w", err)
} Try / catch
jsonData, err := json.Marshal(requestBody)
if err != nil {
return nil, fmt.Errorf("[%s] JSON序列化失败: %w", pluginName, err)
} Prevention
- Keep request structs to JSON-safe types (string, number, bool, slice, map)
- Add a marshaling unit test for the request body
- Avoid custom MarshalJSON that can fail
When it happens
Trigger: json.Marshal on the request body returns an error — practically only if requestBody gains an unsupported field type (chan, func, complex) or a marshaler returns an error.
Common situations: Refactoring adds a field with an unsupported type to the request struct; custom MarshalJSON on a nested value fails.
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/62b74a4d796f5eb4.
Report an issue: GitHub.
Appendix: source
Thrown at plugin/xdyh/xdyh.go:126
if cached, ok := searchCache.Load(cacheKey); ok {
if results, ok := cached.([]model.SearchResult); ok {
return results, nil
}
}
// 2. 构建请求体
requestBody := SearchRequest{
Keyword: keyword,
Sites: nil, // null表示搜索所有站点
MaxWorkers: 10, // API默认并发数
SaveToFile: false,
SplitLinks: true,
}
// 3. JSON序列化
jsonData, err := json.Marshal(requestBody)
if err != nil {
return nil, fmt.Errorf("[%s] JSON序列化失败: %w", pluginName, err)
}
// 4. 创建带超时的上下文
ctx, cancel := context.WithTimeout(context.Background(), DefaultTimeout)
defer cancel()
// 5. 创建请求
req, err := http.NewRequestWithContext(ctx, "POST", apiURL, bytes.NewBuffer(jsonData))
if err != nil {
return nil, fmt.Errorf("[%s] 创建请求失败: %w", pluginName, err)
}
// 6. 设置请求头
p.setRequestHeaders(req)
// 7. 发送请求
resp, err := p.doRequestWithRetry(req, client)
if err != nil {View on GitHub (pinned to beaa561337)