Exceptions

The library provides a hierarchy of exceptions for handling API errors gracefully.

Exception Hierarchy

BetterStackError (base)
├── APIError
│   ├── AuthenticationError (401)
│   ├── ForbiddenError (403)
│   ├── NotFoundError (404)
│   ├── RateLimitError (429)
│   └── ServerError (5xx)
├── ValidationError
└── ConfigurationError

Usage Example

from betterstack.uptime import UptimeAPI
from betterstack.uptime.objects import Monitor
from betterstack.uptime.exceptions import (
    AuthenticationError,
    NotFoundError,
    RateLimitError,
    APIError,
)

api = UptimeAPI("your-token")

try:
    monitor = Monitor.get_all_instances(api)
except AuthenticationError:
    print("Invalid API token")
except NotFoundError:
    print("Resource not found")
except RateLimitError as e:
    print(f"Rate limited. Retry after {e.retry_after} seconds")
except APIError as e:
    print(f"API error {e.status_code}: {e.message}")

Exception Reference

BetterStackError

class betterstack.uptime.exceptions.BetterStackError[source]

Bases: Exception

Base exception for all BetterStack errors.

APIError

class betterstack.uptime.exceptions.APIError(message: str, status_code: int, response_body: dict | None = None)[source]

Bases: BetterStackError

Error returned from the BetterStack API.

__init__(message: str, status_code: int, response_body: dict | None = None) None[source]

AuthenticationError

class betterstack.uptime.exceptions.AuthenticationError(message: str = 'Invalid or missing authentication token')[source]

Bases: APIError

401 Unauthorized - Invalid or missing token.

__init__(message: str = 'Invalid or missing authentication token') None[source]

ForbiddenError

class betterstack.uptime.exceptions.ForbiddenError(message: str = 'Access denied')[source]

Bases: APIError

403 Forbidden - Access denied.

__init__(message: str = 'Access denied') None[source]

NotFoundError

class betterstack.uptime.exceptions.NotFoundError(message: str = 'Resource not found')[source]

Bases: APIError

404 Not Found - Resource doesn’t exist.

__init__(message: str = 'Resource not found') None[source]

RateLimitError

class betterstack.uptime.exceptions.RateLimitError(message: str = 'Rate limit exceeded', retry_after: int | None = None)[source]

Bases: APIError

429 Too Many Requests - Rate limit exceeded.

__init__(message: str = 'Rate limit exceeded', retry_after: int | None = None) None[source]

ServerError

class betterstack.uptime.exceptions.ServerError(message: str = 'Server error', status_code: int = 500)[source]

Bases: APIError

5xx Server Error - Server-side issue.

__init__(message: str = 'Server error', status_code: int = 500) None[source]

ValidationError

class betterstack.uptime.exceptions.ValidationError[source]

Bases: BetterStackError

Local validation error (e.g., invalid parameters).

ConfigurationError

class betterstack.uptime.exceptions.ConfigurationError[source]

Bases: BetterStackError

Configuration error (e.g., missing required settings).