From af637d63d67d30900283c74b027e8d6446652eb4 Mon Sep 17 00:00:00 2001 From: Goutham Pratapa Date: Sun, 22 Aug 2021 18:05:33 +0530 Subject: [PATCH 1/2] [Draft] Add exception handler for better exp --- authenticator/exceptions.py | 64 +++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 authenticator/exceptions.py diff --git a/authenticator/exceptions.py b/authenticator/exceptions.py new file mode 100644 index 0000000..45c5e44 --- /dev/null +++ b/authenticator/exceptions.py @@ -0,0 +1,64 @@ +import Exception +import oauthlib + +from oslo_log import log as logging + +oauthlib.oauth2.rfc6749.errors.InvalidGrantError + + +class OauthException(Exception): + """ + Base Oauth Exception class + To correctly use this class, inherit from it and define + a 'msg_fmt' property. That msg_fmt will get printf'd + with the keyword arguments provided to the constructor. + """ + + msg_fmt = _("An unknown exception occurred.") + code = 500 + headers = {} + safe = False + + def __init__(self, message=None, **kwargs): + self.kwargs = kwargs + + if "code" not in self.kwargs: + try: + self.kwargs["code"] = self.code + except AttributeError: + pass + + try: + if not message: + message = self.msg_fmt % kwargs + else: + message = str(message) + except Exception: + # NOTE(melwitt): This is done in a separate method so it can be + # monkey-patched during testing to make it a hard failure. + self._log_exception() + message = self.msg_fmt + + self.message = message + super(NovaException, self).__init__(message) + + def _log_exception(self): + # kwargs doesn't match a variable in the message + # log the issue and the kwargs + LOG.exception("Exception in string format operation") + for name, value in self.kwargs.items(): + LOG.error("%s: %s" % (name, value)) # noqa + + def format_message(self): + # NOTE(mrodden): use the first argument to the python Exception object + # which should be our full NovaException message, (see __init__) + return self.args[0] + + def __repr__(self): + dict_repr = self.__dict__ + dict_repr["class"] = self.__class__.__name__ + return str(dict_repr) + + +class EncryptionFailure(NovaException): + msg_fmt = _("Failed to encrypt text: %(reason)s") From 51a0c0eeb06a653bfe439e9ecdb2f885b65620eb Mon Sep 17 00:00:00 2001 From: Goutham Pratapa Date: Sun, 22 Aug 2021 18:49:41 +0530 Subject: [PATCH 2/2] Update exceptions.py --- authenticator/exceptions.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/authenticator/exceptions.py b/authenticator/exceptions.py index 45c5e44..6469b33 100644 --- a/authenticator/exceptions.py +++ b/authenticator/exceptions.py @@ -3,8 +3,6 @@ from oslo_log import log as logging -oauthlib.oauth2.rfc6749.errors.InvalidGrantError - class OauthException(Exception): """