Skip to content

Commit af637d6

Browse files
[Draft] Add exception handler for better exp
1 parent d714121 commit af637d6

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed

authenticator/exceptions.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import Exception
2+
import oauthlib
3+
4+
from oslo_log import log as logging
5+
6+
oauthlib.oauth2.rfc6749.errors.InvalidGrantError
7+
8+
9+
class OauthException(Exception):
10+
"""
11+
Base Oauth Exception class
12+
To correctly use this class, inherit from it and define
13+
a 'msg_fmt' property. That msg_fmt will get printf'd
14+
with the keyword arguments provided to the constructor.
15+
"""
16+
17+
msg_fmt = _("An unknown exception occurred.")
18+
code = 500
19+
headers = {}
20+
safe = False
21+
22+
def __init__(self, message=None, **kwargs):
23+
self.kwargs = kwargs
24+
25+
if "code" not in self.kwargs:
26+
try:
27+
self.kwargs["code"] = self.code
28+
except AttributeError:
29+
pass
30+
31+
try:
32+
if not message:
33+
message = self.msg_fmt % kwargs
34+
else:
35+
message = str(message)
36+
except Exception:
37+
# NOTE(melwitt): This is done in a separate method so it can be
38+
# monkey-patched during testing to make it a hard failure.
39+
self._log_exception()
40+
message = self.msg_fmt
41+
42+
self.message = message
43+
super(NovaException, self).__init__(message)
44+
45+
def _log_exception(self):
46+
# kwargs doesn't match a variable in the message
47+
# log the issue and the kwargs
48+
LOG.exception("Exception in string format operation")
49+
for name, value in self.kwargs.items():
50+
LOG.error("%s: %s" % (name, value)) # noqa
51+
52+
def format_message(self):
53+
# NOTE(mrodden): use the first argument to the python Exception object
54+
# which should be our full NovaException message, (see __init__)
55+
return self.args[0]
56+
57+
def __repr__(self):
58+
dict_repr = self.__dict__
59+
dict_repr["class"] = self.__class__.__name__
60+
return str(dict_repr)
61+
62+
63+
class EncryptionFailure(NovaException):
64+
msg_fmt = _("Failed to encrypt text: %(reason)s")

0 commit comments

Comments
 (0)