Skip to content

tux.utils.exceptions

Classes:

Name Description
PermissionLevelError

Raised when a user doesn't have the required permission level.

AppCommandPermissionLevelError

Raised when a user doesn't have the required permission level for an app command.

APIConnectionError

Raised when there's an issue connecting to an external API.

APIRequestError

Raised when an API request fails with a specific status code.

APIResourceNotFoundError

Raised when an API request results in a 404 or similar resource not found error.

APIPermissionError

Raised when an API request fails due to permissions (e.g., 403 Forbidden).

CodeExecutionError

Base exception for code execution errors.

MissingCodeError

Raised when no code is provided for execution.

InvalidCodeFormatError

Raised when code format is invalid.

UnsupportedLanguageError

Raised when the specified language is not supported.

CompilationError

Raised when code compilation fails.

Functions:

Name Description
handle_gather_result

Handle a result from asyncio.gather with return_exceptions=True.

handle_case_result

Handle a case result from asyncio.gather with return_exceptions=True.

Classes

PermissionLevelError(permission: str)

Bases: Exception

Raised when a user doesn't have the required permission level.

Source code in tux/utils/exceptions.py
Python
def __init__(self, permission: str) -> None:
    self.permission = permission
    super().__init__(f"Missing required permission: {permission}")

AppCommandPermissionLevelError(permission: str)

Bases: Exception

Raised when a user doesn't have the required permission level for an app command.

Source code in tux/utils/exceptions.py
Python
def __init__(self, permission: str) -> None:
    self.permission = permission
    super().__init__(f"Missing required permission: {permission}")

APIConnectionError(service_name: str, original_error: Exception)

Bases: Exception

Raised when there's an issue connecting to an external API.

Source code in tux/utils/exceptions.py
Python
def __init__(self, service_name: str, original_error: Exception):
    self.service_name = service_name
    self.original_error = original_error
    super().__init__(f"Connection error with {service_name}: {original_error}")

APIRequestError(service_name: str, status_code: int, reason: str)

Bases: Exception

Raised when an API request fails with a specific status code.

Source code in tux/utils/exceptions.py
Python
def __init__(self, service_name: str, status_code: int, reason: str):
    self.service_name = service_name
    self.status_code = status_code
    self.reason = reason
    super().__init__(f"API request to {service_name} failed with status {status_code}: {reason}")

APIResourceNotFoundError(service_name: str, resource_identifier: str, status_code: int = 404)

Bases: APIRequestError

Raised when an API request results in a 404 or similar resource not found error.

Source code in tux/utils/exceptions.py
Python
def __init__(self, service_name: str, resource_identifier: str, status_code: int = 404):
    self.resource_identifier = resource_identifier
    super().__init__(
        service_name,
        status_code,
        reason=f"Resource '{resource_identifier}' not found.",
    )

APIPermissionError(service_name: str, status_code: int = 403)

Bases: APIRequestError

Raised when an API request fails due to permissions (e.g., 403 Forbidden).

Source code in tux/utils/exceptions.py
Python
def __init__(self, service_name: str, status_code: int = 403):
    super().__init__(
        service_name,
        status_code,
        reason="API request failed due to insufficient permissions.",
    )

CodeExecutionError

Bases: Exception

Base exception for code execution errors.

MissingCodeError()

Bases: CodeExecutionError

Raised when no code is provided for execution.

Source code in tux/utils/exceptions.py
Python
def __init__(self) -> None:
    super().__init__(
        "Please provide code with syntax highlighting in this format:\n"
        '```\n`\u200b``python\nprint("Hello, World!")\n`\u200b``\n```',
    )

InvalidCodeFormatError()

Bases: CodeExecutionError

Raised when code format is invalid.

Source code in tux/utils/exceptions.py
Python
def __init__(self) -> None:
    super().__init__(
        "Please provide code with syntax highlighting in this format:\n"
        '```\n`\u200b``python\nprint("Hello, World!")\n`\u200b``\n```',
    )

UnsupportedLanguageError(language: str, supported_languages: list[str])

Bases: CodeExecutionError

Raised when the specified language is not supported.

Initialize with language-specific error message.

Parameters:

Name Type Description Default
language str

The unsupported language that was requested.

required
supported_languages list[str]

List of supported language names.

required
Source code in tux/utils/exceptions.py
Python
def __init__(self, language: str, supported_languages: list[str]) -> None:
    """
    Initialize with language-specific error message.

    Parameters
    ----------
    language : str
        The unsupported language that was requested.
    supported_languages : list[str]
        List of supported language names.
    """
    self.language = language
    self.supported_languages = supported_languages
    available_langs = ", ".join(supported_languages)

    super().__init__(
        f"No compiler found for `{language}`. The following languages are supported:\n```{available_langs}```",
    )

Functions

CompilationError()

Bases: CodeExecutionError

Raised when code compilation fails.

Source code in tux/utils/exceptions.py
Python
def __init__(self) -> None:
    super().__init__("Failed to get output from the compiler. The code may have compilation errors.")

Functions

handle_gather_result(result: T | BaseException, expected_type: type[T]) -> T

Handle a result from asyncio.gather with return_exceptions=True.

Parameters:

Name Type Description Default
result T | BaseException

The result from asyncio.gather

required
expected_type type[T]

The expected type of the result

required

Returns:

Type Description
T

The result if it matches the expected type

Raises:

Type Description
BaseException

If the result is an exception

TypeError

If the result is not of the expected type

Source code in tux/utils/exceptions.py
Python
def handle_gather_result(result: T | BaseException, expected_type: type[T]) -> T:
    """Handle a result from asyncio.gather with return_exceptions=True.

    Parameters
    ----------
    result : T | BaseException
        The result from asyncio.gather
    expected_type : type[T]
        The expected type of the result

    Returns
    -------
    T
        The result if it matches the expected type

    Raises
    ------
    BaseException
        If the result is an exception
    TypeError
        If the result is not of the expected type
    """
    if isinstance(result, BaseException):
        raise result
    if not isinstance(result, expected_type):
        msg = f"Expected {expected_type.__name__} but got {type(result).__name__}"
        raise TypeError(msg)
    return result

handle_case_result(case_result: Case | BaseException) -> Case

Handle a case result from asyncio.gather with return_exceptions=True.

Text Only
Parameters

case_result : Case | BaseException The case result from asyncio.gather

Returns:

Type Description
Case

The case if valid

Raises:

Type Description
BaseException

If the result is an exception

TypeError

If the result is not a Case

Source code in tux/utils/exceptions.py
Python
def handle_case_result(case_result: Case | BaseException) -> Case:
    """Handle a case result from asyncio.gather with return_exceptions=True.

        Parameters
        ----------
    case_result : Case | BaseException
        The case result from asyncio.gather

    Returns
    -------
    Case
        The case if valid

    Raises
    ------
    BaseException
        If the result is an exception
    TypeError
        If the result is not a Case
    """
    return handle_gather_result(case_result, Case)