Skip to content

Commit dc96955

Browse files
committed
Start of moving VWS exceptions around so we separate VWS exceptions from Cloud Reco exceptions and custom exceptions
1 parent 08913ab commit dc96955

File tree

8 files changed

+123
-122
lines changed

8 files changed

+123
-122
lines changed

src/vws/_result_codes.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66

77
from requests import Response
88

9-
from vws.exceptions import (
9+
from vws.exceptions.custom_exceptions import UnknownVWSErrorPossiblyBadName
10+
from vws.exceptions.vws_exceptions import (
1011
AuthenticationFailure,
1112
BadImage,
1213
DateRangeError,
@@ -23,7 +24,6 @@
2324
TargetStatusNotSuccess,
2425
TargetStatusProcessing,
2526
UnknownTarget,
26-
UnknownVWSErrorPossiblyBadName,
2727
)
2828

2929

@@ -49,7 +49,7 @@ def raise_for_result_code(
4949
result_code = response.json()['result_code']
5050
except json.decoder.JSONDecodeError as exc:
5151
assert 'Oops' in response.text
52-
raise UnknownVWSErrorPossiblyBadName(response=response) from exc
52+
raise UnknownVWSErrorPossiblyBadName() from exc
5353

5454
if result_code == expected_result_code:
5555
return

src/vws/exceptions/__init__.py

Whitespace-only changes.
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
from requests import Response
2+
3+
4+
class CloudRecoException(Exception):
5+
"""
6+
Base class for Vuforia Cloud Recognition Web API exceptions.
7+
"""
8+
9+
def __init__(self, response: Response) -> None:
10+
"""
11+
Args:
12+
response: The response to a request to Vuforia.
13+
"""
14+
super().__init__(response.text)
15+
self._response = response
16+
17+
@property
18+
def response(self) -> Response:
19+
"""
20+
The response returned by Vuforia which included this error.
21+
"""
22+
return self._response
23+
24+
25+
class VWSException(Exception):
26+
"""
27+
Base class for Vuforia Web Service errors.
28+
"""
29+
30+
def __init__(self, response: Response) -> None:
31+
"""
32+
Args:
33+
response: The response to a request to Vuforia.
34+
"""
35+
super().__init__(response.text)
36+
self._response = response
37+
38+
@property
39+
def response(self) -> Response:
40+
"""
41+
The response returned by Vuforia which included this error.
42+
"""
43+
return self._response
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from vws.exceptions.base_exceptions import CloudRecoException
2+
3+
4+
class MatchProcessing(CloudRecoException):
5+
"""
6+
Exception raised when a query is made with an image which matches a target
7+
which is processing or has recently been deleted.
8+
"""
9+
10+
11+
class MaxNumResultsOutOfRange(CloudRecoException):
12+
"""
13+
Exception raised when the ``max_num_results`` given to the Cloud
14+
Recognition Web API query endpoint is out of range.
15+
"""
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import requests
2+
3+
4+
class UnknownVWSErrorPossiblyBadName(Exception):
5+
"""
6+
Exception raised when VWS returns an HTML page which says "Oops, an error
7+
occurred".
8+
9+
This has been seen to happen when the given name includes a bad character.
10+
"""
11+
12+
13+
class ConnectionErrorPossiblyImageTooLarge(requests.ConnectionError):
14+
"""
15+
Exception raised when a ConnectionError is raised from a query. This has
16+
been seen to happen when the given image is too large.
17+
"""
18+
19+
20+
class TargetProcessingTimeout(Exception):
21+
"""
22+
Exception raised when waiting for a target to be processed times out.
23+
"""
Original file line numberDiff line numberDiff line change
@@ -1,84 +1,7 @@
1-
"""
2-
Custom exceptions for Vuforia errors.
3-
"""
4-
51
import json
62
from urllib.parse import urlparse
73

8-
import requests
9-
from requests import Response
10-
11-
12-
class CloudRecoException(Exception):
13-
"""
14-
Base class for Vuforia Cloud Recognition Web API exceptions.
15-
"""
16-
17-
def __init__(self, response: Response) -> None:
18-
"""
19-
Args:
20-
response: The response to a request to Vuforia.
21-
"""
22-
super().__init__(response.text)
23-
self._response = response
24-
25-
@property
26-
def response(self) -> Response:
27-
"""
28-
The response returned by Vuforia which included this error.
29-
"""
30-
return self._response
31-
32-
33-
class VWSException(Exception):
34-
"""
35-
Base class for Vuforia Web Service errors.
36-
"""
37-
38-
def __init__(self, response: Response) -> None:
39-
"""
40-
Args:
41-
response: The response to a request to Vuforia.
42-
"""
43-
super().__init__(response.text)
44-
self._response = response
45-
46-
@property
47-
def response(self) -> Response:
48-
"""
49-
The response returned by Vuforia which included this error.
50-
"""
51-
return self._response
52-
53-
54-
class UnknownVWSErrorPossiblyBadName(VWSException):
55-
"""
56-
Exception raised when VWS returns an HTML page which says "Oops, an error
57-
occurred".
58-
59-
This has been seen to happen when the given name includes a bad character.
60-
"""
61-
62-
63-
class ConnectionErrorPossiblyImageTooLarge(requests.ConnectionError):
64-
"""
65-
Exception raised when a ConnectionError is raised from a query. This has
66-
been seen to happen when the given image is too large.
67-
"""
68-
69-
70-
class MatchProcessing(CloudRecoException):
71-
"""
72-
Exception raised when a query is made with an image which matches a target
73-
which is processing or has recently been deleted.
74-
"""
75-
76-
77-
class MaxNumResultsOutOfRange(CloudRecoException):
78-
"""
79-
Exception raised when the ``max_num_results`` given to the Cloud
80-
Recognition Web API query endpoint is out of range.
81-
"""
4+
from vws.exceptions.base_exceptions import VWSException
825

836

847
class UnknownTarget(VWSException):
@@ -144,6 +67,39 @@ def target_id(self) -> str:
14467
return path.split(sep='/', maxsplit=2)[-1]
14568

14669

70+
# This is not simulated by the mock.
71+
class DateRangeError(VWSException): # pragma: no cover
72+
"""
73+
Exception raised when Vuforia returns a response with a result code
74+
'DateRangeError'.
75+
"""
76+
77+
78+
# This is not simulated by the mock.
79+
class TargetQuotaReached(VWSException): # pragma: no cover
80+
"""
81+
Exception raised when Vuforia returns a response with a result code
82+
'TargetQuotaReached'.
83+
"""
84+
85+
86+
# This is not simulated by the mock.
87+
class ProjectSuspended(VWSException): # pragma: no cover
88+
"""
89+
Exception raised when Vuforia returns a response with a result code
90+
'ProjectSuspended'.
91+
"""
92+
93+
94+
# This is not simulated by the mock.
95+
class ProjectHasNoAPIAccess(VWSException): # pragma: no cover
96+
"""
97+
Exception raised when Vuforia returns a response with a result code
98+
'ProjectHasNoAPIAccess'.
99+
"""
100+
101+
102+
# TODO separate InactiveProject
147103
class ProjectInactive(VWSException):
148104
"""
149105
Exception raised when Vuforia returns a response with a result code
@@ -203,41 +159,3 @@ def target_id(self) -> str:
203159
# Every HTTP path which can raise this error is in the format
204160
# `/something/{target_id}`.
205161
return path.split(sep='/', maxsplit=2)[-1]
206-
207-
208-
# This is not simulated by the mock.
209-
class DateRangeError(VWSException): # pragma: no cover
210-
"""
211-
Exception raised when Vuforia returns a response with a result code
212-
'DateRangeError'.
213-
"""
214-
215-
216-
# This is not simulated by the mock.
217-
class TargetQuotaReached(VWSException): # pragma: no cover
218-
"""
219-
Exception raised when Vuforia returns a response with a result code
220-
'TargetQuotaReached'.
221-
"""
222-
223-
224-
# This is not simulated by the mock.
225-
class ProjectSuspended(VWSException): # pragma: no cover
226-
"""
227-
Exception raised when Vuforia returns a response with a result code
228-
'ProjectSuspended'.
229-
"""
230-
231-
232-
# This is not simulated by the mock.
233-
class ProjectHasNoAPIAccess(VWSException): # pragma: no cover
234-
"""
235-
Exception raised when Vuforia returns a response with a result code
236-
'ProjectHasNoAPIAccess'.
237-
"""
238-
239-
240-
class TargetProcessingTimeout(Exception):
241-
"""
242-
Exception raised when waiting for a target to be processed times out.
243-
"""

src/vws/query.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,13 @@
1212
from vws_auth_tools import authorization_header, rfc_1123_date
1313

1414
from vws._result_codes import raise_for_result_code
15-
from vws.exceptions import (
16-
ConnectionErrorPossiblyImageTooLarge,
15+
from vws.exceptions.cloud_reco_exceptions import (
1716
MatchProcessing,
1817
MaxNumResultsOutOfRange,
1918
)
19+
from vws.exceptions.custom_exceptions import (
20+
ConnectionErrorPossiblyImageTooLarge,
21+
)
2022
from vws.include_target_data import CloudRecoIncludeTargetData
2123
from vws.reports import QueryResult, TargetData
2224

src/vws/vws.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
from vws_auth_tools import authorization_header, rfc_1123_date
1818

1919
from vws._result_codes import raise_for_result_code
20-
from vws.exceptions import TargetProcessingTimeout
20+
from vws.exceptions.custom_exceptions import TargetProcessingTimeout
2121
from vws.reports import (
2222
DatabaseSummaryReport,
2323
TargetRecord,

0 commit comments

Comments
 (0)