Tencent/WeKnora · error

failed to create request: %w

Error message

failed to create request: %w

What it means

http.NewRequestWithContext failed while constructing the DuckDuckGo HTML GET request in searchHTML, and the error is wrapped with "failed to create request". This happens before any network I/O, when the request URL cannot be parsed.

Source

Thrown at internal/infrastructure/web_search/duckduckgo.go:80

	}
	return nil, fmt.Errorf("duckduckgo API search failed: %w", apiErr)
}

// searchHTML performs a web search using DuckDuckGo HTML endpoint
func (p *DuckDuckGoProvider) searchHTML(
	ctx context.Context,
	query string,
	maxResults int,
) ([]*types.WebSearchResult, error) {
	baseURL := "https://html.duckduckgo.com/html/"
	params := url.Values{}
	params.Set("q", query)
	params.Set("kl", "cn-zh")

	reqURL := baseURL + "?" + params.Encode()
	req, err := http.NewRequestWithContext(ctx, "GET", reqURL, nil)
	if err != nil {
		return nil, fmt.Errorf("failed to create request: %w", err)
	}
	req.Header.Set(
		"User-Agent",
		"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36",
	)

	curlCommand := fmt.Sprintf(
		"curl -X GET '%s' -H 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36'",
		req.URL.String(),
	)
	logger.Infof(ctx, "Curl of request: %s", secutils.SanitizeForLog(curlCommand))

	resp, err := p.client.Do(req)
	if err != nil {
		return nil, fmt.Errorf("failed to perform request: %w", err)
	}
	defer resp.Body.Close()

View on GitHub (pinned to 988cbb0330)

Solutions

  1. Read the wrapped error to identify the invalid URL part.
  2. URL-encode the query (url.QueryEscape) before placing it in params, and validate baseURL is an absolute https URL.
  3. Ensure the passed ctx is not already canceled.

Example fix

// before
params.Set("q", query)
// after
params.Set("q", url.QueryEscape(query))
if _, err := url.Parse(baseURL); err != nil {
    return nil, fmt.Errorf("invalid baseURL: %w", err)
}
Defensive patterns

Strategy: validation

Validate before calling

escaped := url.QueryEscape(query)
if escaped == "" || len(escaped) > 2000 {
    return errors.New("query missing or too long")
}

Type guard

func isRequestBuildErr(err error) bool {
    return err != nil && strings.Contains(err.Error(), "failed to create request")
}

Try / catch

results, err := ddg.Search(ctx, query, 10, false)
if isRequestBuildErr(err) {
    return fmt.Errorf("duckduckgo request construction failed (check baseURL/context): %w", err)
}

Prevention

When it happens

Trigger: baseURL + "?" + params.Encode() produces a malformed URL — e.g. query containing characters that break parsing, or an invalid/blank baseURL constant.

Common situations: Extremely large or binary-corrupted query strings; baseURL changed to an invalid value; context already canceled at call time.

Related errors


AI-assisted analysis of Tencent/WeKnora@988cbb0330 (2026-09-02). Data as JSON: /api/errors/ab50573f405929a1. Report an issue: GitHub.