Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 22 additions & 4 deletions src/thecompaniesapi/sdk.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,7 @@ def _make_request(
return {'data': response.text, 'status': response.status_code}

except requests.exceptions.RequestException as e:
# Handle request errors
raise ApiError(f"Request failed: {str(e)}") from e
raise ApiError.from_request_exception(e, f"Request failed: {str(e)}") from e

def get(
self,
Expand Down Expand Up @@ -186,8 +185,27 @@ def delete(


class ApiError(Exception):
"""Custom exception for API errors."""
pass
"""
Custom exception for API errors.
"""
def __init__(self, message: str, status_code: int | None = None) -> None:
super().__init__(message)
self.status_code = status_code

@classmethod
def from_request_exception(cls, e: ApiError, message: str) -> "ApiError":
status_code = None
cause = e.__cause__
if isinstance(cause, requests.HTTPError) and cause.response is not None:
status_code = cause.response.status_code
elif isinstance(cause, requests.exceptions.RetryError):
# Retry exhausted - try to extract status from message
# e.g., "too many 429 error responses"
import re

if match := re.search(r"(\d{3})", str(cause)):
status_code = int(match.group(1))
return cls(message, status_code)


class Client:
Expand Down
35 changes: 33 additions & 2 deletions tests/test_client.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import json
import pytest
import responses
import requests

from unittest.mock import Mock, patch

from src.thecompaniesapi import Client, HttpClient, ApiError
Expand Down Expand Up @@ -154,8 +156,26 @@ def test_request_error_handling(self):

client = HttpClient(api_token="test-token")

with pytest.raises(ApiError, match="Request failed"):
with pytest.raises(ApiError, match="Request failed") as exc_info:
client.get("/v2/error")
assert exc_info.value.status_code == 404

@responses.activate
def test_request_error_timeout_handling(self):
"""Test error handling for HTTP errors."""
responses.add(
responses.GET,
"https://api.thecompaniesapi.com/v2/error",
body=requests.exceptions.Timeout() # Simulate a request failure
)

client = HttpClient(api_token="test-token")

try:
client.get("/v2/error")
except Exception as e:
assert e.status_code is None


@responses.activate
def test_non_json_response(self):
Expand Down Expand Up @@ -255,4 +275,15 @@ def test_api_error_creation(self):
assert str(error) == "Test error message"

with pytest.raises(ApiError, match="Test error message"):
raise error
raise error


def test_api_error_status_code(self):
"""Test ApiError with status code attribute."""
error = ApiError("Error with status", status_code=404)
assert str(error) == "Error with status"
assert error.status_code == 404

with pytest.raises(ApiError) as exc_info:
raise error
assert exc_info.value.status_code == 404