From 9f9df79ab87b7a5d2356df5d9776c3a9e03c80e7 Mon Sep 17 00:00:00 2001 From: clerie Date: Thu, 6 Jan 2022 17:28:19 +0100 Subject: [PATCH] Introduce exception handling --- docs/api/mitel_ommclient2.rst | 8 ++ mitel_ommclient2/exceptions.py | 156 ++++++++++++++++++++++++++ mitel_ommclient2/messages/__init__.py | 19 ++++ 3 files changed, 183 insertions(+) create mode 100644 mitel_ommclient2/exceptions.py diff --git a/docs/api/mitel_ommclient2.rst b/docs/api/mitel_ommclient2.rst index cc0189a..1833985 100644 --- a/docs/api/mitel_ommclient2.rst +++ b/docs/api/mitel_ommclient2.rst @@ -28,6 +28,14 @@ mitel\_ommclient2.connection module :undoc-members: :show-inheritance: +mitel\_ommclient2.exceptions module +----------------------------------- + +.. automodule:: mitel_ommclient2.exceptions + :members: + :undoc-members: + :show-inheritance: + mitel\_ommclient2.session module -------------------------------- diff --git a/mitel_ommclient2/exceptions.py b/mitel_ommclient2/exceptions.py new file mode 100644 index 0000000..b95f570 --- /dev/null +++ b/mitel_ommclient2/exceptions.py @@ -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 diff --git a/mitel_ommclient2/messages/__init__.py b/mitel_ommclient2/messages/__init__.py index d694fca..b2e9a2d 100644 --- a/mitel_ommclient2/messages/__init__.py +++ b/mitel_ommclient2/messages/__init__.py @@ -2,6 +2,8 @@ from xml.dom.minidom import getDOMImplementation, parseString +from ..exceptions import exception_classes, OMResponseException + class Request: """ @@ -58,6 +60,23 @@ class Response: self.attrs = attrs 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 def seq(self): return self.attrs.get("seq")