Skip to content

Commit c6cbcd5

Browse files
Extract error message handling to _error_handling module (#1988)
Move the _get_error_message function from commands.py to a new _error_handling.py module and make it public as get_error_message. This allows other modules to reuse the error handling logic. Co-authored-by: Claude Haiku 4.5 <noreply@anthropic.com>
1 parent 3aa81f6 commit c6cbcd5

2 files changed

Lines changed: 69 additions & 61 deletions

File tree

src/vws_cli/_error_handling.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
"""Error handling utilities for the VWS CLI."""
2+
3+
from beartype import beartype
4+
from vws.exceptions.custom_exceptions import (
5+
ServerError,
6+
TargetProcessingTimeoutError,
7+
)
8+
from vws.exceptions.vws_exceptions import (
9+
AuthenticationFailureError,
10+
BadImageError,
11+
DateRangeError,
12+
FailError,
13+
ImageTooLargeError,
14+
MetadataTooLargeError,
15+
ProjectHasNoAPIAccessError,
16+
ProjectInactiveError,
17+
ProjectSuspendedError,
18+
RequestQuotaReachedError,
19+
RequestTimeTooSkewedError,
20+
TargetNameExistError,
21+
TargetQuotaReachedError,
22+
TargetStatusNotSuccessError,
23+
TargetStatusProcessingError,
24+
UnknownTargetError,
25+
)
26+
27+
28+
@beartype
29+
def get_error_message(exc: Exception) -> str:
30+
"""Get an error message from a VWS exception."""
31+
if isinstance(exc, UnknownTargetError):
32+
return f'Error: Target "{exc.target_id}" does not exist.'
33+
34+
if isinstance(exc, TargetNameExistError):
35+
return f'Error: There is already a target named "{exc.target_name}".'
36+
37+
if isinstance(exc, TargetStatusNotSuccessError):
38+
return (
39+
f'Error: The target "{exc.target_id}" cannot be updated as it is '
40+
"in the processing state."
41+
)
42+
43+
if isinstance(exc, TargetStatusProcessingError):
44+
return (
45+
f'Error: The target "{exc.target_id}" cannot be deleted as it is '
46+
"in the processing state."
47+
)
48+
49+
exc_type_to_message: dict[type[Exception], str] = {
50+
AuthenticationFailureError: "The given secret key was incorrect.",
51+
BadImageError: "Error: The given image is corrupted or the format is not supported.",
52+
DateRangeError: "Error: There was a problem with the date details given in the request.",
53+
FailError: "Error: The request made to Vuforia was invalid and could not be processed. Check the given parameters.",
54+
ImageTooLargeError: "Error: The given image is too large.",
55+
MetadataTooLargeError: "Error: The given metadata is too large.",
56+
ServerError: "Error: There was an unknown error from Vuforia. This may be because there is a problem with the given name.",
57+
ProjectInactiveError: "Error: The project associated with the given keys is inactive.",
58+
RequestQuotaReachedError: "Error: The maximum number of API calls for this database has been reached.",
59+
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.",
60+
TargetProcessingTimeoutError: "Error: The target processing time has exceeded the allowed limit.",
61+
TargetQuotaReachedError: "Error: The maximum number of targets for this database has been reached.",
62+
ProjectSuspendedError: "Error: The request could not be completed because this database has been suspended.",
63+
ProjectHasNoAPIAccessError: "Error: The request could not be completed because this database is not allowed to make API requests.",
64+
}
65+
66+
return exc_type_to_message[type(exc)]

src/vws_cli/commands.py

Lines changed: 3 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import dataclasses
55
import io
66
import sys
7-
from collections.abc import Iterator, Mapping
7+
from collections.abc import Iterator
88
from pathlib import Path
99

1010
import click
@@ -16,25 +16,8 @@
1616
ServerError,
1717
TargetProcessingTimeoutError,
1818
)
19-
from vws.exceptions.vws_exceptions import (
20-
AuthenticationFailureError,
21-
BadImageError,
22-
DateRangeError,
23-
FailError,
24-
ImageTooLargeError,
25-
MetadataTooLargeError,
26-
ProjectHasNoAPIAccessError,
27-
ProjectInactiveError,
28-
ProjectSuspendedError,
29-
RequestQuotaReachedError,
30-
RequestTimeTooSkewedError,
31-
TargetNameExistError,
32-
TargetQuotaReachedError,
33-
TargetStatusNotSuccessError,
34-
TargetStatusProcessingError,
35-
UnknownTargetError,
36-
)
3719

20+
from vws_cli._error_handling import get_error_message
3821
from vws_cli.options.credentials import (
3922
server_access_key_option,
4023
server_secret_key_option,
@@ -55,47 +38,6 @@
5538
from vws_cli.options.vws import base_vws_url_option
5639

5740

58-
@beartype
59-
def _get_error_message(exc: Exception) -> str:
60-
"""Get an error message from a VWS exception."""
61-
if isinstance(exc, UnknownTargetError):
62-
return f'Error: Target "{exc.target_id}" does not exist.'
63-
64-
if isinstance(exc, TargetNameExistError):
65-
return f'Error: There is already a target named "{exc.target_name}".'
66-
67-
if isinstance(exc, TargetStatusNotSuccessError):
68-
return (
69-
f'Error: The target "{exc.target_id}" cannot be updated as it is '
70-
"in the processing state."
71-
)
72-
73-
if isinstance(exc, TargetStatusProcessingError):
74-
return (
75-
f'Error: The target "{exc.target_id}" cannot be deleted as it is '
76-
"in the processing state."
77-
)
78-
79-
exc_type_to_message: Mapping[type[Exception], str] = {
80-
AuthenticationFailureError: "The given secret key was incorrect.",
81-
BadImageError: "Error: The given image is corrupted or the format is not supported.",
82-
DateRangeError: "Error: There was a problem with the date details given in the request.",
83-
FailError: "Error: The request made to Vuforia was invalid and could not be processed. Check the given parameters.",
84-
ImageTooLargeError: "Error: The given image is too large.",
85-
MetadataTooLargeError: "Error: The given metadata is too large.",
86-
ServerError: "Error: There was an unknown error from Vuforia. This may be because there is a problem with the given name.",
87-
ProjectInactiveError: "Error: The project associated with the given keys is inactive.",
88-
RequestQuotaReachedError: "Error: The maximum number of API calls for this database has been reached.",
89-
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.",
90-
TargetProcessingTimeoutError: "Error: The target processing time has exceeded the allowed limit.",
91-
TargetQuotaReachedError: "Error: The maximum number of targets for this database has been reached.",
92-
ProjectSuspendedError: "Error: The request could not be completed because this database has been suspended.",
93-
ProjectHasNoAPIAccessError: "Error: The request could not be completed because this database is not allowed to make API requests.",
94-
}
95-
96-
return exc_type_to_message[type(exc)]
97-
98-
9941
@beartype
10042
@contextlib.contextmanager
10143
def _handle_vws_exceptions() -> Iterator[None]:
@@ -109,7 +51,7 @@ def _handle_vws_exceptions() -> Iterator[None]:
10951
ServerError,
11052
TargetProcessingTimeoutError,
11153
) as exc:
112-
error_message = _get_error_message(exc=exc)
54+
error_message = get_error_message(exc=exc)
11355
else:
11456
return
11557

0 commit comments

Comments
 (0)