henrygd/beszel · error

new request: %w

Error message

new request: %w

What it means

Thrown by downloadFile when http.NewRequestWithContext/NewRequest fails to construct the GET request, meaning the URL string could not be parsed. Because the URL comes from the tool's configuration/args, this is almost always a malformed or empty URL supplied at invocation time.

Source

Thrown at agent/tools/fetchsmartctl/main.go:54

		}
	}

	if err := downloadFile(*url, *out, *sha); err != nil {
		fatalf("download failed: %v", err)
	}
}

func downloadFile(url, dest, shaHex string) error {
	// Prepare destination
	if err := os.MkdirAll(filepath.Dir(dest), 0o755); err != nil {
		return fmt.Errorf("create dir: %w", err)
	}

	// HTTP client
	client := &http.Client{Timeout: 60 * time.Second}
	req, err := http.NewRequest(http.MethodGet, url, nil)
	if err != nil {
		return fmt.Errorf("new request: %w", err)
	}
	req.Header.Set("User-Agent", "beszel-fetchsmartctl/1.0")

	resp, err := client.Do(req)
	if err != nil {
		return fmt.Errorf("http get: %w", err)
	}
	defer resp.Body.Close()

	if resp.StatusCode < 200 || resp.StatusCode >= 300 {
		return fmt.Errorf("unexpected HTTP status: %s", resp.Status)
	}

	tmp := dest + ".tmp"
	f, err := os.OpenFile(tmp, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0o644)
	if err != nil {
		return fmt.Errorf("open tmp: %w", err)
	}

View on GitHub (pinned to b38fb7dafa)

Solutions

  1. Print and inspect the URL argument passed to the tool; fix malformed characters or missing scheme
  2. Quote the URL in shell scripts so spaces/special chars aren't split
  3. Ensure the flag providing the URL is actually set in the build script
  4. Validate with a quick curl of the same URL

Example fix

// before
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
	return fmt.Errorf("new request: %w", err)
}
// after
if u, perr := url.Parse(url); perr != nil || u.Scheme == "" || u.Host == "" {
	return fmt.Errorf("invalid download URL %q", url)
}
req, err := http.NewRequest(http.MethodGet, url, nil)
Defensive patterns

Strategy: validation

Validate before calling

u, err := url.Parse(downloadURL)
if err != nil || u.Scheme == "" || u.Host == "" {
	return fmt.Errorf("invalid URL %q", downloadURL)
}

Type guard

func isValidURL(s string) bool {
	u, err := url.Parse(s)
	return err == nil && (u.Scheme == "http" || u.Scheme == "https") && u.Host != ""
}

Try / catch

if err := downloadFile(url, dest, sha); err != nil {
	if strings.HasPrefix(err.Error(), "new request:") {
		fmt.Printf("bad URL %q: %v\n", url, err)
		os.Exit(2)
	}
	return err
}

Prevention

When it happens

Trigger: Running fetchsmartctl with an invalid or empty URL argument so url.Parse fails before any network activity.

Common situations: Missing CLI flag leaving an empty URL; URL containing spaces or unescaped characters; copy-paste errors in build scripts or Makefiles.

Related errors


AI-assisted analysis of henrygd/beszel@b38fb7dafa (2026-08-31). Data as JSON: /api/errors/21b2af44b65f9b6e. Report an issue: GitHub.