{"record":{"id":"1cf6beea591c9155","repo":"fish2018/pansou","slug":"create-request-failed-w-1cf6be","errorCode":null,"errorMessage":"create request failed: %w","messagePattern":"create request failed: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"plugin/yunso/yunso.go","lineNumber":158,"sourceCode":"func (p *YunsoAsyncPlugin) searchPage(client *http.Client, keyword string, page int) ([]YunsoItem, error) {\n\tctx, cancel := context.WithTimeout(context.Background(), yunsoDefaultTimeout)\n\tdefer cancel()\n\n\tparams := url.Values{}\n\tparams.Set(\"requestID\", \"\")\n\tparams.Set(\"mode\", yunsoDefaultMode)\n\tparams.Set(\"scope_content\", yunsoDefaultScope)\n\tparams.Set(\"stype\", \"\")\n\tparams.Set(\"wd\", keyword)\n\tparams.Set(\"uk\", \"\")\n\tparams.Set(\"page\", strconv.Itoa(page))\n\tparams.Set(\"limit\", strconv.Itoa(yunsoDefaultPageSize))\n\tparams.Set(\"screen_filetype\", \"\")\n\n\tsearchURL := yunsoSearchAPI + \"?\" + params.Encode()\n\treq, err := http.NewRequestWithContext(ctx, http.MethodPost, searchURL, nil)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"create request failed: %w\", err)\n\t}\n\n\treferer := yunsoSearchPage + \"?wd=\" + url.QueryEscape(keyword)\n\treq.Header.Set(\"Accept\", \"application/json, text/plain, */*\")\n\treq.Header.Set(\"Accept-Language\", \"zh-CN,zh;q=0.9,en;q=0.8\")\n\treq.Header.Set(\"Origin\", \"https://www.yunso.net\")\n\treq.Header.Set(\"Referer\", referer)\n\treq.Header.Set(\"User-Agent\", \"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/136.0.0.0 Safari/537.36\")\n\treq.Header.Set(\"X-Requested-With\", \"XMLHttpRequest\")\n\n\tresp, err := client.Do(req)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"request failed: %w\", err)\n\t}\n\tdefer resp.Body.Close()\n\n\tif resp.StatusCode != http.StatusOK {\n\t\treturn nil, fmt.Errorf(\"unexpected status code: %d\", resp.StatusCode)","sourceCodeStart":140,"sourceCodeEnd":176,"githubUrl":"https://github.com/fish2018/pansou/blob/beaa56133755a548ebc51b090b3816e2ae044aa6/plugin/yunso/yunso.go#L140-L176","documentation":"searchPage failed at http.NewRequestWithContext, meaning the outbound POST to the yunso search API could not be constructed. Because the URL is built from a fixed template plus query parameters, this almost always indicates a malformed URL (bad characters in params.Encode() output) or a nil/invalid context.","triggerScenarios":"http.NewRequestWithContext returns an error when constructing the request to yunsoSearchAPI — e.g. invalid URL syntax after appending encoded params, or a canceled context passed in.","commonSituations":"Keyword or config value introduces characters that break URL construction, a parent context is already canceled before the request is built, or the API base URL constant was edited incorrectly.","solutions":["Log the constructed searchURL and verify it parses (url.Parse)","Check the incoming ctx is not already canceled before calling searchPage","Validate yunsoSearchAPI constant is a complete, valid absolute URL","Use url.Values encoding for the keyword instead of raw concatenation"],"exampleFix":"// before\nreq, err := http.NewRequestWithContext(ctx, http.MethodPost, searchURL, nil)\nif err != nil {\n    return nil, fmt.Errorf(\"create request failed: %w\", err)\n}\n// after\nif err := ctx.Err(); err != nil {\n    return nil, fmt.Errorf(\"context canceled before request: %w\", err)\n}\nif _, perr := url.Parse(searchURL); perr != nil {\n    return nil, fmt.Errorf(\"invalid search url %q: %w\", searchURL, perr)\n}\nreq, err := http.NewRequestWithContext(ctx, http.MethodPost, searchURL, nil)\nif err != nil {\n    return nil, fmt.Errorf(\"create request failed: %w\", err)\n}","handlingStrategy":"validation","validationCode":"u, err := url.Parse(searchURL)\nif err != nil || u.Scheme == \"\" || u.Host == \"\" {\n    return fmt.Errorf(\"invalid search url: %q\", searchURL)\n}\nif ctx.Err() != nil {\n    return ctx.Err()\n}","typeGuard":"func validURL(s string) bool {\n    u, err := url.Parse(s)\n    return err == nil && u.Scheme != \"\" && u.Host != \"\"\n}","tryCatchPattern":"items, err := searchPage(ctx, client, keyword, page)\nif err != nil && strings.Contains(err.Error(), \"create request failed\") {\n    log.Error(\"bad request construction\", \"err\", err)\n    return ErrBadConfig\n}","preventionTips":["Always build URLs with url.Values/url.PathEscape, never raw string concat","Validate the base URL constant once at plugin init","Check ctx.Err() before issuing requests"],"tags":["http","url","request-construction"],"backgroundTag":"invalid-url","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"}