Repository navigation
Expand file tree
/
Copy pathexceptions.py
More file actions
59 lines (46 loc) · 1.93 KB
/
Copy pathexceptions.py
File metadata and controls
59 lines (46 loc) · 1.93 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
"""Exceptions raised by the Picnic API client.
Authentication-related exceptions live in :mod:`python_picnic_api2.session` for
historical reasons and are re-exported here so that all public exceptions can be
imported from a single place.
"""
from .session import Picnic2FAError, Picnic2FARequired, PicnicAuthError
class PicnicAPIError(ValueError):
"""Raised when the Picnic API returns a body that is not JSON.
Picnic normally returns JSON, including for errors, but its edge/WAF can
answer with an HTML or plain-text page (e.g. a 403 on ``/cart``). Raised
instead of a bare ``JSONDecodeError`` so the HTTP status and the start of
the body end up in the message.
Subclasses :class:`ValueError` because callers treat an undecodable body as
a transient response error: Home Assistant's Picnic coordinator maps
``ValueError`` to ``UpdateFailed`` and suppresses it for endpoints that
legitimately return an empty body.
"""
def __init__(
self,
message: str,
*,
status_code: int | None = None,
body: str | None = None,
):
super().__init__(message)
self.status_code = status_code
self.body = body
class PicnicParseError(Exception):
"""Raised when a Picnic PML ("layout") response cannot be parsed into a model.
The Picnic ``/pages/*`` endpoints return a UI layout tree of widgets that
varies by country and A/B test. When an expected node or field is missing
this is raised instead of a bare ``KeyError`` so callers get a clear,
actionable message about what was being looked for.
"""
def __init__(self, message: str, *, endpoint: str | None = None):
if endpoint:
message = f"{message} (endpoint: {endpoint})"
super().__init__(message)
self.endpoint = endpoint
__all__ = [
"PicnicAPIError",
"PicnicParseError",
"PicnicAuthError",
"Picnic2FARequired",
"Picnic2FAError",
]