Skip to content

Commit acef908

Browse files
Merge pull request #1071 from adamtheturtle/oops
Add an exception for the bad char case
2 parents 5452858 + 5c5f3fc commit acef908

File tree

5 files changed

+40
-1
lines changed

5 files changed

+40
-1
lines changed

spelling_private_dict.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ TargetProcessingTimeout
1212
TargetStatusProcessing
1313
Ubuntu
1414
UnknownTarget
15+
UnknownVWSErrorPossiblyBadName
1516
api
1617
args
1718
ascii

src/vws/_result_codes.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
Tools for managing result codes.
33
"""
44

5+
import json
6+
57
from requests import Response
68

79
from vws.exceptions import (
@@ -16,6 +18,7 @@
1618
TargetStatusNotSuccess,
1719
TargetStatusProcessing,
1820
UnknownTarget,
21+
UnknownVWSErrorPossiblyBadName,
1922
)
2023

2124

@@ -31,8 +34,18 @@ def raise_for_result_code(
3134
response: A response from Vuforia.
3235
expected_result_code: See
3336
https://library.vuforia.com/articles/Solution/How-To-Use-the-Vuforia-Web-Services-API.html#How-To-Interperete-VWS-API-Result-Codes
37+
38+
Raises:
39+
~vws.exceptions.UnknownVWSErrorPossiblyBadName: Vuforia returns an HTML
40+
page with the text "Oops, an error occurred". This has been seen to
41+
happen when the given name includes a bad character.
3442
"""
35-
result_code = response.json()['result_code']
43+
try:
44+
result_code = response.json()['result_code']
45+
except json.decoder.JSONDecodeError as exc:
46+
assert 'Oops' in response.text
47+
raise UnknownVWSErrorPossiblyBadName() from exc
48+
3649
if result_code == expected_result_code:
3750
return
3851

src/vws/exceptions.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,15 @@
66
from requests import Response
77

88

9+
class UnknownVWSErrorPossiblyBadName(Exception):
10+
"""
11+
Exception raised when VWS returns an HTML page which says "Oops, an error
12+
occurred".
13+
14+
This has been seen to happen when the given name includes a bad character.
15+
"""
16+
17+
918
class ConnectionErrorPossiblyImageTooLarge(
1019
requests.exceptions.ConnectionError,
1120
):

src/vws/vws.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,10 @@ def add_target(
178178
~vws.exceptions.ProjectInactive: The project is inactive.
179179
~vws.exceptions.RequestTimeTooSkewed: There is an error with the
180180
time sent to Vuforia.
181+
~vws.exceptions.UnknownVWSErrorPossiblyBadName: Vuforia returns an
182+
HTML page with the text "Oops, an error occurred". This has
183+
been seen to happen when the given name includes a bad
184+
character.
181185
"""
182186
image_data = image.getvalue()
183187
image_data_encoded = base64.b64encode(image_data).decode('ascii')

tests/test_exceptions.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
TargetStatusNotSuccess,
2626
TargetStatusProcessing,
2727
UnknownTarget,
28+
UnknownVWSErrorPossiblyBadName,
2829
)
2930

3031

@@ -52,6 +53,17 @@ def test_invalid_given_id(vws_client: VWS) -> None:
5253
assert exc.value.response.status_code == codes.NOT_FOUND
5354

5455

56+
def test_add_bad_name(vws_client: VWS, high_quality_image: io.BytesIO) -> None:
57+
"""
58+
When a name with a bad character is given, an
59+
``UnknownVWSErrorPossiblyBadName`` exception is raised.
60+
"""
61+
max_char_value = 65535
62+
bad_name = chr(max_char_value + 1)
63+
with pytest.raises(UnknownVWSErrorPossiblyBadName):
64+
vws_client.add_target(name=bad_name, width=1, image=high_quality_image)
65+
66+
5567
def test_request_quota_reached() -> None:
5668
"""
5769
See https://github.com/adamtheturtle/vws-python/issues/822 for writing

0 commit comments

Comments
 (0)