-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patherrors.py
More file actions
62 lines (46 loc) · 1.86 KB
/
errors.py
File metadata and controls
62 lines (46 loc) · 1.86 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
from typing import Any, Dict, Optional, Protocol, runtime_checkable
@runtime_checkable
class ExceptionWithHeaders(Protocol):
"""
Protocol for exceptions that include HTTP response headers.
This allows type-safe access to headers from error responses without
using hasattr checks.
"""
@property
def headers(self) -> Optional[Dict[str, Any]]:
"""The HTTP response headers associated with this exception."""
raise NotImplementedError
class HTTPStatusError(Exception):
"""
This exception indicates that the client was able to connect to the server, but that
the HTTP response had an error status.
When available, the response headers are accessible via the :attr:`headers` property.
"""
def __init__(self, status: int, headers: Optional[Dict[str, Any]] = None):
super().__init__("HTTP error %d" % status)
self._status = status
self._headers = headers
@property
def status(self) -> int:
return self._status
@property
def headers(self) -> Optional[Dict[str, Any]]:
"""The HTTP response headers, if available."""
return self._headers
class HTTPContentTypeError(Exception):
"""
This exception indicates that the HTTP response did not have the expected content
type of `"text/event-stream"`.
When available, the response headers are accessible via the :attr:`headers` property.
"""
def __init__(self, content_type: str, headers: Optional[Dict[str, Any]] = None):
super().__init__("invalid content type \"%s\"" % content_type)
self._content_type = content_type
self._headers = headers
@property
def content_type(self) -> str:
return self._content_type
@property
def headers(self) -> Optional[Dict[str, Any]]:
"""The HTTP response headers, if available."""
return self._headers