Debian Codegraph @

asynceapi/errors.py

raw symbols SCIP
1 # Copyright (c) 2024-2026 Arista Networks, Inc.
2 # Use of this source code is governed by the Apache License 2.0
3 # that can be found in the LICENSE file.
4 # Initially written by Jeremy Schulman at https://github.com/jeremyschulman/aio-eapi
5 """asynceapi module exceptions."""
6
7 from __future__ import annotations
8
9 from typing import TYPE_CHECKING
10
11 import httpx
12
13 if TYPE_CHECKING:
14 from ._types import EapiComplexCommand, EapiJsonOutput, EapiSimpleCommand, EapiTextOutput
15
16
17 class EapiCommandError(RuntimeError):
18 """Exception class for eAPI command errors.
19
20 Attributes
21 ----------
22 failed: the failed command
23 errmsg: a description of the failure reason
24 errors: the command failure details
25 passed: a list of command results of the commands that passed
26 not_exec: a list of commands that were not executed
27 """
28
29 def __init__(
30 self,
31 failed: str,
32 errors: list[str],
33 errmsg: str,
34 passed: list[EapiJsonOutput] | list[EapiTextOutput],
35 not_exec: list[EapiSimpleCommand | EapiComplexCommand],
36 ) -> None:
37 """Initialize for the EapiCommandError exception."""
38 self.failed = failed
39 self.errmsg = errmsg
40 self.errors = errors
41 self.passed = passed
42 self.not_exec = not_exec
43 super().__init__()
44
45 def __str__(self) -> str:
46 """Return the error message associated with the exception."""
47 return self.errmsg
48
49
50 # alias for exception during sending-receiving
51 EapiTransportError = httpx.HTTPStatusError
52
53
54 class EapiAuthenticationError(RuntimeError):
55 """Exception raised by session auth when the device returns HTTP 401 — either on login or on a command request."""
56
57 _SESSION_EXPIRED_MSG = "Session cookie expired. Consider increasing 'session timeout' under 'management api http-commands' on the device."
58
59 def __init__(self, host: str, response_text: str | None = None, *, session_expired: bool = False) -> None:
60 msg = self._SESSION_EXPIRED_MSG if session_expired else response_text or f"Authentication failed for {host!r} (HTTP 401)."
61 super().__init__(msg)
62 self.host = host
63 self.response_text = response_text
64 self.session_expired = session_expired
65
66
67 class EapiAsyncOnlyError(RuntimeError):
68 """Raised when EapiSessionAuth is used with a synchronous httpx client."""
69
70 def __init__(self) -> None:
71 super().__init__("EapiSessionAuth requires an async httpx client.")