Skip to content
Merged
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
17 changes: 8 additions & 9 deletions instaparser/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,22 @@
Exception classes for the Instaparser Library.
"""

from typing import Any


class InstaparserError(Exception):
"""Base exception for all Instaparser errors."""

pass


class InstaparserAPIError(InstaparserError):
"""Exception raised for API errors."""

def __init__(self, message, status_code=None, response=None):
def __init__(
self,
message: str,
status_code: int | None = None,
response: Any | None = None,
):
super().__init__(message)
self.status_code = status_code
self.response = response
Expand All @@ -21,16 +26,10 @@ def __init__(self, message, status_code=None, response=None):
class InstaparserAuthenticationError(InstaparserAPIError):
"""Exception raised for authentication errors (401)."""

pass


class InstaparserRateLimitError(InstaparserAPIError):
"""Exception raised for rate limit errors (429)."""

pass


class InstaparserValidationError(InstaparserAPIError):
"""Exception raised for validation errors (400)."""

pass
87 changes: 8 additions & 79 deletions tests/test_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,94 +2,23 @@
Tests for exception classes.
"""

import pytest

from instaparser.exceptions import (
InstaparserAPIError,
InstaparserAuthenticationError,
InstaparserError,
InstaparserRateLimitError,
InstaparserValidationError,
)


class TestInstaparserError:
"""Tests for base InstaparserError exception."""

def test_instaparser_error_is_exception(self):
"""Test that InstaparserError is an Exception."""
assert issubclass(InstaparserError, Exception)

def test_instaparser_error_can_be_raised(self):
"""Test that InstaparserError can be raised and caught."""
with pytest.raises(InstaparserError):
raise InstaparserError("Test error")
from instaparser.exceptions import InstaparserAPIError


class TestInstaparserAPIError:
"""Tests for InstaparserAPIError exception."""
"""Tests for InstaparserAPIError attributes."""

def test_instaparser_api_error_inherits_from_base(self):
"""Test that InstaparserAPIError inherits from InstaparserError."""
assert issubclass(InstaparserAPIError, InstaparserError)

def test_instaparser_api_error_with_message(self):
"""Test InstaparserAPIError with just a message."""
def test_message(self):
error = InstaparserAPIError("API error occurred")
assert str(error) == "API error occurred"
assert error.status_code is None
assert error.response is None

def test_instaparser_api_error_with_status_code(self):
"""Test InstaparserAPIError with status code."""
def test_status_code(self):
error = InstaparserAPIError("API error", status_code=500)
assert str(error) == "API error"
assert error.status_code == 500
assert error.response is None

def test_instaparser_api_error_with_response(self):
"""Test InstaparserAPIError with response object."""
mock_response = type("MockResponse", (), {"status_code": 500})()
error = InstaparserAPIError("API error", status_code=500, response=mock_response)
assert str(error) == "API error"
assert error.status_code == 500
assert error.response == mock_response


class TestInstaparserAuthenticationError:
"""Tests for InstaparserAuthenticationError exception."""

def test_authentication_error_inherits_from_api_error(self):
"""Test that InstaparserAuthenticationError inherits from InstaparserAPIError."""
assert issubclass(InstaparserAuthenticationError, InstaparserAPIError)

def test_authentication_error_can_be_raised(self):
"""Test that InstaparserAuthenticationError can be raised."""
with pytest.raises(InstaparserAuthenticationError):
raise InstaparserAuthenticationError("Invalid API key", status_code=401)


class TestInstaparserRateLimitError:
"""Tests for InstaparserRateLimitError exception."""

def test_rate_limit_error_inherits_from_api_error(self):
"""Test that InstaparserRateLimitError inherits from InstaparserAPIError."""
assert issubclass(InstaparserRateLimitError, InstaparserAPIError)

def test_rate_limit_error_can_be_raised(self):
"""Test that InstaparserRateLimitError can be raised."""
with pytest.raises(InstaparserRateLimitError):
raise InstaparserRateLimitError("Rate limit exceeded", status_code=429)


class TestInstaparserValidationError:
"""Tests for InstaparserValidationError exception."""

def test_validation_error_inherits_from_api_error(self):
"""Test that InstaparserValidationError inherits from InstaparserAPIError."""
assert issubclass(InstaparserValidationError, InstaparserAPIError)

def test_validation_error_can_be_raised(self):
"""Test that InstaparserValidationError can be raised."""
with pytest.raises(InstaparserValidationError):
raise InstaparserValidationError("Invalid request", status_code=400)
def test_response(self):
response = object()
error = InstaparserAPIError("API error", status_code=500, response=response)
assert error.response is response