-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathexceptions.py
More file actions
51 lines (30 loc) · 1.32 KB
/
exceptions.py
File metadata and controls
51 lines (30 loc) · 1.32 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
import requests
class SerpApiError(Exception):
"""Base class for exceptions in this module."""
pass
class APIKeyNotProvided(ValueError, SerpApiError):
"""API key is not provided."""
pass
class SearchIDNotProvided(ValueError, SerpApiError):
"""Search ID is not provided."""
pass
class HTTPError(requests.exceptions.HTTPError, SerpApiError):
"""HTTP Error."""
def __init__(self, original_exception):
if (isinstance(original_exception, requests.exceptions.HTTPError)):
http_error_exception: requests.exceptions.HTTPError = original_exception
self.status_code = http_error_exception.response.status_code
try:
self.error = http_error_exception.response.json().get("error", None)
except requests.exceptions.JSONDecodeError:
self.error = None
else:
self.status_code = -1
self.error = None
super().__init__(*original_exception.args, response=getattr(original_exception, 'response', None), request=getattr(original_exception, 'request', None))
class HTTPConnectionError(HTTPError, requests.exceptions.ConnectionError, SerpApiError):
"""Connection Error."""
pass
class TimeoutError(requests.exceptions.Timeout, SerpApiError):
"""Timeout Error."""
pass