{"record":{"id":"bb3869e8c1396900","repo":"fish2018/pansou","slug":"decode-response-failed-page-d-w","errorCode":null,"errorMessage":"decode response failed (page %d): %w","messagePattern":"decode response failed \\(page (.+?)\\): %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"plugin/hunhepan/hunhepan.go","lineNumber":257,"sourceCode":"\t\t\tdefer resp.Body.Close()\n\n\t\t\tdebugLog(\"收到响应 (page %d), 状态码: %d\", pageNum, resp.StatusCode)\n\n\t\t\t// 读取响应体\n\t\t\trespBody, err := io.ReadAll(resp.Body)\n\t\t\tif err != nil {\n\t\t\t\tdebugLog(\"读取响应失败 (page %d): %v\", pageNum, err)\n\t\t\t\terrChan <- fmt.Errorf(\"read response body failed (page %d): %w\", pageNum, err)\n\t\t\t\treturn\n\t\t\t}\n\n\t\t\tdebugLog(\"响应内容 (page %d, 前500字符): %s\", pageNum, string(respBody[:min(500, len(respBody))]))\n\n\t\t\t// 解析响应\n\t\t\tvar apiResp HunhepanResponse\n\t\t\tif err := json.Unmarshal(respBody, &apiResp); err != nil {\n\t\t\t\tdebugLog(\"JSON解析失败 (page %d): %v\", pageNum, err)\n\t\t\t\terrChan <- fmt.Errorf(\"decode response failed (page %d): %w\", pageNum, err)\n\t\t\t\treturn\n\t\t\t}\n\n\t\t\t// 检查响应状态\n\t\t\tif apiResp.Code != 200 {\n\t\t\t\tdebugLog(\"API返回错误 (page %d): code=%d, msg=%s\", pageNum, apiResp.Code, apiResp.Msg)\n\t\t\t\terrChan <- fmt.Errorf(\"API returned error (page %d): %s\", pageNum, apiResp.Msg)\n\t\t\t\treturn\n\t\t\t}\n\n\t\t\tdebugLog(\"成功获取第 %d 页数据，共 %d 条结果\", pageNum, len(apiResp.Data.List))\n\n\t\t\t// 将结果发送到通道\n\t\t\tresultChan <- apiResp.Data.List\n\t\t}(page)\n\t}\n\n\t// 启动一个goroutine等待所有页面请求完成并关闭通道","sourceCodeStart":239,"sourceCodeEnd":275,"githubUrl":"https://github.com/fish2018/pansou/blob/beaa56133755a548ebc51b090b3816e2ae044aa6/plugin/hunhepan/hunhepan.go#L239-L275","documentation":"Hunhepan plugin throws this when json.Unmarshal(respBody, &apiResp) fails while decoding a page's body into HunhepanResponse. It means the response bytes are not valid JSON or do not match the expected struct (e.g. wrong JSON types for fields).","triggerScenarios":"The API returns HTML (login page, error page, CAPTCHA), an empty body, truncated JSON, or JSON whose field types differ from HunhepanResponse (e.g. code as string instead of number).","commonSituations":"API endpoint moved or now requires auth cookies; an anti-bot WAF serves HTML; the site changed its response schema after a plugin update; CDN serves an error page.","solutions":["Inspect the logged first-500-chars debug output to see what the API actually returned.","Check whether the API now requires authentication or a different endpoint/headers (User-Agent, Referer).","Confirm the HunhepanResponse struct matches the current API schema (field names and JSON types).","Add a content-type check before unmarshalling to fail fast on HTML responses."],"exampleFix":"// before\nif err := json.Unmarshal(respBody, &apiResp); err != nil {\n\terrChan <- fmt.Errorf(\"decode response failed (page %d): %w\", pageNum, err)\n\treturn\n}\n// after\nif err := json.Unmarshal(respBody, &apiResp); err != nil {\n\tvar unmarshalErr *json.UnmarshalTypeError\n\tif errors.As(err, &unmarshalErr) {\n\t\terrChan <- fmt.Errorf(\"decode response failed (page %d): field %s type mismatch: %w\", pageNum, unmarshalErr.Field, err)\n\t} else {\n\t\terrChan <- fmt.Errorf(\"decode response failed (page %d): %w\", pageNum, err)\n\t}\n\treturn\n}","handlingStrategy":"type-guard","validationCode":"// after obtaining the raw response, before trusting JSON\nct := resp.Header.Get(\"Content-Type\")\nif !strings.Contains(ct, \"application/json\") {\n\treturn fmt.Errorf(\"unexpected content-type %q — API likely served HTML/auth page\", ct)\n}","typeGuard":"func looksLikeJSON(b []byte) bool {\n\tt := bytes.TrimSpace(b)\n\treturn len(t) > 0 && (t[0] == '{' || t[0] == '[')\n}","tryCatchPattern":"results, err := plugin.Search(ctx, keyword)\nvar pe *json.UnmarshalTypeError\nif err != nil && errors.As(err, pe) == (pe != nil) {\n\t// schema/type mismatch: log the debug body dump and check API contract changes\n} else if err != nil {\n\tlog.Printf(\"hunhepan decode failed (schema or auth page?): %v\", err)\n}","preventionTips":["Check the logged first-500-chars output to identify HTML vs JSON responses","Keep the response struct in sync with the live API schema","Validate content-type before unmarshalling","Re-test plugins after upstream site updates — schemas change without notice"],"tags":["json","go","unmarshal","response-parsing"],"backgroundTag":"json-unmarshal-failed","analyzedSha":"beaa56133755a548ebc51b090b3816e2ae044aa6","analyzedAt":"2026-09-07T00:31:18.025Z","contentChangedAt":"2026-09-07T00:31:18.025Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}