{"record":{"id":"9ef0435d874cb20a","repo":"fish2018/pansou","slug":"s-w-9ef043","errorCode":null,"errorMessage":"[%s] 解析搜索结果失败: %w","messagePattern":"\\[(.+?)\\] 解析搜索结果失败: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"plugin/5266ys/5266ys.go","lineNumber":198,"sourceCode":"\tresp, err := client.Do(req)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"[%s] 搜索请求失败: %w\", p.Name(), err)\n\t}\n\tdefer resp.Body.Close()\n\tif resp.StatusCode != http.StatusOK {\n\t\treturn nil, fmt.Errorf(\"[%s] 搜索请求返回 HTTP %d\", p.Name(), resp.StatusCode)\n\t}\n\tbody, err := io.ReadAll(io.LimitReader(resp.Body, 4<<20))\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"[%s] 读取搜索结果失败: %w\", p.Name(), err)\n\t}\n\tdecoded, err := decodeGB18030(body)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"[%s] 解码搜索结果失败: %w\", p.Name(), err)\n\t}\n\tdoc, err := goquery.NewDocumentFromReader(strings.NewReader(decoded))\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"[%s] 解析搜索结果失败: %w\", p.Name(), err)\n\t}\n\treturn doc, nil\n}\n\nfunc (p *Plugin) fetchDetail(client *http.Client, detailURL string) ([]magnetItem, string, string) {\n\tctx, cancel := context.WithTimeout(context.Background(), requestTimeout)\n\tdefer cancel()\n\treq, err := http.NewRequestWithContext(ctx, http.MethodGet, detailURL, nil)\n\tif err != nil {\n\t\treturn nil, \"\", \"\"\n\t}\n\tsetHeaders(req, baseURL+\"/\")\n\tresp, err := client.Do(req)\n\tif err != nil {\n\t\treturn nil, \"\", \"\"\n\t}\n\tdefer resp.Body.Close()\n\tif resp.StatusCode != http.StatusOK {","sourceCodeStart":180,"sourceCodeEnd":216,"githubUrl":"https://github.com/fish2018/pansou/blob/beaa56133755a548ebc51b090b3816e2ae044aa6/plugin/5266ys/5266ys.go#L180-L216","documentation":"fetchSearch wraps an error from goquery.NewDocumentFromReader(strings.NewReader(decoded)) — parsing the UTF-8 HTML into a document failed. goquery only errors if the underlying charset reader cannot be constructed or the HTML parser fails on malformed input; in practice this is extremely rare because Go's HTML parser is lenient with broken markup.","triggerScenarios":"goquery.NewDocumentFromReader returns err: the decoded string is not valid input for the HTML parser — practically only when decodeGB18030 produced invalid UTF-8 or garbage rather than HTML, since goquery tolerates malformed tags.","commonSituations":"A previous decoding step (charset-decode-failed family) silently produced invalid UTF-8 that now breaks parsing; an anti-bot page with binary/JS-challenge content is fed to the parser; a goquery version regression on pathological input.","solutions":["Confirm decodeGB18030 output is valid UTF-8 (utf8.Valid) and log a prefix of the decoded string to see what is actually being parsed.","If invalid UTF-8 is possible, sanitize with strings.ToValidUTF8 before parsing.","Log the decoded body on failure to check whether the site is returning a challenge page instead of search results.","Upgrade goquery (github.com/PuerkitoBio/goquery) to the latest version if parsing genuinely fails on valid HTML."],"exampleFix":"// before\ndoc, err := goquery.NewDocumentFromReader(strings.NewReader(decoded))\nif err != nil {\n    return nil, fmt.Errorf(\"[%s] 解析搜索结果失败: %w\", p.Name(), err)\n}\n// after\nif !utf8.ValidString(decoded) {\n    decoded = strings.ToValidUTF8(decoded, \"\")\n}\ndoc, err := goquery.NewDocumentFromReader(strings.NewReader(decoded))\nif err != nil {\n    return nil, fmt.Errorf(\"[%s] 解析搜索结果失败: %w\", p.Name(), err)\n}","handlingStrategy":"validation","validationCode":"if !utf8.ValidString(decoded) {\n    decoded = strings.ToValidUTF8(decoded, \"\")\n}\n// 再传给 goquery","typeGuard":"func isParsableHTML(s string) bool {\n    return utf8.ValidString(s) && strings.Contains(strings.ToLower(s[:min(2048, len(s))]), \"<\")\n}","tryCatchPattern":"doc, err := goquery.NewDocumentFromReader(strings.NewReader(decoded))\nif err != nil {\n    // 记录 decoded 前 512 字节便于诊断, 然后降级为空结果\n    return nil, fmt.Errorf(\"解析失败: %w\", err)\n}","preventionTips":["Sanitize decoder output to valid UTF-8 before parsing.","Log page content on parse failure to detect challenge pages.","Keep goquery up to date.","Treat parse errors as a symptom of upstream (decode/network) problems."],"tags":["go","goquery","html-parsing"],"backgroundTag":"html-parse-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"}