-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathbase_exceptions.py
More file actions
74 lines (59 loc) · 2.01 KB
/
Copy pathbase_exceptions.py
File metadata and controls
74 lines (59 loc) · 2.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
"""
Base exceptions for errors returned by Vuforia Web Services or the
Vuforia
Cloud Recognition Web API.
"""
from collections.abc import Mapping # noqa: TC003
from typing import ClassVar
from beartype import beartype
from vws.response import Response # noqa: TC001
@beartype
class CloudRecoError(Exception):
"""Base class for Vuforia Cloud Recognition Web API exceptions."""
def __init__(self, response: Response) -> None:
"""
Args:
response: The response to a request to Vuforia.
"""
super().__init__(response.text)
self._response = response
@property
def response(self) -> Response:
"""The response returned by Vuforia which included this error."""
return self._response
@beartype
class VWSError(Exception):
"""Base class for Vuforia Web Services errors.
These errors are defined at
https://developer.vuforia.com/library/web-api/cloud-targets-web-services-api#result-codes.
"""
_exceptions_by_result_code: ClassVar[dict[str, type[VWSError]]] = {}
def __init__(self, response: Response) -> None:
"""
Args:
response: The response to a request to Vuforia.
"""
super().__init__()
self._response = response
@classmethod
def register_exceptions_by_result_code(
cls,
*,
exceptions_by_result_code: Mapping[str, type[VWSError]],
) -> None:
"""Register ``result_code`` to exception mappings."""
cls._exceptions_by_result_code.update(exceptions_by_result_code)
@classmethod
def from_result_code(
cls,
*,
result_code: str,
response: Response,
) -> VWSError:
"""Create the mapped exception for a VWS ``result_code``."""
exception_type = cls._exceptions_by_result_code[result_code]
return exception_type(response=response)
@property
def response(self) -> Response:
"""The response returned by Vuforia which included this error."""
return self._response