{"record":{"id":"b3d05fb0e6b1e172","repo":"affaan-m/ECC","slug":"create-request-w","errorCode":null,"errorMessage":"create request: %w","messagePattern":"create request: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"skills/golang-patterns/SKILL.md","lineNumber":207,"sourceCode":"            }\n        }()\n    }\n\n    wg.Wait()\n    close(results)\n}\n```\n\n### Context for Cancellation and Timeouts\n\n```go\nfunc FetchWithTimeout(ctx context.Context, url string) ([]byte, error) {\n    ctx, cancel := context.WithTimeout(ctx, 5*time.Second)\n    defer cancel()\n\n    req, err := http.NewRequestWithContext(ctx, \"GET\", url, nil)\n    if err != nil {\n        return nil, fmt.Errorf(\"create request: %w\", err)\n    }\n\n    resp, err := http.DefaultClient.Do(req)\n    if err != nil {\n        return nil, fmt.Errorf(\"fetch %s: %w\", url, err)\n    }\n    defer resp.Body.Close()\n\n    return io.ReadAll(resp.Body)\n}\n```\n\n### Graceful Shutdown\n\n```go\nfunc GracefulShutdown(server *http.Server) {\n    quit := make(chan os.Signal, 1)\n    signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)","sourceCodeStart":189,"sourceCodeEnd":225,"githubUrl":"https://github.com/affaan-m/ECC/blob/01e15490f04e29cfefe3896951f43db46994d8ee/skills/golang-patterns/SKILL.md#L189-L225","documentation":"Wrapped error from FetchWithTimeout in golang-patterns. After building a context with timeout, http.NewRequestWithContext is called; if request construction fails the error is wrapped as fmt.Errorf(\"create request: %w\", err). This step does no networking - it only validates the method, url, and header values - so failure here means the inputs to the request were invalid, not that the server was unreachable.","triggerScenarios":"Calling FetchWithTimeout(ctx, url) when http.NewRequestWithContext returns an error. Concrete triggers: the url is empty, contains spaces or control characters, is missing a scheme/host, the method is invalid, or a header value contains a CR/LF or other disallowed octet.","commonSituations":"URL built from unsanitized user input; missing scheme (\"example.com\" instead of \"https://example.com\"); a header derived from user data that contains a newline (header injection); wrong HTTP method constant; copy-paste left a placeholder URL.","solutions":["Validate the URL with net/url.Parse and check u.Err / scheme + host before constructing the request.","Sanitize or reject any header value containing CR/LF or control characters.","Default the scheme to https when missing.","Treat request-construction errors as programmer/input errors (4xx-style), not transient failures."],"exampleFix":"// before\nreq, err := http.NewRequestWithContext(ctx, \"GET\", url, nil)\nif err != nil {\n    return nil, fmt.Errorf(\"create request: %w\", err)\n}\n\n// after\nu, err := url.Parse(url)\nif err != nil || u.Scheme == \"\" || u.Host == \"\" {\n    return nil, fmt.Errorf(\"invalid url %q: %w\", url, err)\n}\nreq, err := http.NewRequestWithContext(ctx, http.MethodGet, u.String(), nil)\nif err != nil {\n    return nil, fmt.Errorf(\"create request: %w\", err)\n}","handlingStrategy":"validation","validationCode":"func validateRequestInputs(method, urlStr string, headers http.Header) error {\n    if !validMethod(method) {\n        return fmt.Errorf(\"invalid method %q\", method)\n    }\n    u, err := url.Parse(urlStr)\n    if err != nil || u.Scheme == \"\" || u.Host == \"\" {\n        return fmt.Errorf(\"invalid url %q\", urlStr)\n    }\n    for k, vs := range headers {\n        for _, v := range vs {\n            if strings.ContainsAny(v, \"\\r\\n\") {\n                return fmt.Errorf(\"header %s contains CRLF\", k)\n            }\n        }\n    }\n    return nil\n}","typeGuard":"func isInvalidURL(err error) bool {\n    var ue *url.Error\n    return errors.As(err, &ue)\n}","tryCatchPattern":"req, err := http.NewRequestWithContext(ctx, method, urlStr, nil)\nif err != nil {\n    if isInvalidURL(err) {\n        return BadRequest(\"invalid url\")\n    }\n    return InternalError(err)\n}","preventionTips":["Parse and validate URLs with net/url before building requests.","Sanitize header values; reject CR/LF.","Default missing schemes to https.","Treat request-construction errors as input errors, not transient."],"tags":["go","http","validation","error-wrapping"],"backgroundTag":null,"analyzedSha":"01e15490f04e29cfefe3896951f43db46994d8ee","analyzedAt":"2026-08-13T00:31:08.655Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}