Skip to content

exceptions

exceptions

Provide exception classes for the itkdb package.

BadJSON

Bases: ResponseException

Indicate the response did not contain valid JSON.

BadRequest

Bases: ResponseException

Indicate invalid parameters for the request.

Conflict

Bases: ResponseException

Indicate a conflicting change in the target resource.

Forbidden

Forbidden(response)

Bases: ResponseException

Indicate the authentication is not permitted for the request.

Source code in itkdb/exceptions.py
def __init__(self, response):
    additional_message = None

    with suppress(JSONDecodeError):
        additional_message = (
            response.json()
            .get("uuAppErrorMap", {})
            .get("uu-app-workspace/authorization/userIsNotAuthorized", {})
            .get("message", None)
        )
    super().__init__(response, additional_message)

ITkDBException

Bases: Exception

Base exception class for exceptions that occur within this package.

InvalidInvocation

Bases: ITkDBException

Indicate that the code to execute cannot be completed.

NotFound

Bases: ResponseException

Indicate that the requested URL was not found.

Redirect

Redirect(response)

Bases: ResponseException

Indicate the request resulted in a redirect.

This class adds the attribute path, which is the path to which the response redirects.

:param response: A requests.response instance containing a location header.

Source code in itkdb/exceptions.py
def __init__(self, response):
    """Initialize a Redirect exception instance.

    :param response: A requests.response instance containing a location
    header.

    """
    path = urlparse(response.headers["location"]).path
    self.path = path[:-5] if path.endswith(".json") else path
    self.response = response
    super().__init__(f"Redirect to {self.path}")

path instance-attribute

path = path[:-5] if path.endswith('.json') else path

response instance-attribute

response = response

ResponseException

ResponseException(response, additional_message = None)

Bases: ITkDBException

Indicate that there was an error with the completed HTTP request.

Source code in itkdb/exceptions.py
def __init__(self, response, additional_message=None):
    """Initialize a ResponseException instance.
    :param response: A requests.response instance.
    """
    self.response = response
    message = f"received {response.status_code} HTTP response for following request\n{pretty_print(response.request)}"

    try:
        if additional_message is None:
            additional_message = json.dumps(response.json(), indent=2)

    except JSONDecodeError:
        additional_message = response.text.strip()

    if additional_message:
        if "text/html" in response.headers.get("content-type", ""):
            additional_message = html2text(additional_message)

        message = (
            f"{message}\n\nThe following details may help:\n{additional_message}"
        )
    super().__init__(message)

response instance-attribute

response = response

ServerError

Bases: ResponseException

Indicate issues on the server end preventing request fulfillment.

SpecialError

SpecialError(response)

Bases: ResponseException

Indicate syntax or spam-prevention issues.

:param response: A requests.response instance containing a message and a list of special errors.

Source code in itkdb/exceptions.py
def __init__(self, response):
    """Initialize a SpecialError exception instance.

    :param response: A requests.response instance containing a message
    and a list of special errors.

    """
    self.response = response

    resp_dict = self.response.json()  # assumes valid JSON
    self.message = resp_dict.get("message", "")
    self.reason = resp_dict.get("reason", "")
    self.special_errors = resp_dict.get("special_errors", [])
    super().__init__(f"Special error {self.message!r}")

message instance-attribute

message = resp_dict.get('message', '')

reason instance-attribute

reason = resp_dict.get('reason', '')

response instance-attribute

response = response

special_errors instance-attribute

special_errors = resp_dict.get('special_errors', [])

TooLarge

Bases: ResponseException

Indicate that the request data exceeds the allowed limit.

UnavailableForLegalReasons

Bases: ResponseException

Indicate that the requested URL is unavailable due to legal reasons.

UnhandledResponse

Bases: ResponseException

Indicate a response status code we have not dealt with yet.

html2text

html2text(string)

Pass-through function.

Source code in itkdb/exceptions.py
def html2text(string):
    """
    Pass-through function.
    """
    return string

Last update: April 19, 2023