From d34e2cea6bbb6976efd718afd3083df18b352f63 Mon Sep 17 00:00:00 2001 From: checktheroads Date: Tue, 3 Sep 2019 00:42:45 -0700 Subject: [PATCH] improve error handling --- hyperglass/exceptions.py | 66 +++++++++++++++------------------------- 1 file changed, 24 insertions(+), 42 deletions(-) diff --git a/hyperglass/exceptions.py b/hyperglass/exceptions.py index 4eba6df..6c6fb65 100644 --- a/hyperglass/exceptions.py +++ b/hyperglass/exceptions.py @@ -6,15 +6,16 @@ from hyperglass.constants import code class HyperglassError(Exception): - """ - hyperglass base exception - """ + """hyperglass base exception""" def __init__(self, message="", status=500, keywords={}): self.message = message self.status = status self.keywords = keywords + def __str__(self): + return self.message + def __dict__(self): return { "message": self.message, @@ -24,18 +25,13 @@ class HyperglassError(Exception): class ConfigError(HyperglassError): - """ - Raised for generic user-config issues. - """ + """Raised for generic user-config issues.""" - def __init__(self, unformatted_msg, kwargs={}): + def __init__(self, unformatted_msg, **kwargs): self.message = unformatted_msg.format(**kwargs) self.keywords = [value for value in kwargs.values()] super().__init__(message=self.message, keywords=self.keywords) - def __str__(self): - return self.message - class ConfigInvalid(HyperglassError): """Raised when a config item fails type or option validation""" @@ -47,16 +43,13 @@ class ConfigInvalid(HyperglassError): self.keywords = [value for value in kwargs.values()] super().__init__(message=self.message, keywords=self.keywords) - def __str__(self): - return self.message - class ConfigMissing(HyperglassError): """ - Raised when a required config file or item is missing or undefined + Raised when a required config file or item is missing or undefined. """ - def __init__(self, kwargs={}): + def __init__(self, **kwargs): self.message = ( "{missing_item} is missing or undefined and is required to start " "hyperglass. Please consult the installation documentation." @@ -64,14 +57,11 @@ class ConfigMissing(HyperglassError): self.keywords = [value for value in kwargs.values()] super().__init__(message=self.message, keywords=self.keywords) - def __str__(self): - return self.message - class ScrapeError(HyperglassError): """Raised upon a scrape/netmiko error""" - def __init__(self, msg, kwargs={}): + def __init__(self, msg, **kwargs): self.message = msg.format(**kwargs) self.status = code.target_error self.keywords = [value for value in kwargs.values()] @@ -79,14 +69,11 @@ class ScrapeError(HyperglassError): message=self.message, status=self.status, keywords=self.keywords ) - def __str__(self): - return self.message - class AuthError(HyperglassError): """Raised when authentication to a device fails""" - def __init__(self, msg, kwargs={}): + def __init__(self, msg, **kwargs): self.message = msg.format(**kwargs) self.status = code.target_error self.keywords = [value for value in kwargs.values()] @@ -94,14 +81,11 @@ class AuthError(HyperglassError): message=self.message, status=self.status, keywords=self.keywords ) - def __str__(self): - return self.message - class RestError(HyperglassError): """Raised upon a rest API client error""" - def __init__(self, msg, kwargs={}): + def __init__(self, msg, **kwargs): self.message = msg.format(**kwargs) self.status = code.target_error self.keywords = [value for value in kwargs.values()] @@ -109,9 +93,6 @@ class RestError(HyperglassError): message=self.message, status=self.status, keywords=self.keywords ) - def __str__(self): - return self.message - class InputInvalid(HyperglassError): """Raised when input validation fails""" @@ -124,9 +105,6 @@ class InputInvalid(HyperglassError): message=self.message, status=self.status, keywords=self.keywords ) - def __str__(self): - return self.message - class InputNotAllowed(HyperglassError): """ @@ -142,16 +120,11 @@ class InputNotAllowed(HyperglassError): message=self.message, status=self.status, keywords=self.keywords ) - def __str__(self): - return self.message - class UnsupportedDevice(HyperglassError): - """ - Raised when an input NOS is not in the supported NOS list. - """ + """Raised when an input NOS is not in the supported NOS list.""" - def __init__(self, kwargs={}): + def __init__(self, **kwargs): self.message = "".format(**kwargs) self.status = code.target_error self.keywords = [value for value in kwargs.values()] @@ -159,5 +132,14 @@ class UnsupportedDevice(HyperglassError): message=self.message, status=self.status, keywords=self.keywords ) - def __str__(self): - return self.message + +class DeviceTimeout(HyperglassError): + """Raised when the connection to a device times out.""" + + def __init__(self, msg, **kwargs): + self.message = msg.format(**kwargs) + self.status = code.target_error + self.keywords = [value for value in kwargs.values()] + super().__init__( + message=self.message, status=self.status, keywords=self.keywords + )