jd-opensource/joyagent-jdgenie · error · ValueError

服务器URL必须以http://或https://开头

Error message

服务器URL必须以http://或https://开头

What it means

_validate_server_url performs a simple format check: the URL must start with http:// or https://, otherwise ValueError is raised. This prevents later failures when the client builds request URLs against a malformed base.

Solutions

  1. Prefix the configured host with https:// (or http:// for local dev) before constructing the client
  2. Normalize the URL at config-load time so it always includes the scheme
  3. Document the expected URL format in the config template
  4. Accept host-only values by auto-prepending the scheme in a wrapper

Example fix

# before
client = GenieClient(server_url="localhost:8080")  # ValueError
# after
base = config["server_url"]
if not base.startswith(("http://", "https://")):
    base = "http://" + base
client = GenieClient(server_url=base)
Defensive patterns

Strategy: validation

Validate before calling

def normalize_server_url(url: str) -> str:
    if not url.startswith(("http://", "https://")):
        url = "https://" + url
    return url.rstrip("/")

client = GenieClient(server_url=normalize_server_url(raw_url))

Type guard

def has_http_scheme(url: str) -> bool:
    return url.startswith(("http://", "https://"))

Try / catch

try:
    client = GenieClient(server_url=server_url)
except ValueError as e:
    logging.error("invalid server URL: %s", e)
    raise SystemExit(1)

Prevention

When it happens

Trigger: Passing a URL without scheme ("myhost:8000", "localhost:8080") or with the wrong scheme ("ftp://", "ws://") to GenieClient.__init__.

Common situations: Users copying host-only addresses from docs into config; stripping the scheme for display and forgetting to restore it; ws:// URLs from websocket configs pasted into the HTTP client config.

Understand the failure class

Background: "Invalid URL" errors: why new URL(), URI.parse, and reqwest::Url reject your string — missing scheme, whitespace, and bad path format — this error's family across 39 libraries.

Related errors


AI-assisted analysis of jd-opensource/joyagent-jdgenie@2417e0b8b6 (2026-09-08). Data as JSON: /api/errors/a9d6f8c89a9dc58d. Report an issue: GitHub.

Appendix: source

Thrown at genie-client/app/client.py:66

    def _validate_server_url(server_url: str) -> str:
        """
        验证服务器URL的有效性

        Args:
            server_url: 待验证的服务器URL

        Returns:
            验证后的服务器URL

        Raises:
            ValueError: 当URL无效时抛出
        """
        if not server_url or not isinstance(server_url, str):
            raise ValueError("服务器URL不能为空且必须是字符串类型")

        # 简单的URL格式验证
        if not (server_url.startswith('http://') or server_url.startswith('https://')):
            raise ValueError("服务器URL必须以http://或https://开头")

        return server_url.rstrip('/')  # 移除末尾的斜杠

    def _configure_from_entity(self, entity: HeaderEntity) -> None:
        """
        根据 HeaderEntity 配置客户端参数

        Args:
            entity: 包含配置信息的实体对象
        """
        try:
            if entity.timeout is not None:
                self.timeout = max(1, int(entity.timeout))  # 确保超时时间至少为1秒
                logger.debug(f"设置连接超时时间: {self.timeout}s")

            if entity.sse_read_timeout is not None:
                self.sse_read_timeout = max(30, int(entity.sse_read_timeout))  # 最少30秒
                logger.debug(f"设置SSE读取超时时间: {self.sse_read_timeout}s")

View on GitHub (pinned to 2417e0b8b6)