fish2018/pansou · error

创建搜索请求失败

Error message

创建搜索请求失败: %w

What it means

Wraps an error from http.NewRequestWithContext when building the GET request to the xiaokupan search endpoint. Since the URL was already parsed successfully one line earlier, this indicates an invalid method, malformed context, or an invalid URL that passed Parse but fails NewRequest's stricter checks (e.g. nil/invalid URL host).

Solutions

  1. Check that the final endpoint URL includes scheme and host — a schemeless baseURL is the usual cause
  2. Configure baseURL as a full absolute URL starting with https://
  3. Log parsedEndpoint.String() to see exactly what was passed to NewRequestWithContext
  4. Verify the context passed in is not already canceled

Example fix

// before
baseURL := "xiaokupan.com" // schemeless
// after
baseURL := "https://xiaokupan.com"
Defensive patterns

Strategy: validation

Validate before calling

u, _ := url.Parse(endpoint)
if u.Scheme == "" || u.Host == "" {
    return errors.New("endpoint missing scheme/host; baseURL must be absolute (https://...)")
}

Type guard

func isAbsoluteURL(u *url.URL) bool { return u.IsAbs() && u.Host != "" }

Try / catch

req, err := http.NewRequestWithContext(ctx, http.MethodGet, parsedEndpoint.String(), nil)
if err != nil {
    log.Printf("request build failed for %s: %v", parsedEndpoint.String(), err)
    return err
}

Prevention

When it happens

Trigger: http.NewRequestWithContext returns an error — usually because the parsed endpoint URL is missing a scheme/host (baseURL like "xiaokupan.com" without https:// survives url.Parse but yields an unusable request URL), or the context is invalid.

Common situations: baseURL configured without a scheme ("xiaokupan.com" instead of "https://xiaokupan.com"); endpoint string contains characters NewRequest rejects; programming error passing a canceled context.

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

Appendix: source

Thrown at plugin/xiaokupan/xiaokupan.go:129

	if err != nil {
		return nil, fmt.Errorf("构造搜索参数失败: %w", err)
	}

	endpoint := fmt.Sprintf("%s/_serverFn/%s", strings.TrimRight(p.baseURL, "/"), functionID)
	parsedEndpoint, err := url.Parse(endpoint)
	if err != nil {
		return nil, fmt.Errorf("解析搜索地址失败: %w", err)
	}
	query := parsedEndpoint.Query()
	query.Set("payload", string(payload))
	parsedEndpoint.RawQuery = query.Encode()

	ctx, cancel := context.WithTimeout(context.Background(), requestTimeout)
	defer cancel()

	req, err := http.NewRequestWithContext(ctx, http.MethodGet, parsedEndpoint.String(), nil)
	if err != nil {
		return nil, fmt.Errorf("创建搜索请求失败: %w", err)
	}
	p.setSearchHeaders(req, keyword)

	body, err := doLimitedRequest(client, req, maxSearchResponseSize)
	if err != nil {
		return nil, err
	}
	return parseSearchResponse(body)
}

func buildSearchPayload(keyword string) ([]byte, error) {
	payload := map[string]interface{}{
		"t": map[string]interface{}{
			"t": 10,
			"i": 0,
			"p": map[string]interface{}{
				"k": []string{"data"},
				"v": []interface{}{map[string]interface{}{

View on GitHub (pinned to beaa561337)