Debian Codegraph @

neutron_lib/callbacks/exceptions.py

raw
1 # Licensed under the Apache License, Version 2.0 (the "License"); you may
2 # not use this file except in compliance with the License. You may obtain
3 # a copy of the License at
4 #
5 # http://www.apache.org/licenses/LICENSE-2.0
6 #
7 # Unless required by applicable law or agreed to in writing, software
8 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10 # License for the specific language governing permissions and limitations
11 # under the License.
12
13 from neutron_lib._i18n import _
14 from neutron_lib import exceptions
15
16
17 class Invalid(exceptions.NeutronException):
18 message = _("The value '%(value)s' for %(element)s is not valid.")
19
20
21 class CallbackFailure(exceptions.MultipleExceptions):
22
23 def __init__(self, errors):
24 self.errors = errors
25
26 def __str__(self):
27 if isinstance(self.errors, list):
28 return ','.join(str(error) for error in self.errors)
29 else:
30 return str(self.errors)
31
32 @property
33 def inner_exceptions(self):
34 """The list of unpacked errors for this exception.
35
36 :return: A list of unpacked errors for this exception. An unpacked
37 error is the Exception's 'error' attribute if it inherits from
38 NotificationError, otherwise it's the exception itself.
39 """
40 if isinstance(self.errors, list):
41 return [self._unpack_if_notification_error(e) for e in self.errors]
42 return [self._unpack_if_notification_error(self.errors)]
43
44 @staticmethod
45 def _unpack_if_notification_error(exc):
46 if isinstance(exc, NotificationError):
47 return exc.error
48 return exc
49
50
51 class NotificationError:
52
53 def __init__(self, callback_id, error, cancellable=False):
54 self.callback_id = callback_id
55 self.error = error
56 self._cancellable = cancellable
57
58 def __str__(self):
59 return f'Callback {self.callback_id} failed with "{self.error}"'
60
61 @property
62 def is_cancellable(self):
63 return self._cancellable