Skip to content

Commit 391319b

Browse files
Merge pull request #2108 from VWS-Python/server-error
Return a new error when the server returns a 5xx status code.
2 parents 693c3b0 + ca07d23 commit 391319b

File tree

5 files changed

+100
-8
lines changed

5 files changed

+100
-8
lines changed

CHANGELOG.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ Changelog
44
Next
55
----
66

7+
* Return a new error (``vws.custom_exceptions.ServerError``) when the server returns a 5xx status code.
8+
79
2023.12.27
810
------------
911

src/vws/exceptions/custom_exceptions.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,46 @@ class TargetProcessingTimeout(Exception):
4242
"""
4343
Exception raised when waiting for a target to be processed times out.
4444
"""
45+
46+
47+
class ServerError(Exception): # pragma: no cover
48+
"""
49+
Exception raised when VWS returns a server error.
50+
"""
51+
52+
def __init__(self, response: Response) -> None:
53+
"""
54+
Args:
55+
response: The response returned by Vuforia.
56+
"""
57+
super().__init__(response.text)
58+
self._response = response
59+
60+
@property
61+
def response(self) -> Response:
62+
"""
63+
The response returned by Vuforia which included this error.
64+
"""
65+
return self._response
66+
67+
68+
class TooManyRequests(Exception): # pragma: no cover
69+
"""
70+
Exception raised when Vuforia returns a response with a result code
71+
'TooManyRequests'.
72+
"""
73+
74+
def __init__(self, response: Response) -> None:
75+
"""
76+
Args:
77+
response: The response returned by Vuforia.
78+
"""
79+
super().__init__(response.text)
80+
self._response = response
81+
82+
@property
83+
def response(self) -> Response:
84+
"""
85+
The response returned by Vuforia which included this error.
86+
"""
87+
return self._response

src/vws/exceptions/vws_exceptions.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,6 @@ class RequestQuotaReached(VWSException): # pragma: no cover
5656
"""
5757

5858

59-
class TooManyRequests(VWSException): # pragma: no cover
60-
"""
61-
Exception raised when Vuforia returns a response with a result code
62-
'TooManyRequests'.
63-
"""
64-
65-
6659
class TargetStatusProcessing(VWSException):
6760
"""
6861
Exception raised when Vuforia returns a response with a result code

