{"record":{"id":"3696a07d17efe8f8","repo":"charmbracelet/crush","slug":"failed-to-create-request-w-3696a0","errorCode":null,"errorMessage":"failed to create request: %w","messagePattern":"failed to create request: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"info","filePath":"internal/agent/tools/sourcegraph.go","lineNumber":117,"sourceCode":"\t\t\trequest := graphqlRequest{\n\t\t\t\tQuery: \"query Search($query: String!) { search(query: $query, version: V2, patternType: keyword ) { results { matchCount, limitHit, resultCount, approximateResultCount, missing { name }, timedout { name }, indexUnavailable, results { __typename, ... on FileMatch { repository { name }, file { path, url, content }, lineMatches { preview, lineNumber, offsetAndLengths } } } } } }\",\n\t\t\t}\n\t\t\trequest.Variables.Query = params.Query\n\n\t\t\tgraphqlQueryBytes, err := json.Marshal(request)\n\t\t\tif err != nil {\n\t\t\t\treturn fantasy.ToolResponse{}, fmt.Errorf(\"failed to marshal GraphQL request: %w\", err)\n\t\t\t}\n\t\t\tgraphqlQuery := string(graphqlQueryBytes)\n\n\t\t\treq, err := http.NewRequestWithContext(\n\t\t\t\trequestCtx,\n\t\t\t\t\"POST\",\n\t\t\t\t\"https://sourcegraph.com/.api/graphql\",\n\t\t\t\tbytes.NewBuffer([]byte(graphqlQuery)),\n\t\t\t)\n\t\t\tif err != nil {\n\t\t\t\treturn fantasy.ToolResponse{}, fmt.Errorf(\"failed to create request: %w\", err)\n\t\t\t}\n\n\t\t\treq.Header.Set(\"Content-Type\", \"application/json\")\n\t\t\treq.Header.Set(\"User-Agent\", \"crush/1.0\")\n\n\t\t\tresp, err := client.Do(req)\n\t\t\tif err != nil {\n\t\t\t\treturn fantasy.ToolResponse{}, fmt.Errorf(\"failed to fetch URL: %w\", err)\n\t\t\t}\n\t\t\tdefer resp.Body.Close()\n\n\t\t\tif resp.StatusCode != http.StatusOK {\n\t\t\t\tbody, _ := io.ReadAll(resp.Body)\n\t\t\t\tif len(body) > 0 {\n\t\t\t\t\treturn fantasy.NewTextErrorResponse(fmt.Sprintf(\"Request failed with status code: %d, response: %s\", resp.StatusCode, string(body))), nil\n\t\t\t\t}\n\n\t\t\t\treturn fantasy.NewTextErrorResponse(fmt.Sprintf(\"Request failed with status code: %d\", resp.StatusCode)), nil","sourceCodeStart":99,"sourceCodeEnd":135,"githubUrl":"https://github.com/charmbracelet/crush/blob/7944b8e52225d8805e31eacbf7ef24856b0dfb7a/internal/agent/tools/sourcegraph.go#L99-L135","documentation":"Wraps an http.NewRequestWithContext error when constructing the POST to https://sourcegraph.com/.api/graphql (internal/agent/tools/sourcegraph.go:110-118). This fires only for an invalid HTTP method, an unparseable URL, or a nil context/URL mismatch — none of which occur here since method, URL, and context are hardcoded/derived correctly. It is effectively a defensive branch.","triggerScenarios":"http.NewRequestWithContext returns an error — requires an invalid context (nil), a malformed URL, or an invalid method; with the hardcoded \"POST\" and fixed URL this is unreachable in normal operation.","commonSituations":"Essentially never hit by users; could only appear if the source were modified to use a configurable endpoint with a malformed URL string (e.g. missing scheme, control characters).","solutions":["No user action needed; treat as a bug report trigger if ever observed.","If self-hosting a modified build with a custom endpoint, validate the endpoint URL parses (url.Parse) at startup."],"exampleFix":"// before (if endpoint becomes configurable)\nreq, err := http.NewRequestWithContext(requestCtx, \"POST\", endpoint, body)\n\n// after\nu, perr := url.Parse(endpoint)\nif perr != nil || u.Scheme == \"\" || u.Host == \"\" {\n    return fantasy.ToolResponse{}, fmt.Errorf(\"invalid sourcegraph endpoint: %q\", endpoint)\n}\nreq, err := http.NewRequestWithContext(requestCtx, \"POST\", endpoint, body)","handlingStrategy":"validation","validationCode":"u, err := url.Parse(endpoint)\nif err != nil || u.Scheme == \"\" || u.Host == \"\" {\n    return fmt.Errorf(\"invalid endpoint URL: %q\", endpoint)\n}","typeGuard":null,"tryCatchPattern":"req, err := http.NewRequestWithContext(ctx, http.MethodPost, endpoint, body)\nif err != nil {\n    return fmt.Errorf(\"failed to create request: %w\", err)\n}","preventionTips":["Validate any configurable endpoint with url.Parse at startup.","Keep method constants (http.MethodPost) rather than ad-hoc strings.","Never pass a nil context into http.NewRequestWithContext."],"tags":["http","request","go"],"backgroundTag":"invalid-http-request","analyzedSha":"7944b8e52225d8805e31eacbf7ef24856b0dfb7a","analyzedAt":"2026-08-29T12:48:59.079Z","schemaVersion":2},"datasetVersion":"2026-08-29T17:17:51.833Z"}