1
0
mirror of https://github.com/checktheroads/hyperglass synced 2024-05-11 05:55:08 +00:00
Files
checktheroads-hyperglass/hyperglass/exceptions/public.py
2021-09-15 18:25:37 -07:00

163 lines
5.1 KiB
Python

"""User-facing/Public exceptions."""
# Standard Library
from typing import TYPE_CHECKING, Any, Dict, Optional
# Project
from hyperglass.state import use_state
# Local
from ._common import PublicHyperglassError
if TYPE_CHECKING:
# Project
from hyperglass.models.api.query import Query
from hyperglass.models.config.devices import Device
_state = use_state()
MESSAGES = _state.params.messages
TEXT = _state.params.web.text
class ScrapeError(
PublicHyperglassError, template=MESSAGES.connection_error, level="danger",
):
"""Raised when an SSH driver error occurs."""
def __init__(self, *, error: BaseException, device: "Device"):
"""Initialize parent error."""
super().__init__(error=str(error), device=device.name, proxy=device.proxy)
class AuthError(PublicHyperglassError, template=MESSAGES.authentication_error, level="danger"):
"""Raised when authentication to a device fails."""
def __init__(self, *, error: BaseException, device: "Device"):
"""Initialize parent error."""
super().__init__(error=str(error), device=device.name, proxy=device.proxy)
class RestError(PublicHyperglassError, template=MESSAGES.connection_error, level="danger"):
"""Raised upon a rest API client error."""
def __init__(self, *, error: BaseException, device: "Device"):
"""Initialize parent error."""
super().__init__(error=str(error), device=device.name)
class DeviceTimeout(PublicHyperglassError, template=MESSAGES.request_timeout, level="danger"):
"""Raised when the connection to a device times out."""
def __init__(self, *, error: BaseException, device: "Device"):
"""Initialize parent error."""
super().__init__(error=str(error), device=device.name, proxy=device.proxy)
class InvalidQuery(PublicHyperglassError, template=MESSAGES.invalid_query):
"""Raised when input validation fails."""
def __init__(
self, *, error: Optional[str] = None, query: "Query", **kwargs: Dict[str, Any]
) -> None:
"""Initialize parent error."""
kwargs = {
"query_type": query.query_type,
"target": query.query_target,
**kwargs,
}
if error is not None:
self.handle_error(error)
kwargs["error"] = str(error)
super().__init__(**kwargs)
class NotFound(PublicHyperglassError, template=MESSAGES.not_found):
"""Raised when an object is not found."""
def __init__(self, type: str, name: str, **kwargs: Dict[str, str]) -> None:
"""Initialize parent error."""
super().__init__(type=type, name=name, **kwargs)
class QueryLocationNotFound(NotFound):
"""Raised when a query location is not found."""
def __init__(self, location: Any, **kwargs: Dict[str, Any]) -> None:
"""Initialize a NotFound error for a query location."""
super().__init__(type=TEXT.query_location, name=str(location), **kwargs)
class QueryTypeNotFound(NotFound):
"""Raised when a query type is not found."""
def __init__(self, query_type: Any, **kwargs: Dict[str, Any]) -> None:
"""Initialize a NotFound error for a query type."""
super().__init__(type=TEXT.query_type, name=str(query_type), **kwargs)
class QueryGroupNotFound(NotFound):
"""Raised when a query group is not found."""
def __init__(self, group: Any, **kwargs: Dict[str, Any]) -> None:
"""Initialize a NotFound error for a query group."""
super().__init__(type=TEXT.query_group, name=str(group), **kwargs)
class InputInvalid(PublicHyperglassError, template=MESSAGES.invalid_input):
"""Raised when input validation fails."""
def __init__(
self, *, error: Optional[Any] = None, target: str, **kwargs: Dict[str, Any]
) -> None:
"""Initialize parent error."""
kwargs = {"target": target, **kwargs}
if error is not None:
self.handle_error(error)
kwargs["error"] = str(error)
super().__init__(**kwargs)
class InputNotAllowed(PublicHyperglassError, template=MESSAGES.acl_not_allowed):
"""Raised when input validation fails due to a configured check."""
def __init__(
self, *, error: Optional[str] = None, query: "Query", **kwargs: Dict[str, Any]
) -> None:
"""Initialize parent error."""
kwargs = {
"query_type": query.query_type,
"target": query.query_target,
**kwargs,
}
if error is not None:
self.handle_error(error)
kwargs["error"] = str(error)
super().__init__(**kwargs)
class ResponseEmpty(PublicHyperglassError, template=MESSAGES.no_output):
"""Raised when hyperglass can connect to the device but the response is empty."""
def __init__(
self, *, error: Optional[str] = None, query: "Query", **kwargs: Dict[str, Any]
) -> None:
"""Initialize parent error."""
kwargs = {
"query_type": query.query_type,
"target": query.query_target,
**kwargs,
}
if error is not None:
self.handle_error(error)
kwargs["error"] = str(error)
super().__init__(**kwargs)