fish2018/pansou · error

创建请求失败

Error message

创建请求失败: %w

What it means

In getBuildId (pansearch plugin), http.NewRequest fails while trying to fetch the site's buildId. The function normally falls back to a cached buildId for graceful degradation, but if no cache exists it returns this wrapped error.

Solutions

  1. Validate the pansearch base URL is a well-formed absolute http(s) URL
  2. Ensure the context passed in is not already canceled/deadlined-exceeded
  3. Pre-warm buildIdCache by calling getBuildId once at startup so fallback exists later
  4. Retry after fixing configuration; the error is deterministic until the URL or context is fixed

Example fix

// before
buildId, err := getBuildId(ctx)
if err != nil { return "", err }
// after
u, err := url.Parse(siteBaseURL)
if err != nil || u.Scheme == "" {
    return "", fmt.Errorf("invalid pansearch base URL: %q", siteBaseURL)
}
buildId, err := getBuildId(ctx)
if err != nil { return "", err }
Defensive patterns

Strategy: validation

Validate before calling

u, err := url.Parse(siteBaseURL)
if err != nil || (u.Scheme != "http" && u.Scheme != "https") || u.Host == "" {
    return fmt.Errorf("invalid site URL: %q", siteBaseURL)
}
if ctx.Err() != nil {
    return ctx.Err()
}

Try / catch

buildId, err := getBuildId(ctx)
if err != nil {
    if strings.Contains(err.Error(), "创建请求失败") {
        return fmt.Errorf("buildId fetch could not start (bad URL/context): %w", err)
    }
    return err
}

Prevention

When it happens

Trigger: http.NewRequestWithContext returns an error for the site URL — malformed URL, unsupported scheme, or context already canceled before request creation — and buildIdCache is empty.

Common situations: Site base URL misconfigured (invalid characters/spaces in URL); caller-supplied context canceled or deadline exceeded before the request starts; first run with no cached buildId and a bad configured URL.

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/a55ee4f09440465b. Report an issue: GitHub.

Appendix: source

Thrown at plugin/pansearch/pansearch.go:372

	// 双重检查
	if buildIdCache != "" && time.Since(buildIdCacheTime) < BuildIdCacheDuration*time.Minute {
		return buildIdCache, nil
	}

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

	// 发送请求获取页面
	req, err := http.NewRequestWithContext(ctx, "GET", WebsiteURL, nil)
	if err != nil {
		// 如果创建请求失败但有旧的缓存,使用旧的缓存(优雅降级)
		if buildIdCache != "" {
			// fmt.Printf("创建请求失败,使用旧的buildId: %v\n", err)
			return buildIdCache, nil
		}
		return "", fmt.Errorf("创建请求失败: %w", err)
	}

	// 设置完整的请求头
	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")

	client = p.requestClient(client)

	// 使用重试机制发送请求
	var resp *http.Response
	var respErr error

	for retry := 0; retry <= p.retries; retry++ {
		if retry > 0 {

View on GitHub (pinned to beaa561337)