diff options
Diffstat (limited to 'oslo_vmware/exceptions.py')
-rw-r--r-- | oslo_vmware/exceptions.py | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/oslo_vmware/exceptions.py b/oslo_vmware/exceptions.py index 4018a82..df6d676 100644 --- a/oslo_vmware/exceptions.py +++ b/oslo_vmware/exceptions.py @@ -296,6 +296,32 @@ def get_fault_class(name): return fault_class +def translate_fault(localized_method_fault, excep_msg=None): + """Produce proper VimException subclass object, + + The exception is based on a vmodl.LocalizedMethodFault. + + :param excep_msg: Message to set to the exception. Defaults to + localizedMessage of the fault. + """ + try: + if not excep_msg: + excep_msg = six.text_type(localized_method_fault.localizedMessage) + name = localized_method_fault.fault.__class__.__name__ + fault_class = get_fault_class(name) + if fault_class: + ex = fault_class(excep_msg) + else: + ex = VimFaultException([name], excep_msg) + except Exception as e: + LOG.debug("Unexpected exception thrown (%s) while translating" + " fault (%s) with message: %s.", + e, localized_method_fault, excep_msg) + ex = VimException(message=excep_msg, cause=e) + + return ex + + def register_fault_class(name, exception): fault_class = _fault_classes_registry.get(name) if not issubclass(exception, VimException): |