fish2018/pansou · error

[ ] 创建请求失败

Error message

[%s] 创建请求失败: %w

What it means

In searchAtBase, http.NewRequestWithContext failed while constructing the GET request for the search URL on a given mirror base. Like any NewRequest failure this is a malformed-URL / invalid-argument problem, not a network problem.

Solutions

  1. Escape query parameters with url.QueryEscape(keyword) before building searchURL.
  2. Validate the base URL with url.Parse and skip/reject malformed mirrors.
  3. Log searchURL on failure to see the exact malformed value.
  4. Ensure the keyword string is valid UTF-8 and free of control characters.

Example fix

// before
searchURL := baseURL + "/search.php?keyword=" + keyword
req, err := http.NewRequestWithContext(ctx, "GET", searchURL, nil)
// after
searchURL := baseURL + "/search.php?keyword=" + url.QueryEscape(keyword)
req, err := http.NewRequestWithContext(ctx, "GET", searchURL, nil)
Defensive patterns

Strategy: validation

Validate before calling

u, err := url.Parse(baseURL)
if err != nil || u.Scheme == "" || u.Host == "" {
    return fmt.Errorf("invalid mirror base: %q", baseURL)
}
searchURL := *u
searchURL.Path = path.Join(u.Path, "search.php")
q := searchURL.Query()
q.Set("keyword", keyword) // QueryEscape handled by Values.Encode
searchURL.RawQuery = q.Encode()

Type guard

func isValidURL(s string) bool {
    u, err := url.Parse(s)
    return err == nil && u.Scheme != "" && u.Host != ""
}

Try / catch

results, err := p.Search(keyword, ext)
if err != nil {
    if strings.Contains(err.Error(), "创建请求失败") {
        return errors.New("search URL construction failed — check keyword encoding / mirror base")
    }
    return err
}

Prevention

When it happens

Trigger: searchURL built from a base URL plus query parameters contains invalid characters (unescaped Chinese keywords, spaces, control characters) or the base URL itself is malformed, causing http.NewRequestWithContext to reject it.

Common situations: Keyword containing spaces or special characters interpolated without url.QueryEscape; a stale/malformed mirror base in the config; corrupted encoding of the keyword.

Understand the failure class

Background: "Invalid URL" / "URL cannot be empty": fix the malformed or missing URL behind request-construction failures — this error's family across 50 libraries.

Related errors


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

Appendix: source

Thrown at plugin/huban/huban.go:251

	// 8. 异步获取详情页信息
	enhancedResults := p.enhanceWithDetails(client, results, selectedBase)

	// 9. 关键词过滤
	return plugin.FilterResultsByKeyword(enhancedResults, keyword), nil
}

func (p *HubanAsyncPlugin) searchAtBase(client *http.Client, keyword, baseURL string) ([]model.SearchResult, error) {
	searchURL := fmt.Sprintf("%s/index.php/vod/search/wd/%s.html", strings.TrimRight(baseURL, "/"), url.QueryEscape(keyword))

	// 2. 创建带超时的上下文
	ctx, cancel := context.WithTimeout(context.Background(), DefaultTimeout)
	defer cancel()

	// 3. 创建请求
	req, err := http.NewRequestWithContext(ctx, "GET", searchURL, nil)
	if err != nil {
		return nil, fmt.Errorf("[%s] 创建请求失败: %w", p.Name(), err)
	}

	// 4. 设置请求头
	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", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8")
	req.Header.Set("Accept-Language", "zh-CN,zh;q=0.9,en;q=0.8")
	req.Header.Set("Connection", "keep-alive")
	req.Header.Set("Referer", strings.TrimRight(baseURL, "/")+"/")

	// 5. 发送请求
	resp, err := p.doRequestWithRetry(req, client)
	if err != nil {
		return nil, fmt.Errorf("[%s] 搜索请求失败: %w", p.Name(), err)
	}
	defer resp.Body.Close()

	if resp.StatusCode != 200 {
		return nil, fmt.Errorf("[%s] 搜索请求返回状态码: %d", p.Name(), resp.StatusCode)

View on GitHub (pinned to beaa561337)