From 80e72f089d3b138f9e41ec1a98de8735413d6042 Mon Sep 17 00:00:00 2001 From: Sergi Delgado Segura Date: Thu, 9 Apr 2020 17:49:46 +0200 Subject: [PATCH] Splits exceptions in common + client --- cli/exceptions.py | 23 +++++--------------- common/exceptions.py | 37 +++++++++++++++++++++++++++++++++ watchtower-plugin/exceptions.py | 30 +------------------------- watchtower-plugin/keys.py | 2 +- 4 files changed, 44 insertions(+), 48 deletions(-) create mode 100644 common/exceptions.py diff --git a/cli/exceptions.py b/cli/exceptions.py index 7498ad9..fd0d395 100644 --- a/cli/exceptions.py +++ b/cli/exceptions.py @@ -1,22 +1,9 @@ -class InvalidParameter(ValueError): - """Raised when a command line parameter is invalid (either missing or wrong)""" - - def __init__(self, msg, **kwargs): - self.reason = msg - self.kwargs = kwargs +from common.exceptions import BasicException -class InvalidKey(Exception): - """Raised when there is an error loading the keys""" - - def __init__(self, msg, **kwargs): - self.reason = msg - self.kwargs = kwargs - - -class TowerResponseError(Exception): +class TowerConnectionError(BasicException): """Raised when the tower responds with an error""" - def __init__(self, msg, **kwargs): - self.reason = msg - self.kwargs = kwargs + +class TowerResponseError(BasicException): + """Raised when the tower responds with an error""" diff --git a/common/exceptions.py b/common/exceptions.py new file mode 100644 index 0000000..2dbf076 --- /dev/null +++ b/common/exceptions.py @@ -0,0 +1,37 @@ +class BasicException(Exception): + def __init__(self, msg, **kwargs): + self.msg = msg + self.kwargs = kwargs + + def __str__(self): + if len(self.kwargs) > 2: + params = "".join("{}={}, ".format(k, v) for k, v in self.kwargs.items()) + + # Remove the extra 2 characters (space and comma) and add all data to the final message. + message = self.msg + " ({})".format(params[:-2]) + + else: + message = self.msg + + return message + + def to_json(self): + response = {"error": self.msg} + response.update(self.kwargs) + return response + + +class InvalidParameter(BasicException): + """Raised when a command line parameter is invalid (either missing or wrong)""" + + +class InvalidKey(BasicException): + """Raised when there is an error loading the keys""" + + +class EncryptionError(BasicException): + """Raised when there is an error with encryption related functions, covers decryption""" + + +class SignatureError(BasicException): + """Raised when there is an with the signature related functions, covers EC recover""" diff --git a/watchtower-plugin/exceptions.py b/watchtower-plugin/exceptions.py index 48cca04..fd0d395 100644 --- a/watchtower-plugin/exceptions.py +++ b/watchtower-plugin/exceptions.py @@ -1,32 +1,4 @@ -class BasicException(Exception): - def __init__(self, msg, **kwargs): - self.msg = msg - self.kwargs = kwargs - - def __str__(self): - if len(self.kwargs) > 2: - params = "".join("{}={}, ".format(k, v) for k, v in self.kwargs.items()) - - # Remove the extra 2 characters (space and comma) and add all data to the final message. - message = self.msg + " ({})".format(params[:-2]) - - else: - message = self.msg - - return message - - def to_json(self): - response = {"error": self.msg} - response.update(self.kwargs) - return response - - -class InvalidParameter(BasicException): - """Raised when a command line parameter is invalid (either missing or wrong)""" - - -class InvalidKey(BasicException): - """Raised when there is an error loading the keys""" +from common.exceptions import BasicException class TowerConnectionError(BasicException): diff --git a/watchtower-plugin/keys.py b/watchtower-plugin/keys.py index 17d374c..b42f98d 100644 --- a/watchtower-plugin/keys.py +++ b/watchtower-plugin/keys.py @@ -5,7 +5,7 @@ from cryptography.hazmat.backends import default_backend from cryptography.hazmat.primitives import serialization from cryptography.hazmat.primitives.asymmetric import ec -from exceptions import InvalidKey +from common.exceptions import InvalidKey from common.cryptographer import Cryptographer