1
0
mirror of https://github.com/checktheroads/hyperglass synced 2024-05-11 05:55:08 +00:00

171 lines
4.5 KiB
Python
Raw Normal View History

"""Custom exceptions for hyperglass."""
2019-12-30 09:47:31 -07:00
# Standard Library Imports
import json as _json
2019-12-30 09:47:31 -07:00
# Project Imports
from hyperglass.util import log
class HyperglassError(Exception):
"""hyperglass base exception."""
def __init__(self, message="", alert="warning", keywords=None):
"""Initialize the hyperglass base exception class.
Keyword Arguments:
message {str} -- Error message (default: {""})
alert {str} -- Error severity (default: {"warning"})
keywords {list} -- 'Important' keywords (default: {None})
"""
2019-12-30 01:44:19 -07:00
self._message = message
self._alert = alert
self._keywords = keywords or []
if self._alert == "warning":
log.error(repr(self))
2019-12-30 01:44:19 -07:00
elif self._alert == "danger":
log.critical(repr(self))
else:
log.info(repr(self))
2019-08-31 23:50:02 -07:00
2019-09-03 00:42:45 -07:00
def __str__(self):
"""Return the instance's error message.
Returns:
{str} -- Error Message
"""
2019-12-30 01:44:19 -07:00
return self._message
2019-09-03 00:42:45 -07:00
def __repr__(self):
"""Return the instance's severity & error message in a string.
Returns:
{str} -- Error message with code
"""
2019-12-30 01:44:19 -07:00
return f"[{self.alert.upper()}] {self._message}"
2019-08-31 23:50:02 -07:00
def __dict__(self):
"""Return the instance's attributes as a dictionary.
Returns:
{dict} -- Exception attributes in dict
"""
2019-12-30 01:44:19 -07:00
return {
"message": self._message,
"alert": self._alert,
"keywords": self._keywords,
}
def json(self):
"""Return the instance's attributes as a JSON object.
Returns:
{str} -- Exception attributes as JSON
"""
return _json.dumps(self.__dict__())
@property
def message(self):
"""Return the instance's `message` attribute.
Returns:
{str} -- Error Message
"""
2019-12-30 01:44:19 -07:00
return self._message
@property
def alert(self):
"""Return the instance's `alert` attribute.
Returns:
{str} -- Alert name
"""
2019-12-30 01:44:19 -07:00
return self._alert
@property
def keywords(self):
"""Return the instance's `keywords` attribute.
Returns:
{list} -- Keywords List
"""
2019-12-30 01:44:19 -07:00
return self._keywords
class _UnformattedHyperglassError(HyperglassError):
"""Base exception class for freeform error messages."""
2019-12-30 01:44:19 -07:00
def __init__(self, unformatted_msg="", alert="warning", **kwargs):
"""Format error message with keyword arguments.
Keyword Arguments:
message {str} -- Error message (default: {""})
alert {str} -- Error severity (default: {"warning"})
keywords {list} -- 'Important' keywords (default: {None})
"""
2019-12-30 01:44:19 -07:00
self._message = unformatted_msg.format(**kwargs)
self._alert = alert
self._keywords = list(kwargs.values())
super().__init__(
message=self._message, alert=self._alert, keywords=self._keywords
)
class ConfigError(_UnformattedHyperglassError):
"""Raised for generic user-config issues."""
class ConfigInvalid(_UnformattedHyperglassError):
"""Raised when a config item fails type or option validation."""
2019-12-30 01:44:19 -07:00
_message = 'The value field "{field}" is invalid: {error_msg}'
class ConfigMissing(_UnformattedHyperglassError):
"""Raised when a required config file or item is missing or undefined."""
2019-12-30 01:44:19 -07:00
_message = (
"{missing_item} is missing or undefined and is required to start "
"hyperglass. Please consult the installation documentation."
)
2019-08-29 07:10:40 -07:00
class ScrapeError(_UnformattedHyperglassError):
"""Raised when a scrape/netmiko error occurs."""
2019-12-30 01:44:19 -07:00
_alert = "danger"
class AuthError(_UnformattedHyperglassError):
"""Raised when authentication to a device fails."""
2019-12-30 01:44:19 -07:00
_alert = "danger"
class RestError(_UnformattedHyperglassError):
"""Raised upon a rest API client error."""
2019-12-30 01:44:19 -07:00
_alert = "danger"
2019-09-03 00:42:45 -07:00
class DeviceTimeout(_UnformattedHyperglassError):
2019-09-03 00:42:45 -07:00
"""Raised when the connection to a device times out."""
2019-12-30 01:44:19 -07:00
_alert = "danger"
class InputInvalid(_UnformattedHyperglassError):
"""Raised when input validation fails."""
class InputNotAllowed(_UnformattedHyperglassError):
"""Raised when input validation fails due to a configured check."""
class ResponseEmpty(_UnformattedHyperglassError):
"""Raised when hyperglass can connect to the device but the response is empty."""
class UnsupportedDevice(_UnformattedHyperglassError):
"""Raised when an input NOS is not in the supported NOS list."""