Introduce exception handling

This commit is contained in:
clerie 2022-01-06 17:28:19 +01:00
parent 72f148f7b9
commit 9f9df79ab8
3 changed files with 183 additions and 0 deletions

View File

@ -28,6 +28,14 @@ mitel\_ommclient2.connection module
:undoc-members: :undoc-members:
:show-inheritance: :show-inheritance:
mitel\_ommclient2.exceptions module
-----------------------------------
.. automodule:: mitel_ommclient2.exceptions
:members:
:undoc-members:
:show-inheritance:
mitel\_ommclient2.session module mitel\_ommclient2.session module
-------------------------------- --------------------------------

View File

@ -0,0 +1,156 @@
#!/usr/bin/env python3
exception_classes = {}
def _collect_exception_class(c):
"""
Decorator that collects exception classes for parsing error codes.
"""
exception_classes[c.__name__] = c
return c
class OMResponseException(Exception):
def __init__(self, response, msg=None):
self.response = response
if msg is None:
msg = self.response.info
super().__init__(msg)
@_collect_exception_class
class EAreaFull(OMResponseException):
pass
@_collect_exception_class
class EAuth(OMResponseException):
pass
@_collect_exception_class
class EDectRegDomainInvalid(OMResponseException):
pass
@_collect_exception_class
class EEncryptNotAllowed(OMResponseException):
pass
@_collect_exception_class
class EExist(OMResponseException):
pass
@_collect_exception_class
class EFailed(OMResponseException):
pass
@_collect_exception_class
class EForbidden(OMResponseException):
pass
@_collect_exception_class
class EInProgress(OMResponseException):
pass
@_collect_exception_class
class EInval(OMResponseException):
def __init__(self, response):
super().__init__(response, response.bad)
@_collect_exception_class
class EInvalidChars(OMResponseException):
pass
@_collect_exception_class
class ELicense(OMResponseException):
pass
@_collect_exception_class
class ELicenseFile(OMResponseException):
pass
@_collect_exception_class
class ELicenseWrongInstallId(OMResponseException):
pass
@_collect_exception_class
class EMissing(OMResponseException):
def __init__(self, response):
super().__init__(response, response.bad)
@_collect_exception_class
class ENoEnt(OMResponseException):
pass
@_collect_exception_class
class ENoMem(OMResponseException):
pass
@_collect_exception_class
class EPerm(OMResponseException):
pass
@_collect_exception_class
class EPwEmpty(OMResponseException):
pass
@_collect_exception_class
class EPwSimilarToHost(OMResponseException):
pass
@_collect_exception_class
class EPwSimilarToName(OMResponseException):
pass
@_collect_exception_class
class EPwTooManySimilarChars(OMResponseException):
pass
@_collect_exception_class
class EPwTooShort(OMResponseException):
pass
@_collect_exception_class
class EPwTooSimilar(OMResponseException):
pass
@_collect_exception_class
class EPwTooWeak(OMResponseException):
pass
@_collect_exception_class
class EPwUnchanged(OMResponseException):
pass
@_collect_exception_class
class ETooLong(OMResponseException):
def __init__(self, response):
super().__init__(response, response.bad + ", maximum of " + str(response.maxLen))
@_collect_exception_class
class EWlanRegDomainInvalid(OMResponseException):
pass

View File

@ -2,6 +2,8 @@
from xml.dom.minidom import getDOMImplementation, parseString from xml.dom.minidom import getDOMImplementation, parseString
from ..exceptions import exception_classes, OMResponseException
class Request: class Request:
""" """
@ -58,6 +60,23 @@ class Response:
self.attrs = attrs self.attrs = attrs
self.childs = childs self.childs = childs
def raise_on_error(self):
"""
Raises an exception if the response contains an error.
Usage::
>>> try:
>>> r.raise_on_error()
>>> except mitel_ommclient2.exceptions.EAuth as e:
>>> print("We don't care about authentication!")
See children of :class:`mitel_ommclient2.exceptions.OMResponseException` for all possible exceptions.
"""
if self.errCode is not None:
raise exception_classes.get(self.errCode, OMResponseException)()
@property @property
def seq(self): def seq(self):
return self.attrs.get("seq") return self.attrs.get("seq")