Skip to content

exceptions

Exceptions.

Exceptions for the bitpin package.

Description.

This module contains all the exceptions that are raised by the bitpin package.

APIException

Bases: Exception

API Exception.

Attributes:

Name Type Description
message str

Message.

result Any

Result.

status_code int

Status code.

response Union[Response, ClientResponse]

Response.

request Union[PreparedRequest, ClientRequest]

Request.

url str

URL.

Source code in src/bitpin/exceptions.py
class APIException(Exception):
    """
    API Exception.

    Attributes:
        message (str): Message.
        result (t.Any): Result.
        status_code (int): Status code.
        response (t.Union[requests.Response, aiohttp.ClientResponse]): Response.
        request (t.Union[requests.PreparedRequest, aiohttp.ClientRequest]): Request.
        url (str): URL.
    """

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

        Args:
            response (t.Union[requests.Response, aiohttp.ClientResponse]): Response.
            status_code (int): Status code.
            text (str): Text.
        """

        try:
            json_res = json.loads(text)
        except ValueError:
            self.message = f"Invalid JSON error message from Bitpin: {response.text}"
            self.result = None
        else:
            self.message = json_res.get("detail", "Unknown error")
            self.result = json_res.get("result")

        self.status_code = status_code
        self.response = response
        self.request = getattr(response, "request", None)
        self.url = getattr(response, "url", None)

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

        Returns:
            str: String representation.
        """

        return f"APIError(code={self.status_code}): {self.message} | {self.result} | {self.url}"

__init__(response, status_code, text)

Constructor.

Parameters:

Name Type Description Default
response Union[Response, ClientResponse]

Response.

required
status_code int

Status code.

required
text str

Text.

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

    Args:
        response (t.Union[requests.Response, aiohttp.ClientResponse]): Response.
        status_code (int): Status code.
        text (str): Text.
    """

    try:
        json_res = json.loads(text)
    except ValueError:
        self.message = f"Invalid JSON error message from Bitpin: {response.text}"
        self.result = None
    else:
        self.message = json_res.get("detail", "Unknown error")
        self.result = json_res.get("result")

    self.status_code = status_code
    self.response = response
    self.request = getattr(response, "request", None)
    self.url = getattr(response, "url", None)

__str__()

String representation.

Returns:

Name Type Description
str str

String representation.

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

    Returns:
        str: String representation.
    """

    return f"APIError(code={self.status_code}): {self.message} | {self.result} | {self.url}"

RequestException

Bases: Exception

Request Exception.

Attributes:

Name Type Description
message str

Message.

Source code in src/bitpin/exceptions.py
class RequestException(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"RequestException: {self.message}"

__init__(message)

Constructor.

Parameters:

Name Type Description Default
message str

Message.

required
Source code in src/bitpin/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/bitpin/exceptions.py
def __str__(self) -> str:
    """
    String representation.

    Returns:
        str: String representation.
    """

    return f"RequestException: {self.message}"