Skip to content

exceptions

Exceptions.

Exceptions for the bit24 package.

Classes:

  • APIError: API Exception.
  • RequestError: Request Exception.

APIError

Bases: Exception

API Exception.

Attributes:

Name Type Description
url str

URL.

request Http

Request.

response HttpResponse

Response.

message

(str): Message.

Source code in src/bit24/exceptions.py
class APIError(Exception):
    """
    API Exception.

    Attributes:
        url (str): URL.
        request (t.Http): Request.
        response (t.HttpResponse): Response.
        message: (str): Message.
    """

    def __init__(self, response: t.HttpResponse, status_code: int, text: str):
        """
        Constructor.

        Args:
            response (HttpResponse): Response.
            status_code (int): Status code.
            text (str): Text.
        """
        self.request = getattr(response, "request", None)
        self.url = getattr(response, "url", None)
        self.response = response

        try:
            json_res: rt.BaseResponse = json.loads(text)
        except ValueError:
            self.message = f"Invalid JSON error message from Bit24: {response.text}"
        else:
            error: rt.Error | dict[str, Any] = json_res.get("error", {}) or {}
            self.status_code = json_res.get("status_code", status_code)
            self.message = error.get("message", "Unknown error") or "Unknown error"
            self.errors = error.get("errors", []) or []

    def __str__(self) -> str:
        """
        String representation.

        Returns:
            str: String representation.
        """
        return f"APIError(code={self.status_code}): '{self.message}' | Errors: {self.errors}"

__init__(response, status_code, text)

Constructor.

Parameters:

Name Type Description Default
response HttpResponse

Response.

required
status_code int

Status code.

required
text str

Text.

required
Source code in src/bit24/exceptions.py
def __init__(self, response: t.HttpResponse, status_code: int, text: str):
    """
    Constructor.

    Args:
        response (HttpResponse): Response.
        status_code (int): Status code.
        text (str): Text.
    """
    self.request = getattr(response, "request", None)
    self.url = getattr(response, "url", None)
    self.response = response

    try:
        json_res: rt.BaseResponse = json.loads(text)
    except ValueError:
        self.message = f"Invalid JSON error message from Bit24: {response.text}"
    else:
        error: rt.Error | dict[str, Any] = json_res.get("error", {}) or {}
        self.status_code = json_res.get("status_code", status_code)
        self.message = error.get("message", "Unknown error") or "Unknown error"
        self.errors = error.get("errors", []) or []

__str__()

String representation.

Returns:

Name Type Description
str str

String representation.

Source code in src/bit24/exceptions.py
def __str__(self) -> str:
    """
    String representation.

    Returns:
        str: String representation.
    """
    return f"APIError(code={self.status_code}): '{self.message}' | Errors: {self.errors}"

RequestError

Bases: Exception

Request Exception.

Attributes:

Name Type Description
message str

Message.

Source code in src/bit24/exceptions.py
class RequestError(Exception):
    """
    Request Exception.

    Attributes:
        message (str): Message.
    """

    def __init__(self, message: str):
        """
        Constructor.

        Args:
            message (str): Message.
        """
        self.message = message

    def __str__(self) -> str:
        """
        String representation.

        Returns:
            str: String representation.
        """
        return f"RequestError: {self.message}"

__init__(message)

Constructor.

Parameters:

Name Type Description Default
message str

Message.

required
Source code in src/bit24/exceptions.py
def __init__(self, message: str):
    """
    Constructor.

    Args:
        message (str): Message.
    """
    self.message = message

__str__()

String representation.

Returns:

Name Type Description
str str

String representation.

Source code in src/bit24/exceptions.py
def __str__(self) -> str:
    """
    String representation.

    Returns:
        str: String representation.
    """
    return f"RequestError: {self.message}"