-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy path_errors.py
More file actions
41 lines (30 loc) · 1.27 KB
/
_errors.py
File metadata and controls
41 lines (30 loc) · 1.27 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
import logging
from typing import Any, Dict, Optional
from aiohttp import ClientResponseError
from zyte_api.errors import ParsedError
logger = logging.getLogger("zyte_api")
class RequestError(ClientResponseError):
"""Exception raised upon receiving a :ref:`rate-limiting
<zapi-rate-limit>` or :ref:`unsuccessful
<zapi-unsuccessful-responses>` response from Zyte API."""
def __init__(self, *args, **kwargs):
#: Query sent to Zyte API.
#:
#: May be slightly different from the input query due to
#: pre-processing logic on the client side.
self.query: Dict[str, Any] = kwargs.pop("query")
#: Request ID.
self.request_id: Optional[str] = kwargs.get("headers", {}).get("request-id")
#: Response body.
self.response_content: Optional[bytes] = kwargs.pop("response_content")
super().__init__(*args, **kwargs)
@property
def parsed(self):
"""Response as a :class:`ParsedError` object."""
return ParsedError.from_body(self.response_content)
def __str__(self):
return (
f"RequestError: {self.status}, message={self.message}, "
f"headers={self.headers}, body={self.response_content}, "
f"request_id={self.request_id}"
)