Skip to content

errors

Exception classes for analytix.

APIError

Bases: AnalytixError

The YouTube Analytics API has returned an error.

Parameters:

Name Type Description Default
code Union[str, int]

The error code.

required
message str

The error message.

required
Source code in analytix/errors.py
class APIError(AnalytixError):
    """The YouTube Analytics API has returned an error.

    Parameters
    ----------
    code
        The error code.
    message
        The error message.
    """

    def __init__(self, code: Union[str, int], message: str) -> None:
        super().__init__(f"API returned {code}: {message}")

AnalytixError

Bases: Exception

The base exception class for analytix.

Source code in analytix/errors.py
class AnalytixError(Exception):
    """The base exception class for analytix."""

AuthorisationError

Bases: AnalytixError

Something's gone wrong during the authorisation process.

Source code in analytix/errors.py
class AuthorisationError(AnalytixError):
    """Something's gone wrong during the authorisation process."""

BadRequest

Bases: APIError

The YouTube Analytics API has returned a 400 status.

This only happens when analytix has failed to catch an invalid request. If you see an error like this, report it!

Parameters:

Name Type Description Default
code Union[str, int]

The error code.

required
message str

The error message.

required
Source code in analytix/errors.py
class BadRequest(APIError):
    """The YouTube Analytics API has returned a 400 status.

    This only happens when analytix has failed to catch an invalid
    request. If you see an error like this, report it!

    Parameters
    ----------
    code
        The error code.
    message
        The error message.
    """

DataFrameConversionError

Bases: AnalytixError

Your report could not be converted to a DataFrame or table.

Source code in analytix/errors.py
class DataFrameConversionError(AnalytixError):
    """Your report could not be converted to a DataFrame or table."""

Forbidden

Bases: APIError

The YouTube Analytics API has returned a 403 status.

This is raised when you attempt to access monetary data from a non-partnered channel.

Parameters:

Name Type Description Default
code Union[str, int]

The error code.

required
message str

The error message.

required
Source code in analytix/errors.py
class Forbidden(APIError):
    """The YouTube Analytics API has returned a 403 status.

    This is raised when you attempt to access monetary data from a
    non-partnered channel.

    Parameters
    ----------
    code
        The error code.
    message
        The error message.
    """

IdTokenError

Bases: AuthorisationError

Your ID token could not be token.

Source code in analytix/errors.py
class IdTokenError(AuthorisationError):
    """Your ID token could not be token."""

InvalidRequest

Bases: AnalytixError

analytix has found a problem in the request you tried to make to the YouTube Analytics API.

Source code in analytix/errors.py
class InvalidRequest(AnalytixError):
    """analytix has found a problem in the request you tried to make
    to the YouTube Analytics API."""

    @staticmethod
    def list_of(values: Set[str]) -> str:
        items = tuple(f"{v!r}" for v in sorted(values))

        if len(items) > 2:
            return f"{', '.join(items[:-1])}, and {items[-1]}"

        return " and ".join(items)

    @classmethod
    def invalid(cls, key: str, values: Set[str]) -> "InvalidRequest":
        plural = "s" if len(values) > 1 else ""
        return cls(f"invalid {key}{plural} provided: {cls.list_of(values)}")

    @classmethod
    def incompatible_dimensions(cls, values: Set[str]) -> "InvalidRequest":
        return cls(f"dimensions {cls.list_of(values)} cannot be used together")

    @classmethod
    def incompatible_filters(cls, values: Set[str]) -> "InvalidRequest":
        return cls(f"filters {cls.list_of(values)} cannot be used together")

    @classmethod
    def invalid_filter_value(cls, key: str, value: str) -> "InvalidRequest":
        return cls(f"invalid value {value!r} for filter {key!r}")

    @classmethod
    def incompatible_filter_value(cls, key: str, value: str) -> "InvalidRequest":
        return cls(
            f"value {value!r} for filter {key!r} cannot be used with the given "
            "dimensions",
        )

    @classmethod
    def incompatible_metrics(cls, values: Set[str]) -> "InvalidRequest":
        plural = "s" if len(values) > 1 else ""
        return cls(
            f"metric{plural} {cls.list_of(values)} cannot be used with the given "
            "dimensions and filters",
        )

    @classmethod
    def incompatible_sort_options(cls, values: Set[str]) -> "InvalidRequest":
        plural = "s" if len(values) > 1 else ""
        return cls(
            f"sort option{plural} {cls.list_of(values)} cannot be used with the given "
            "dimensions and filters",
        )

    @classmethod
    def non_matching_sort_options(cls, values: Set[str]) -> "InvalidRequest":
        plural = "s" if len(values) > 1 else ""
        isare = "are" if plural else "is"
        return cls(
            f"sort option{plural} {cls.list_of(values)} {isare} not part of the given "
            "metrics",
        )

    @classmethod
    def invalid_set(
        cls,
        key: str,
        values: Set[str],
        expd: str,
        recv: int,
    ) -> "InvalidRequest":
        plural = "" if expd in {"1", "at least 1"} else "s"
        return cls(
            f"expected {expd} {key}{plural} from {cls.list_of(values)}, got {recv}",
        )

MissingOptionalComponents

Bases: AnalytixError

Components not installed by analytix by default are required for a specific operation, but are not installed.

Parameters:

Name Type Description Default
*args str

The libraries that need to be installed.

()
Source code in analytix/errors.py
class MissingOptionalComponents(AnalytixError):
    """Components not installed by analytix by default are required for
    a specific operation, but are not installed.

    Parameters
    ----------
    *args
        The libraries that need to be installed.
    """

    def __init__(self, *args: str) -> None:
        vals = " ".join(args)
        super().__init__(
            f"some necessary libraries are not installed (hint: pip install {vals})",
        )

NotAuthorised

Bases: AuthorisationError

The client does not have sufficient authorisation to complete the requested operation.

Source code in analytix/errors.py
class NotAuthorised(AuthorisationError):
    """The client does not have sufficient authorisation to complete the
    requested operation."""

NotFound

Bases: APIError

The YouTube Analytics API has returned a 404 status.

Parameters:

Name Type Description Default
code Union[str, int]

The error code.

required
message str

The error message.

required
Source code in analytix/errors.py
class NotFound(APIError):
    """The YouTube Analytics API has returned a 404 status.

    Parameters
    ----------
    code
        The error code.
    message
        The error message.
    """

RefreshTokenExpired

Bases: AuthorisationError

Your refresh token has expired.

Source code in analytix/errors.py
class RefreshTokenExpired(AuthorisationError):
    """Your refresh token has expired."""

Unauthorised

Bases: APIError

The YouTube Analytics API has returned a 401 status.

Parameters:

Name Type Description Default
code Union[str, int]

The error code.

required
message str

The error message.

required
Source code in analytix/errors.py
class Unauthorised(APIError):
    """The YouTube Analytics API has returned a 401 status.

    Parameters
    ----------
    code
        The error code.
    message
        The error message.
    """