Exceptions

CPAU API Exception Classes

This module defines custom exceptions for the CPAU API library.

exception cpau.exceptions.CpauError[source]

Bases: Exception

Base exception for all CPAU API errors.

exception cpau.exceptions.CpauConnectionError[source]

Bases: CpauError

Raised when unable to connect to CPAU portal.

exception cpau.exceptions.CpauAuthenticationError[source]

Bases: CpauError

Raised when authentication fails.

exception cpau.exceptions.CpauApiError[source]

Bases: CpauError

Raised when API request fails.

exception cpau.exceptions.CpauMeterNotFoundError[source]

Bases: CpauError

Raised when specified meter is not found.

Exception Hierarchy

All CPAU-specific exceptions inherit from CpauError:

CpauError
├── CpauAuthenticationError
├── CpauConnectionError
├── CpauApiError
└── CpauMeterNotFoundError

Exception Classes

CpauError

exception cpau.exceptions.CpauError[source]

Bases: Exception

Base exception for all CPAU API errors.

Base exception for all CPAU-related errors.

CpauAuthenticationError

exception cpau.exceptions.CpauAuthenticationError[source]

Bases: CpauError

Raised when authentication fails.

Raised when authentication with CPAU portal fails.

Common causes:

  • Invalid credentials

  • Account locked due to too many failed login attempts

  • CPAU portal is down or unavailable

CpauConnectionError

exception cpau.exceptions.CpauConnectionError[source]

Bases: CpauError

Raised when unable to connect to CPAU portal.

Raised when unable to connect to CPAU portal.

Common causes:

  • Network connectivity issues

  • CPAU portal is down

  • Firewall blocking connection

CpauApiError

exception cpau.exceptions.CpauApiError[source]

Bases: CpauError

Raised when API request fails.

Raised when API request fails or returns unexpected data.

Common causes:

  • Invalid date range

  • Invalid interval type

  • CPAU API changed (breaking change)

  • Data not available for requested period

CpauMeterNotFoundError

exception cpau.exceptions.CpauMeterNotFoundError[source]

Bases: CpauError

Raised when specified meter is not found.

Raised when no meter is found for the account.

Common causes:

  • New account with no meter assigned yet

  • Account has access to multiple meters (not yet supported)

Example Usage

from cpau import CpauApiSession
from cpau.exceptions import (
    CpauAuthenticationError,
    CpauConnectionError,
    CpauApiError
)
from datetime import date

try:
    with CpauApiSession(userid='user@example.com', password='wrong') as session:
        meter = session.get_electric_meter()
        data = meter.get_usage('daily', date(2024, 12, 1))

except CpauAuthenticationError as e:
    print(f"Login failed: {e}")
    print("Check your credentials in secrets.json")

except CpauConnectionError as e:
    print(f"Connection failed: {e}")
    print("Check your network connection")

except CpauApiError as e:
    print(f"API error: {e}")
    print("The CPAU portal may have changed or be unavailable")