fish2018/pansou · error

[ ] 创建请求失败

Error message

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

What it means

In labi's searchAtBase, http.NewRequestWithContext failed while building the GET request for the given mirror's search URL. The request never left the machine; the URL string itself was rejected by net/http (bad scheme, invalid characters, unparseable host).

Solutions

  1. URL-encode the keyword (url.QueryEscape) before building searchURL
  2. Trim and validate baseURL (strings.TrimSpace, must start with http:// or https://) before constructing the request
  3. Log the exact searchURL next to the wrapped error to see the offending string
  4. Fix or remove the broken mirror entry from the base list

Example fix

// before
searchURL := baseURL + "/search.php?searchname=" + keyword
// after
searchURL := strings.TrimRight(baseURL, "/") + "/search.php?searchname=" + url.QueryEscape(keyword)
Defensive patterns

Strategy: validation

Validate before calling

if !strings.HasPrefix(strings.TrimSpace(baseURL), "http") || url.QueryEscape(keyword) == "" {
    // reject before searchAtBase builds the request
}

Type guard

func validBaseURL(s string) bool {
    u, err := url.Parse(strings.TrimSpace(s))
    return err == nil && (u.Scheme == "http" || u.Scheme == "https") && u.Host != ""
}

Try / catch

if err != nil {
    var urlErr *url.Error
    if errors.As(err, &urlErr) {
        log.Printf("invalid mirror URL %q: %v", urlErr.URL, urlErr.Err)
    }
    return nil
}

Prevention

When it happens

Trigger: http.NewRequestWithContext(ctx, "GET", searchURL, nil) errors because searchURL built from baseURL plus the keyword is malformed — e.g. baseURL missing scheme, keyword containing spaces/control characters, or an empty URL string.

Common situations: A mirror base URL configured or hard-coded without 'https://', keyword not url.QueryEscape'd before joining the search path, whitespace/newline in a scraped base URL.

Understand the failure class

Background: "Invalid URL" errors: why new URL(), URI.parse, and reqwest::Url reject your string — missing scheme, whitespace, and bad path format — this error's family across 39 libraries.

Related errors


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

Appendix: source

Thrown at plugin/labi/labi.go:184

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

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

func (p *LabiAsyncPlugin) 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("Upgrade-Insecure-Requests", "1")
	req.Header.Set("Cache-Control", "max-age=0")
	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()

View on GitHub (pinned to beaa561337)