{"record":{"id":"b40ed02d6c11b570","repo":"BerriAI/litellm","slug":"http-self-status-code","errorCode":null,"errorMessage":"HTTP {self.status_code}","messagePattern":"HTTP \\{self\\.status_code\\}","errorType":"http","errorClass":"Exception","httpStatus":null,"severity":"info","filePath":"litellm/integrations/mock_client_factory.py","lineNumber":83,"sourceCode":"        return self._text\n\n    @property\n    def content(self) -> bytes:\n        \"\"\"Return response content.\"\"\"\n        return self._content\n\n    def json(self) -> dict:\n        \"\"\"Return JSON response data.\"\"\"\n        return self._json_data\n\n    def read(self) -> bytes:\n        \"\"\"Read response content.\"\"\"\n        return self._content\n\n    def raise_for_status(self):\n        \"\"\"Raise exception for error status codes.\"\"\"\n        if self.status_code >= 400:\n            raise Exception(f\"HTTP {self.status_code}\")\n\n\ndef _is_url_match(url, matchers: list[str]) -> bool:\n    \"\"\"Check if URL matches any of the provided matchers.\"\"\"\n    try:\n        parsed_url: Final = httpx.URL(url) if isinstance(url, str) else url\n        url_str: Final = str(parsed_url).lower()\n        hostname: Final = parsed_url.host or \"\"\n\n        for matcher in matchers:\n            if matcher.lower() in url_str or matcher.lower() in hostname.lower():\n                return True\n\n        # Also check for localhost with matcher in path\n        if hostname in (\"localhost\", \"127.0.0.1\"):\n            for matcher in matchers:\n                if matcher.lower() in url_str:\n                    return True","sourceCodeStart":65,"sourceCodeEnd":101,"githubUrl":"https://github.com/BerriAI/litellm/blob/6c2dcb801bf2b75c18f1bb24140e7cf57465cc4d/litellm/integrations/mock_client_factory.py#L65-L101","documentation":"This is LiteLLM's mock HTTP response object (mock_client_factory) mimicking httpx/requests raise_for_status: it raises a plain Exception with 'HTTP {status}' whenever the mocked status code is >= 400. It exists only in tests and mocked transports, so hitting it means your mocked endpoint was configured to return an error status and the code under test called raise_for_status (directly or via litellm's HTTP client wrapper).","triggerScenarios":"Registering a mock handler that returns status 400/401/429/500 and letting litellm's client raise_for_status run; testing retry/backoff paths by simulating API errors; a mock matcher accidentally matching the real request URL with an error fixture.","commonSituations":"Writing unit tests for litellm HTTP error handling; fixture reuse where an error-response mock matches more URLs than intended (broad matcher strings); asserting behavior after mocked failures.","solutions":["Update the mock to return a success status (2xx) when you intend the happy path","If testing error paths, catch this Exception (or httpx.HTTPStatusError in real mode) around the call under test","Tighten mock URL matchers so error fixtures only match the intended endpoint","Return realistic error bodies too, since code under test may read response.text/status_code after catching"],"exampleFix":"# before\nmock_response = MockResponse(status_code=500, json_data={\"error\": \"boom\"})\nresult = client.fetch(url)  # raises Exception: HTTP 500 via raise_for_status\n\n# after (happy path)\nmock_response = MockResponse(status_code=200, json_data={\"id\": \"ok\"})\n# or (error path under test)\ntry:\n    client.fetch(url)\nexcept Exception as e:\n    assert \"HTTP 500\" in str(e)","handlingStrategy":"try-catch","validationCode":"def mock_will_raise(status_code: int) -> bool:\n    return status_code >= 400","typeGuard":null,"tryCatchPattern":"try:\n    resp.raise_for_status()\nexcept Exception as e:  # mock raises plain Exception, not httpx.HTTPStatusError\n    assert str(e).startswith(\"HTTP \")\n    # handle simulated failure path here","preventionTips":["Keep error-status mocks on tightly matched URLs","In tests, assert on status_code/raise_for_status rather than swallowing all exceptions"],"tags":["python","mock","testing","http","httpx"],"backgroundTag":null,"analyzedSha":"6c2dcb801bf2b75c18f1bb24140e7cf57465cc4d","analyzedAt":"2026-08-15T07:12:03.035Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}