alibaba/open-code-review · error

invalid Base URL %q: %w

Error message

invalid Base URL %q: %w

What it means

validateBaseURL parses a provider Base URL with url.Parse before saving; if parsing itself fails, the error is wrapped as "invalid Base URL %q". This gives immediate feedback that the URL string is malformed rather than failing later at LLM-client request time.

Source

Thrown at cmd/opencodereview/provider_cmd.go:453

}

func maskKey(key string) string {
	if key == "" {
		return "(not set)"
	}
	if len(key) <= 8 {
		return "***"
	}
	return key[:4] + "***" + key[len(key)-4:]
}

// validateBaseURL checks that a provider Base URL has an http or https scheme
// and a non-empty host, giving the user immediate feedback rather than
// a runtime failure when the LLM client tries to use it.
func validateBaseURL(raw string) error {
	parsed, err := url.Parse(raw)
	if err != nil {
		return fmt.Errorf("invalid Base URL %q: %w", raw, err)
	}
	if parsed.Scheme != "http" && parsed.Scheme != "https" {
		return fmt.Errorf("Base URL must use http or https scheme, got %q", parsed.Scheme)
	}
	if parsed.Host == "" {
		return fmt.Errorf("Base URL %q must include a host", raw)
	}
	return nil
}

View on GitHub (pinned to 5cf97d0d15)

Solutions

  1. Re-enter the Base URL with valid URL syntax and no stray control characters
  2. Percent-encode special characters properly (e.g. spaces as %20)
  3. Check the wrapped inner error (url.Parse's message) for the exact syntax problem
  4. Use a plain scheme://host[:port][/path] form, e.g. https://api.example.com/v1

Example fix

// before
$ ocr config provider set base-url "https://api.example.com/v1%zz"
// error: invalid Base URL ...: invalid URL escape "%zz"
// after
$ ocr config provider set base-url "https://api.example.com/v1"
Defensive patterns

Strategy: validation

Validate before calling

u, err := url.Parse(strings.TrimSpace(raw))
if err != nil { return fmt.Errorf("invalid Base URL %q: %w", raw, err) }
if u.Scheme != "http" && u.Scheme != "https" { return fmt.Errorf("scheme must be http/https") }
if u.Host == "" { return fmt.Errorf("host required") }

Type guard

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

Try / catch

if err := applyProviderField(...); err != nil {
    if strings.Contains(err.Error(), "invalid Base URL") {
        fmt.Fprintf(os.Stderr, "check URL syntax; underlying: %v\n", errors.Unwrap(err))
    }
    return err
}

Prevention

When it happens

Trigger: ocr config provider set/update (applyProviderField) called with a Base URL that Go's url.Parse rejects — e.g. control characters, invalid percent-encoding, or other unparseable syntax.

Common situations: Pasting a URL with stray whitespace/control characters or unescaped special characters from documentation; typos introducing invalid escape sequences like %zz.

Related errors


AI-assisted analysis of alibaba/open-code-review@5cf97d0d15 (2026-09-02). Data as JSON: /api/errors/f743b15cae018a7b. Report an issue: GitHub.