{"record":{"id":"a2bedf5993f9364e","repo":"Tencent/WeKnora","slug":"failed-to-execute-request-w-a2bedf","errorCode":null,"errorMessage":"failed to execute request: %w","messagePattern":"failed to execute request: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/infrastructure/web_search/tavily.go","lineNumber":86,"sourceCode":"\t\tAPIKey:     p.apiKey,\n\t\tQuery:      query,\n\t\tMaxResults: maxResults,\n\t}\n\n\tbodyBytes, err := json.Marshal(reqBody)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"failed to marshal request: %w\", err)\n\t}\n\n\treq, err := http.NewRequestWithContext(ctx, \"POST\", p.baseURL, bytes.NewReader(bodyBytes))\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"failed to create request: %w\", err)\n\t}\n\treq.Header.Set(\"Content-Type\", \"application/json\")\n\n\tresp, err := p.client.Do(req)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"failed to execute request: %w\", err)\n\t}\n\tdefer resp.Body.Close()\n\n\tif resp.StatusCode != http.StatusOK {\n\t\trespBody, _ := io.ReadAll(resp.Body)\n\t\tlogger.Warnf(ctx, \"[WebSearch][Tavily] API returned status %d: %s\", resp.StatusCode, string(respBody))\n\t\treturn nil, fmt.Errorf(\"tavily API returned status %d: %s\", resp.StatusCode, string(respBody))\n\t}\n\n\trespBody, err := io.ReadAll(resp.Body)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"failed to read response: %w\", err)\n\t}\n\n\tvar respData tavilySearchResponse\n\tif err := json.Unmarshal(respBody, &respData); err != nil {\n\t\treturn nil, fmt.Errorf(\"failed to unmarshal response: %w\", err)\n\t}","sourceCodeStart":68,"sourceCodeEnd":104,"githubUrl":"https://github.com/Tencent/WeKnora/blob/988cbb03305e055d8ebb7d46d9ac6cc0803cd074/internal/infrastructure/web_search/tavily.go#L68-L104","documentation":"Tavily web search provider wraps any network-layer failure from the HTTP client (p.client.Do) with this error. It means the HTTP request to the Tavily API could not be executed at all — connection setup, DNS, TLS, or transport failure — not an HTTP error status. The original error is preserved via %w for errors.Is/As inspection.","triggerScenarios":"Calling provider.Search when the network to api.tavily.com is unreachable: DNS resolution failure, connection refused/timeout, TLS handshake error, or a cancelled/expired context before the response is received.","commonSituations":"No internet or firewall/proxy blocking outbound HTTPS, misconfigured HTTP proxy env vars, transient DNS outages in containers, request context cancelled by an upstream timeout, or an invalid custom p.client configuration.","solutions":["Check network connectivity and DNS resolution to api.tavily.com (curl -v https://api.tavily.com).","Verify proxy/firewall settings allow outbound HTTPS (HTTPS_PROXY, HTTP_PROXY env vars).","Inspect the wrapped error with errors.Is(err, context.DeadlineExceeded) / net.Error to identify timeouts vs connection failures.","Increase client timeout or retry with backoff for transient network faults; ensure the context deadline is generous enough."],"exampleFix":"// before\nresults, err := provider.Search(ctx, query, 5, false)\nif err != nil { return err }\n// after\nresults, err := provider.Search(ctx, query, 5, false)\nif err != nil {\n    var netErr net.Error\n    if errors.As(err, &netErr) && netErr.Timeout() {\n        return retryWithBackoff(ctx)\n    }\n    return fmt.Errorf(\"tavily search failed: %w\", err)\n}","handlingStrategy":"retry","validationCode":"// before calling\nif err := ctx.Err(); err != nil { return fmt.Errorf(\"context already cancelled: %w\", err) }\n// optionally probe connectivity:\nconn, err := net.DialTimeout(\"tcp\", \"api.tavily.com:443\", 3*time.Second)\nif err != nil { return fmt.Errorf(\"tavily unreachable: %w\", err) }\nconn.Close()","typeGuard":"func isNetworkErr(err error) bool {\n    var netErr net.Error\n    if errors.As(err, &netErr) { return true }\n    var dnsErr *net.DNSError\n    return errors.As(err, &dnsErr)\n}","tryCatchPattern":"results, err := provider.Search(ctx, q, 5, false)\nif err != nil {\n    if strings.Contains(err.Error(), \"failed to execute request\") {\n        // transient network problem: retry with backoff\n        results, err = retry(ctx, 3, func() error { _, err := provider.Search(ctx, q, 5, false); return err })\n    }\n    if err != nil { return err }\n}","preventionTips":["Set a generous context deadline and honor cancellation","Configure reasonable client timeouts and connection pooling","Verify egress/proxy allowlists for api.tavily.com in your deployment","Use errors.Is/errors.As on the wrapped error to classify before retrying"],"tags":["network","http-client","web-search","tavily"],"backgroundTag":"http-request-failed","analyzedSha":"988cbb03305e055d8ebb7d46d9ac6cc0803cd074","analyzedAt":"2026-09-02T14:41:08.344Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}