{"record":{"id":"5e24c675bcb9c19e","repo":"juicedata/juicefs","slug":"error-creating-get-request-v","errorCode":null,"errorMessage":"error creating GET request: %v","messagePattern":"error creating GET request: (.+?)","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"cmd/debug.go","lineNumber":227,"sourceCode":"\t\t\t\t\tlistenPort = port\n\t\t\t\t}\n\t\t\t\tcontinue\n\t\t\t}\n\t\t}\n\t}\n\n\tif listenPort == -1 {\n\t\treturn 0, fmt.Errorf(\"no valid pprof port found\")\n\t}\n\treturn listenPort, nil\n}\n\nfunc getRequest(url string, timeout time.Duration) ([]byte, error) {\n\tctx, cancel := context.WithTimeout(context.Background(), timeout)\n\tdefer cancel()\n\treq, err := http.NewRequestWithContext(ctx, \"GET\", url, nil)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"error creating GET request: %v\", err)\n\t}\n\tresp, err := http.DefaultClient.Do(req)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"error GET request: %v\", err)\n\t}\n\tif resp.StatusCode != 200 {\n\t\treturn nil, fmt.Errorf(\"error GET request, status code %d\", resp.StatusCode)\n\t}\n\n\tdefer func(body io.ReadCloser) {\n\t\tif err := body.Close(); err != nil {\n\t\t\tlogger.Errorf(\"error closing body: %v\", err)\n\t\t}\n\t}(resp.Body)\n\tbody, err := io.ReadAll(resp.Body)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"error reading response: %v\", err)\n\t}","sourceCodeStart":209,"sourceCodeEnd":245,"githubUrl":"https://github.com/juicedata/juicefs/blob/c9a67b23e8e08ec23ec331aa6f1675e2319e921c/cmd/debug.go#L209-L245","documentation":"getRequest in cmd/debug.go builds an http GET with a context timeout via http.NewRequestWithContext. Errors returned at request-construction time (invalid method, unparsable/invalid URL per net/url.Parse, or a nil context/body misuse) are wrapped as 'error creating GET request: %v'. Unlike the later 'error GET request' case, the request never reached the network — it failed to be created.","triggerScenarios":"Called from checkPort with `http://localhost:<port>/debug/pprof/cmdline?debug=1` and from reqAndSaveMetric with a metric URL; the error fires only when `http.NewRequestWithContext` returns err — practically always a malformed URL string such as an empty URL, control characters, or an invalid port in the host part.","commonSituations":"A port parsed from lsof output is 0 or garbage and gets interpolated into the URL template; a metric URL configured/constructed with unescaped characters; an empty metric.url passed to reqAndSaveMetric.","solutions":["Log/inspect the exact url value passed in; validate it with url.Parse before calling getRequest.","Fix the port discovery path so checkPort is only called with a valid 6060-6099 port (see error 165/166).","Verify configured metric URLs in reqAndSaveMetric are absolute http(s) URLs with no stray whitespace or unescaped characters."],"exampleFix":"// before\nu := fmt.Sprintf(\"http://localhost:%d/debug/pprof/cmdline?debug=1\", port)\nresp, err := getRequest(u, 3*time.Second)\n// after\nif _, err := url.Parse(u); err != nil {\n    return fmt.Errorf(\"invalid pprof url %q: %v\", u, err)\n}\nresp, err := getRequest(u, 3*time.Second)","handlingStrategy":"validation","validationCode":"u, err := url.Parse(rawURL)\nif err != nil || u.Scheme == \"\" || u.Host == \"\" {\n    return fmt.Errorf(\"invalid URL %q: %v\", rawURL, err)\n}","typeGuard":null,"tryCatchPattern":"body, err := getRequest(url, timeout)\nif err != nil {\n    if strings.Contains(err.Error(), \"error creating GET request\") {\n        logger.Errorf(\"malformed url %q: %v\", url, err) // fix url, retrying won't help\n        return err\n    }\n}","preventionTips":["Validate URLs with url.Parse before handing them to getRequest.","Never interpolate unparsed/garbage ports into URL templates; range-check ports (6060-6099) first.","Trim whitespace and escape query parameters in configured metric URLs."],"tags":["go","http","url-validation"],"backgroundTag":"invalid-url","analyzedSha":"c9a67b23e8e08ec23ec331aa6f1675e2319e921c","analyzedAt":"2026-09-06T17:55:48.476Z","contentChangedAt":"2026-09-06T17:55:48.476Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}