Skip to content

Commit d1a8d97

Browse files
committed
Add custom exception for oops error
1 parent 5452858 commit d1a8d97

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed

src/vws/_result_codes.py

Lines changed: 9 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

@@ -32,7 +35,12 @@ def raise_for_result_code(
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
3437
"""
35-
result_code = response.json()['result_code']
38+
try:
39+
result_code = response.json()['result_code']
40+
except json.decoder.JSONDecodeError as exc:
41+
assert 'Oops' in response.text
42+
raise UnknownVWSErrorPossiblyBadName() from exc
43+
3644
if result_code == expected_result_code:
3745
return
3846

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
):

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)