{"record":{"id":"17a0bc0621075cfa","repo":"HKUDS/Vibe-Trading","slug":"trading-212-request-failed-exc","errorCode":null,"errorMessage":"Trading 212 request failed: {exc}","messagePattern":"Trading 212 request failed: (.+?)","errorType":"http","errorClass":"Trading212APIError","httpStatus":null,"severity":"error","filePath":"agent/src/trading/connectors/trading212/sdk.py","lineNumber":427,"sourceCode":"\n    url = urljoin(f\"{config.base_url.rstrip('/')}/\", path.lstrip(\"/\"))\n    headers = {\"Accept\": \"application/json\"}\n    auth = None\n    if config.api_secret:\n        auth = (config.api_key, config.api_secret)\n    else:\n        headers[\"Authorization\"] = config.api_key\n    try:\n        response = requests.request(\n            method.upper(),\n            url,\n            headers=headers,\n            auth=auth,\n            params=dict(params or {}),\n            timeout=config.timeout,\n        )\n    except requests.RequestException as exc:\n        raise Trading212APIError(f\"Trading 212 request failed: {exc}\") from exc\n\n    if response.status_code in (401, 403):\n        raise Trading212APIError(\"Trading 212 API authentication failed: check api_key/api_secret.\")\n    if response.status_code >= 400:\n        raise Trading212APIError(f\"Trading 212 API returned HTTP {response.status_code}: {_error_message(response)}\")\n    if not response.content:\n        return None\n    try:\n        return response.json()\n    except ValueError as exc:\n        raise Trading212APIError(\"Trading 212 API returned invalid JSON.\") from exc\n\n\ndef _error_message(response: requests.Response) -> str:\n    try:\n        payload = response.json()\n    except ValueError:\n        return response.text.strip() or response.reason or \"request failed\"","sourceCodeStart":409,"sourceCodeEnd":445,"githubUrl":"https://github.com/HKUDS/Vibe-Trading/blob/80ffdda44c5c4db0dd84d70e051cca591cea67df/agent/src/trading/connectors/trading212/sdk.py#L409-L445","documentation":"Raised by _request when the underlying requests HTTP call raises RequestException — DNS failure, connection refused, timeout (config.timeout), TLS errors, etc. The original exception is chained for diagnosis.","triggerScenarios":"Any _get call when config.base_url is unreachable or wrong (DNS failure, wrong host, https vs http mismatch), the network is down, a proxy blocks the request, or the server takes longer than config.timeout to respond.","commonSituations":"Typo in base_url in the saved config; corporate proxy/VPN blocking api.trading212.com; sandbox vs production URL mix-up; timeout too small for slow account endpoints; intermittent connectivity in CI.","solutions":["Check the chained exception message for the concrete cause (DNS, refused, timeout)","Verify config.base_url is correct and reachable (curl it from the same host)","Increase config.timeout if the cause is a timeout","Fix proxy/firewall/VPN rules if connectivity is blocked; add retry with backoff for transient failures"],"exampleFix":"# before\nconfig = load_config()  # base_url = \"https://api.trading212.example\"\n# after: set the correct base_url and a larger timeout\nconfig = replace(config, base_url=\"https://api.trading212.com\", timeout=30)\nsave_config(config)","handlingStrategy":"retry","validationCode":"import socket\nfrom urllib.parse import urlsplit\n\ndef base_url_reachable(base_url: str, timeout: float = 5.0) -> bool:\n    parts = urlsplit(base_url)\n    try:\n        socket.create_connection((parts.hostname, parts.port or 443), timeout=timeout).close()\n    except OSError:\n        return False\n    return True","typeGuard":null,"tryCatchPattern":"try:\n    data = get_account_snapshot(config)\nexcept Trading212APIError as exc:\n    if not str(exc).startswith(\"Trading 212 request failed\"):\n        raise  # not a transport error\n    logger.warning(\"transport error, retrying: %s\", exc)\n    data = get_account_snapshot(config)  # or backoff loop","preventionTips":["Set a realistic config.timeout for slow endpoints","Wrap polling loops with exponential backoff on transport errors","Validate base_url format and reachability during connector setup"],"tags":["trading212","network","timeout","http"],"backgroundTag":"connection-error","analyzedSha":"80ffdda44c5c4db0dd84d70e051cca591cea67df","analyzedAt":"2026-08-28T12:46:38.989Z","schemaVersion":2},"datasetVersion":"2026-08-28T16:17:29.566Z"}