forked from DIMO-Network/dimo-python-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors.py
More file actions
41 lines (27 loc) · 1.25 KB
/
errors.py
File metadata and controls
41 lines (27 loc) · 1.25 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
from typing import Any, Type, Union
class DimoError(Exception):
"""Base class for execeptions"""
pass
class DimoTypeError(DimoError):
def __init__(
self, param_name: str, expected_type: Union[Type, tuple], actual_value: Any
):
self.param_name = param_name
self.expected_type = expected_type
self.actual_value = actual_value
self.message = f"{param_name} must be a {expected_type.__name__}, but was entered as type {type(actual_value).__name__}."
super().__init__(self.message)
def check_type(param_name: str, value: Any, expected_type: Union[Type, tuple]):
if not isinstance(value, expected_type):
raise DimoTypeError(param_name, expected_type, value)
def check_optional_type(param_name: str, value: Any, expected_type: Union[Type, tuple]):
if value is not None and not isinstance(value, expected_type):
raise DimoTypeError(param_name, expected_type, value)
class DimoValueError(DimoError):
pass
class HTTPError(Exception):
"""Http error wrapper with status code and (optional) response body"""
def __init__(self, status: int, message: str, body: Any = None):
super().__init__(f"HTTP {status}: {message}")
self.status = status
self.body = body