{"record":{"id":"161dcd24f1d88092","repo":"vxcontrol/pentagi","slug":"failed-to-create-request-w","errorCode":null,"errorMessage":"failed to create request: %w","messagePattern":"failed to create request: %w","errorType":"exception","errorClass":"FatalError","httpStatus":null,"severity":"error","filePath":"backend/pkg/tools/searchers/perplexity.go","lineNumber":174,"sourceCode":"\t\tSearchContextSize:      p.contextSize(),\n\t\tMaxTokens:              p.maxTokens(),\n\t\tTemperature:            p.temperature(),\n\t\tTopP:                   p.topP(),\n\t\tReturnImages:           false,\n\t\tReturnRelatedQuestions: false,\n\t\tStream:                 false,\n\t}\n\n\t// Serializing the request\n\treqBody, err := json.Marshal(reqPayload)\n\tif err != nil {\n\t\treturn \"\", Fatal(fmt.Errorf(\"failed to marshal request body: %w\", err))\n\t}\n\n\t// Creating HTTP request\n\treq, err := http.NewRequestWithContext(ctx, http.MethodPost, perplexityURL, bytes.NewBuffer(reqBody))\n\tif err != nil {\n\t\treturn \"\", Fatal(fmt.Errorf(\"failed to create request: %w\", err))\n\t}\n\n\t// Setting request headers\n\treq.Header.Set(\"Authorization\", \"Bearer \"+p.apiKey())\n\treq.Header.Set(\"Content-Type\", \"application/json\")\n\n\t// Sending the request\n\tresp, err := client.Do(req)\n\tif err != nil {\n\t\treturn \"\", Retryable(fmt.Errorf(\"failed to send request: %w\", err), 0)\n\t}\n\tdefer resp.Body.Close()\n\n\t// Handling the response. handleErrorResponse keeps the per-status message; the\n\t// retryable/fatal classification is decided here from the status code.\n\tif resp.StatusCode != http.StatusOK {\n\t\tbaseErr := p.handleErrorResponse(resp.StatusCode)\n\t\tif resp.StatusCode == http.StatusTooManyRequests || resp.StatusCode >= 500 {","sourceCodeStart":156,"sourceCodeEnd":192,"githubUrl":"https://github.com/vxcontrol/pentagi/blob/ea665308baaff015b226f308438a68d929d0f29b/backend/pkg/tools/searchers/perplexity.go#L156-L192","documentation":"`http.NewRequestWithContext` fails while constructing the POST to the Perplexity API, wrapped as Fatal at backend/pkg/tools/searchers/perplexity.go:174. With `perplexityURL` being a compile-time constant this essentially only fires if the URL constant/variable is invalid (bad method, unparsable URL, nil body on custom builds) or ctx is already built with an invalid state.","triggerScenarios":"`search` is called and `http.NewRequestWithContext(ctx, http.MethodPost, perplexityURL, ...)` returns an error — practically only when perplexityURL has been overridden to a malformed custom endpoint URL (invalid characters, missing scheme, control characters).","commonSituations":"Custom fork or patched build redirecting Perplexity traffic to a self-hosted gateway with a bad URL; misinterpreted build tag swapping the constant; URL copied with trailing spaces/newline.","solutions":["Inspect the wrapped error — NewRequest errors name the invalid URL component","If perplexityURL was made configurable, validate it with url.Parse and require http/https scheme at startup","Trim spaces/newlines from any configured endpoint URL","Nothing to fix at runtime on stock builds: verify you are on an unmodified binary"],"exampleFix":"// before\nendpoint := os.Getenv(\"PERPLEXITY_URL\") // may be \" https://x \" or schemeless\n// after\nendpoint := strings.TrimSpace(os.Getenv(\"PERPLEXITY_URL\"))\nif u, err := url.Parse(endpoint); err != nil || (u.Scheme != \"http\" && u.Scheme != \"https\") {\n    endpoint = perplexityURL // fall back to official API\n}","handlingStrategy":"validation","validationCode":"// validate the endpoint before constructing requests\nu, err := url.Parse(endpoint)\nif err != nil || (u.Scheme != \"http\" && u.Scheme != \"https\") || u.Host == \"\" {\n    endpoint = perplexityURL // fall back to official constant\n}","typeGuard":null,"tryCatchPattern":"_, err := engine.Handle(ctx, req)\nif err != nil && searchers.IsFatal(err) && strings.Contains(err.Error(), \"failed to create request\") {\n    // URL/method construction problem: check endpoint override, do not retry\n}","preventionTips":["Keep perplexityURL as a validated constant; if configurable, validate with url.Parse at startup","Trim whitespace/newlines from any configured endpoint","Add a startup request-construction smoke test","Do not inject user input into the endpoint URL"],"tags":["http","url","request-construction"],"backgroundTag":"invalid-url","analyzedSha":"ea665308baaff015b226f308438a68d929d0f29b","analyzedAt":"2026-09-01T14:16:31.421Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}