{"record":{"id":"b2e429a9e942426e","repo":"micro/go-micro","slug":"api-request-failed-w","errorCode":null,"errorMessage":"API request failed: %w","messagePattern":"API request failed: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"ai/anthropic/anthropic.go","lineNumber":374,"sourceCode":"\t\treturn nil, nil, fmt.Errorf(\"failed to marshal request: %w\", err)\n\t}\n\n\t// Build HTTP request\n\tapiURL := strings.TrimRight(p.opts.BaseURL, \"/\") + \"/v1/messages\"\n\thttpReq, err := http.NewRequestWithContext(ctx, http.MethodPost, apiURL, bytes.NewReader(reqBody))\n\tif err != nil {\n\t\treturn nil, nil, fmt.Errorf(\"failed to create request: %w\", err)\n\t}\n\n\t// Set headers\n\thttpReq.Header.Set(\"Content-Type\", \"application/json\")\n\thttpReq.Header.Set(\"x-api-key\", p.opts.APIKey)\n\thttpReq.Header.Set(\"anthropic-version\", \"2023-06-01\")\n\n\t// Make request\n\thttpResp, err := http.DefaultClient.Do(httpReq)\n\tif err != nil {\n\t\treturn nil, nil, fmt.Errorf(\"API request failed: %w\", err)\n\t}\n\tdefer httpResp.Body.Close()\n\n\t// Read response\n\trespBody, err := io.ReadAll(httpResp.Body)\n\tif err != nil {\n\t\treturn nil, nil, fmt.Errorf(\"failed to read response: %w\", err)\n\t}\n\tif httpResp.StatusCode != http.StatusOK {\n\t\treturn nil, nil, ai.NewHTTPError(httpResp, respBody)\n\t}\n\n\t// Parse response\n\tvar anthropicResp struct {\n\t\tContent []struct {\n\t\t\tType  string          `json:\"type\"`\n\t\t\tText  string          `json:\"text\"`\n\t\t\tID    string          `json:\"id\"`","sourceCodeStart":356,"sourceCodeEnd":392,"githubUrl":"https://github.com/micro/go-micro/blob/24529f140421a11a33b6999ab7944f2021cfd69c/ai/anthropic/anthropic.go#L356-L392","documentation":"Returned by Provider.callAPI when the HTTP transport fails executing the POST to /v1/messages — the request did not complete. The wrapped error is a *url.Error containing the underlying cause (DNS, TLS, timeout, connection refused, or context cancellation).","triggerScenarios":"http.DefaultClient.Do returns an error: no network/DNS failure, TLS handshake failure, connection refused (wrong port on a self-hosted proxy), client timeout, or the caller's ctx was cancelled or exceeded its deadline.","commonSituations":"Offline environments or missing HTTPS_PROXY in corporate networks, local Anthropic-compatible proxies (LiteLLM, Ollama gateways) on the wrong host/port, short context deadlines exceeded by long generations.","solutions":["Test connectivity to the endpoint directly: curl -v $BASE_URL/v1/messages.","Check errors.Is(err, context.DeadlineExceeded) to distinguish timeout from other transport failures and raise the deadline if needed.","Verify proxy settings (HTTPS_PROXY/HTTP_PROXY) or configure a custom http.Client with the right transport.","If pointing at a local gateway, confirm the port and that the service is actually listening (connection refused)."],"exampleFix":"// before\nctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)\nresp, err := provider.Generate(ctx, prompt)\n// after\nctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)\nresp, err := provider.Generate(ctx, prompt)\nif err != nil && errors.Is(err, context.DeadlineExceeded) { /* retry with longer budget */ }","handlingStrategy":"retry","validationCode":"func endpointReachable(base string) error {\n\tu, _ := url.Parse(strings.TrimSpace(base))\n\tc, err := net.DialTimeout(\"tcp\", net.JoinHostPort(u.Hostname(), portOr(u, \"443\")), 5*time.Second)\n\tif err != nil { return err }\n\tc.Close(); return nil\n}","typeGuard":null,"tryCatchPattern":"resp, err := provider.Generate(ctx, prompt)\nif err != nil {\n\tswitch {\n\tcase errors.Is(err, context.DeadlineExceeded):\n\t\t// increase timeout, retry\n\tcase errors.Is(err, context.Canceled):\n\t\t// caller cancelled: don't retry\n\tdefault:\n\t\t// transport error: retry once, then surface with cause\n\t}\n}","preventionTips":["Use deadlines sized for model latency (tens of seconds+)","Verify proxy env vars in restricted networks","For local gateways, confirm host/port and that the service is listening","Use a custom http.Client with explicit timeouts instead of http.DefaultClient"],"tags":["network","http","timeout","anthropic"],"backgroundTag":"http-request-failed","analyzedSha":"24529f140421a11a33b6999ab7944f2021cfd69c","analyzedAt":"2026-09-01T02:52:24.923Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T05:18:18.240Z"}