Skip to content

Exceptions API Reference

This page documents all exception classes used in the library.

Base Exception

BMRSException

Bases: Exception

Base exception for all BMRS client errors.

Source code in elexon_bmrs/exceptions.py
4
5
6
7
class BMRSException(Exception):
    """Base exception for all BMRS client errors."""

    pass

Specific Exceptions

APIError

Bases: BMRSException

Raised when the API returns an error response.

Source code in elexon_bmrs/exceptions.py
class APIError(BMRSException):
    """Raised when the API returns an error response."""

    def __init__(self, message: str, status_code: int = None, response: dict = None):
        self.status_code = status_code
        self.response = response
        super().__init__(message)

AuthenticationError

Bases: BMRSException

Raised when API authentication fails.

Source code in elexon_bmrs/exceptions.py
class AuthenticationError(BMRSException):
    """Raised when API authentication fails."""

    pass

RateLimitError

Bases: BMRSException

Raised when API rate limit is exceeded.

Source code in elexon_bmrs/exceptions.py
class RateLimitError(BMRSException):
    """Raised when API rate limit is exceeded."""

    def __init__(self, message: str, retry_after: int = None):
        self.retry_after = retry_after
        super().__init__(message)

ValidationError

Bases: BMRSException

Raised when input validation fails.

Source code in elexon_bmrs/exceptions.py
class ValidationError(BMRSException):
    """Raised when input validation fails."""

    pass

Usage Examples

Handling API Errors

from elexon_bmrs import BMRSClient
from elexon_bmrs.exceptions import APIError

try:
    client = BMRSClient(api_key="your-key")
    data = client.get_system_demand(
        from_date="2024-01-01",
        to_date="2024-01-02"
    )
except APIError as e:
    print(f"API Error: {e}")
    print(f"Status Code: {e.status_code}")
    print(f"Response: {e.response}")

Handling Authentication Errors

from elexon_bmrs import BMRSClient
from elexon_bmrs.exceptions import AuthenticationError

try:
    client = BMRSClient(api_key="invalid-key")
    data = client.get_system_demand(
        from_date="2024-01-01",
        to_date="2024-01-02"
    )
except AuthenticationError:
    print("Invalid API key. Please check your credentials.")

Handling Rate Limits

import time
from elexon_bmrs import BMRSClient
from elexon_bmrs.exceptions import RateLimitError

try:
    client = BMRSClient(api_key="your-key")
    data = client.get_system_demand(
        from_date="2024-01-01",
        to_date="2024-01-02"
    )
except RateLimitError as e:
    print(f"Rate limited. Retry after {e.retry_after} seconds")
    if e.retry_after:
        time.sleep(e.retry_after)

Handling Validation Errors

from elexon_bmrs import BMRSClient
from elexon_bmrs.exceptions import ValidationError

try:
    client = BMRSClient(api_key="your-key")
    # Invalid date format
    data = client.get_system_demand(
        from_date="invalid-date",
        to_date="2024-01-02"
    )
except ValidationError as e:
    print(f"Validation error: {e}")

Catching All BMRS Exceptions

from elexon_bmrs import BMRSClient
from elexon_bmrs.exceptions import BMRSException

try:
    client = BMRSClient(api_key="your-key")
    data = client.get_system_demand(
        from_date="2024-01-01",
        to_date="2024-01-02"
    )
except BMRSException as e:
    print(f"BMRS Error: {e}")
    # Handle all BMRS-related errors