src/vws/query.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
)
2323
from vws.exceptions.custom_exceptions import (
2424
RequestEntityTooLarge,
25+
ServerError,
2526
)
2627
from vws.include_target_data import CloudRecoIncludeTargetData
2728
from vws.reports import QueryResult, TargetData
@@ -94,6 +95,8 @@ def query(
9495
file in the grayscale or RGB color space.
9596
~vws.exceptions.custom_exceptions.RequestEntityTooLarge: The given
9697
image is too large.
98+
~vws.exceptions.custom_exceptions.ServerError: There is an error
99+
with Vuforia's servers.
97100
98101
Returns:
99102
An ordered list of target details of matching targets.
@@ -145,6 +148,11 @@ def query(
145148
if "Integer out of range" in response.text:
146149
raise MaxNumResultsOutOfRange(response=response)
147150

151+
if (
152+
response.status_code >= HTTPStatus.INTERNAL_SERVER_ERROR
153+
): # pragma: no cover
154+
raise ServerError(response=response)
155+
148156
result_code = response.json()["result_code"]
149157
if result_code != "Success":
150158
exception = {

src/vws/vws.py

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818

1919
from vws.exceptions.custom_exceptions import (
2020
OopsAnErrorOccurredPossiblyBadName,
21+
ServerError,
2122
TargetProcessingTimeout,
23+
TooManyRequests,
2224
)
2325
from vws.exceptions.vws_exceptions import (
2426
AuthenticationFailure,
@@ -36,7 +38,6 @@
3638
TargetQuotaReached,
3739
TargetStatusNotSuccess,
3840
TargetStatusProcessing,
39-
TooManyRequests,
4041
UnknownTarget,
4142
)
4243
from vws.reports import (
@@ -166,6 +167,10 @@ def _make_request(
166167
an HTML page with the text "Oops, an error occurred". This has
167168
been seen to happen when the given name includes a bad
168169
character.
170+
~vws.exceptions.custom_exceptions.ServerError: There is an error
171+
with Vuforia's servers.
172+
~vws.exceptions.custom_exceptions.TooManyRequests: Vuforia is rate
173+
limiting access.
169174
json.decoder.JSONDecodeError: The server did not respond with valid
170175
JSON. This may happen if the server address is not a valid
171176
Vuforia server.
@@ -188,6 +193,11 @@ def _make_request(
188193
# The Vuforia API returns a 429 response with no JSON body.
189194
raise TooManyRequests(response=response)
190195

196+
if (
197+
response.status_code >= HTTPStatus.INTERNAL_SERVER_ERROR
198+
): # pragma: no cover
199+
raise ServerError(response=response)
200+
191201
result_code = response.json()["result_code"]
192202

193203
if result_code == expected_result_code:
@@ -268,6 +278,10 @@ def add_target(
268278
Vuforia returns an HTML page with the text "Oops, an error
269279
occurred". This has been seen to happen when the given name
270280
includes a bad character.
281+
~vws.exceptions.custom_exceptions.ServerError: There is an error
282+
with Vuforia's servers.
283+
~vws.exceptions.custom_exceptions.TooManyRequests: Vuforia is rate
284+
limiting access.
271285
"""
272286
image_data = _get_image_data(image=image)
273287
image_data_encoded = base64.b64encode(image_data).decode("ascii")
@@ -314,6 +328,10 @@ def get_target_record(self, target_id: str) -> TargetStatusAndRecord:
314328
does not match a target in the database.
315329
~vws.exceptions.vws_exceptions.RequestTimeTooSkewed: There is an
316330
error with the time sent to Vuforia.
331+
~vws.exceptions.custom_exceptions.ServerError: There is an error
332+
with Vuforia's servers.
333+
~vws.exceptions.custom_exceptions.TooManyRequests: Vuforia is rate
334+
limiting access.
317335
"""
318336
response = self._make_request(
319337
method="GET",
@@ -371,6 +389,10 @@ def wait_for_target_processed(
371389
does not match a target in the database.
372390
~vws.exceptions.vws_exceptions.RequestTimeTooSkewed: There is an
373391
error with the time sent to Vuforia.
392+
~vws.exceptions.custom_exceptions.ServerError: There is an error
393+
with Vuforia's servers.
394+
~vws.exceptions.custom_exceptions.TooManyRequests: Vuforia is rate
395+
limiting access.
374396
"""
375397
start_time = time.monotonic()
376398
while True:
@@ -402,6 +424,10 @@ def list_targets(self) -> list[str]:
402424
known database.
403425
~vws.exceptions.vws_exceptions.RequestTimeTooSkewed: There is an
404426
error with the time sent to Vuforia.
427+
~vws.exceptions.custom_exceptions.ServerError: There is an error
428+
with Vuforia's servers.
429+
~vws.exceptions.custom_exceptions.TooManyRequests: Vuforia is rate
430+
limiting access.
405431
"""
406432
response = self._make_request(
407433
method="GET",
@@ -435,6 +461,10 @@ def get_target_summary_report(self, target_id: str) -> TargetSummaryReport:
435461
does not match a target in the database.
436462
~vws.exceptions.vws_exceptions.RequestTimeTooSkewed: There is an
437463
error with the time sent to Vuforia.
464+
~vws.exceptions.custom_exceptions.ServerError: There is an error
465+
with Vuforia's servers.
466+
~vws.exceptions.custom_exceptions.TooManyRequests: Vuforia is rate
467+
limiting access.
438468
"""
439469
response = self._make_request(
440470
method="GET",
@@ -474,6 +504,10 @@ def get_database_summary_report(self) -> DatabaseSummaryReport:
474504
known database.
475505
~vws.exceptions.vws_exceptions.RequestTimeTooSkewed: There is an
476506
error with the time sent to Vuforia.
507+
~vws.exceptions.custom_exceptions.ServerError: There is an error
508+
with Vuforia's servers.
509+
~vws.exceptions.custom_exceptions.TooManyRequests: Vuforia is rate
510+
limiting access.
477511
"""
478512
response = self._make_request(
479513
method="GET",
@@ -520,6 +554,10 @@ def delete_target(self, target_id: str) -> None:
520554
target is in the processing state.
521555
~vws.exceptions.vws_exceptions.RequestTimeTooSkewed: There is an
522556
error with the time sent to Vuforia.
557+
~vws.exceptions.custom_exceptions.ServerError: There is an error
558+
with Vuforia's servers.
559+
~vws.exceptions.custom_exceptions.TooManyRequests: Vuforia is rate
560+
limiting access.
523561
"""
524562
self._make_request(
525563
method="DELETE",
@@ -553,6 +591,10 @@ def get_duplicate_targets(self, target_id: str) -> list[str]:
553591
inactive.
554592
~vws.exceptions.vws_exceptions.RequestTimeTooSkewed: There is an
555593
error with the time sent to Vuforia.
594+
~vws.exceptions.custom_exceptions.ServerError: There is an error
595+
with Vuforia's servers.
596+
~vws.exceptions.custom_exceptions.TooManyRequests: Vuforia is rate
597+
limiting access.
556598
"""
557599
response = self._make_request(
558600
method="GET",
@@ -612,6 +654,10 @@ def update_target(
612654
inactive.
613655
~vws.exceptions.vws_exceptions.RequestTimeTooSkewed: There is an
614656
error with the time sent to Vuforia.
657+
~vws.exceptions.custom_exceptions.ServerError: There is an error
658+
with Vuforia's servers.
659+
~vws.exceptions.custom_exceptions.TooManyRequests: Vuforia is rate
660+
limiting access.
615661
"""
616662
data: dict[str, str | bool | float | int] = {}
617663

0 commit comments

Comments
 (0)