Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions src/vws_cli/_error_handling.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
"""Error handling utilities for the VWS CLI."""

from beartype import beartype
from vws.exceptions.custom_exceptions import (
ServerError,
TargetProcessingTimeoutError,
)
from vws.exceptions.vws_exceptions import (
AuthenticationFailureError,
BadImageError,
DateRangeError,
FailError,
ImageTooLargeError,
MetadataTooLargeError,
ProjectHasNoAPIAccessError,
ProjectInactiveError,
ProjectSuspendedError,
RequestQuotaReachedError,
RequestTimeTooSkewedError,
TargetNameExistError,
TargetQuotaReachedError,
TargetStatusNotSuccessError,
TargetStatusProcessingError,
UnknownTargetError,
)


@beartype
def get_error_message(exc: Exception) -> str:
"""Get an error message from a VWS exception."""
if isinstance(exc, UnknownTargetError):
return f'Error: Target "{exc.target_id}" does not exist.'

if isinstance(exc, TargetNameExistError):
return f'Error: There is already a target named "{exc.target_name}".'

if isinstance(exc, TargetStatusNotSuccessError):
return (
f'Error: The target "{exc.target_id}" cannot be updated as it is '
"in the processing state."
)

if isinstance(exc, TargetStatusProcessingError):
return (
f'Error: The target "{exc.target_id}" cannot be deleted as it is '
"in the processing state."
)

exc_type_to_message: dict[type[Exception], str] = {
AuthenticationFailureError: "The given secret key was incorrect.",
BadImageError: "Error: The given image is corrupted or the format is not supported.",
DateRangeError: "Error: There was a problem with the date details given in the request.",
FailError: "Error: The request made to Vuforia was invalid and could not be processed. Check the given parameters.",
ImageTooLargeError: "Error: The given image is too large.",
MetadataTooLargeError: "Error: The given metadata is too large.",
ServerError: "Error: There was an unknown error from Vuforia. This may be because there is a problem with the given name.",
ProjectInactiveError: "Error: The project associated with the given keys is inactive.",
RequestQuotaReachedError: "Error: The maximum number of API calls for this database has been reached.",
RequestTimeTooSkewedError: "Error: Vuforia reported that the time given with this request was outside the expected range. This may be because the system clock is out of sync.",
TargetProcessingTimeoutError: "Error: The target processing time has exceeded the allowed limit.",
TargetQuotaReachedError: "Error: The maximum number of targets for this database has been reached.",
ProjectSuspendedError: "Error: The request could not be completed because this database has been suspended.",
ProjectHasNoAPIAccessError: "Error: The request could not be completed because this database is not allowed to make API requests.",
}

return exc_type_to_message[type(exc)]
64 changes: 3 additions & 61 deletions src/vws_cli/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import dataclasses
import io
import sys
from collections.abc import Iterator, Mapping
from collections.abc import Iterator
from pathlib import Path

import click
Expand All @@ -16,25 +16,8 @@
ServerError,
TargetProcessingTimeoutError,
)
from vws.exceptions.vws_exceptions import (
AuthenticationFailureError,
BadImageError,
DateRangeError,
FailError,
ImageTooLargeError,
MetadataTooLargeError,
ProjectHasNoAPIAccessError,
ProjectInactiveError,
ProjectSuspendedError,
RequestQuotaReachedError,
RequestTimeTooSkewedError,
TargetNameExistError,
TargetQuotaReachedError,
TargetStatusNotSuccessError,
TargetStatusProcessingError,
UnknownTargetError,
)

from vws_cli._error_handling import get_error_message
from vws_cli.options.credentials import (
server_access_key_option,
server_secret_key_option,
Expand All @@ -55,47 +38,6 @@
from vws_cli.options.vws import base_vws_url_option


@beartype
def _get_error_message(exc: Exception) -> str:
"""Get an error message from a VWS exception."""
if isinstance(exc, UnknownTargetError):
return f'Error: Target "{exc.target_id}" does not exist.'

if isinstance(exc, TargetNameExistError):
return f'Error: There is already a target named "{exc.target_name}".'

if isinstance(exc, TargetStatusNotSuccessError):
return (
f'Error: The target "{exc.target_id}" cannot be updated as it is '
"in the processing state."
)

if isinstance(exc, TargetStatusProcessingError):
return (
f'Error: The target "{exc.target_id}" cannot be deleted as it is '
"in the processing state."
)

exc_type_to_message: Mapping[type[Exception], str] = {
AuthenticationFailureError: "The given secret key was incorrect.",
BadImageError: "Error: The given image is corrupted or the format is not supported.",
DateRangeError: "Error: There was a problem with the date details given in the request.",
FailError: "Error: The request made to Vuforia was invalid and could not be processed. Check the given parameters.",
ImageTooLargeError: "Error: The given image is too large.",
MetadataTooLargeError: "Error: The given metadata is too large.",
ServerError: "Error: There was an unknown error from Vuforia. This may be because there is a problem with the given name.",
ProjectInactiveError: "Error: The project associated with the given keys is inactive.",
RequestQuotaReachedError: "Error: The maximum number of API calls for this database has been reached.",
RequestTimeTooSkewedError: "Error: Vuforia reported that the time given with this request was outside the expected range. This may be because the system clock is out of sync.",
TargetProcessingTimeoutError: "Error: The target processing time has exceeded the allowed limit.",
TargetQuotaReachedError: "Error: The maximum number of targets for this database has been reached.",
ProjectSuspendedError: "Error: The request could not be completed because this database has been suspended.",
ProjectHasNoAPIAccessError: "Error: The request could not be completed because this database is not allowed to make API requests.",
}

return exc_type_to_message[type(exc)]


@beartype
@contextlib.contextmanager
def _handle_vws_exceptions() -> Iterator[None]:
Expand All @@ -109,7 +51,7 @@ def _handle_vws_exceptions() -> Iterator[None]:
ServerError,
TargetProcessingTimeoutError,
) as exc:
error_message = _get_error_message(exc=exc)
error_message = get_error_message(exc=exc)
else:
return

Expand Down