From 558484bf8a161e007070cc36331f301ef4d1aa8d Mon Sep 17 00:00:00 2001 From: Javier Novoa Date: Tue, 16 Apr 2024 23:24:51 -0600 Subject: [PATCH] bugfix: REST connector httperror for different Mambu error responses. --- CHANGELOG | 3 +++ MambuPy/__init__.py | 2 +- MambuPy/api/connector/rest.py | 16 +++++++++++----- tests/api/connector/unit_rest.py | 25 +++++++++++++++++++++++++ tests/api/unit_mambuuser.py | 4 +--- 5 files changed, 41 insertions(+), 9 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 4bfdbdb..71f5781 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -233,3 +233,6 @@ Release Notes - ORM support with v2.0.0b27 features included in REST API * v2.0.0b30 - MambuCentre v2 assignedBranch field allows get_assignedBranch() method +* v2.0.0b32 + - REST connector httperror raises MambuError, different Mambu error responses. + - v2 code black linting diff --git a/MambuPy/__init__.py b/MambuPy/__init__.py index d310b32..d53bc86 100644 --- a/MambuPy/__init__.py +++ b/MambuPy/__init__.py @@ -35,5 +35,5 @@ to achive TDD when implementing features or correcting bugs. """ -__version__ = "1.10.2" +__version__ = "2.0.0b32" """The version of this module.""" diff --git a/MambuPy/api/connector/rest.py b/MambuPy/api/connector/rest.py index 028df56..ce22d91 100644 --- a/MambuPy/api/connector/rest.py +++ b/MambuPy/api/connector/rest.py @@ -159,14 +159,20 @@ def __request(self, method, url, params=None, data=None, content_type=None): }, ] } + try: + error = content["errors"][0] + except KeyError: + error = content raise MambuError( "{} ({}) - {}{}".format( - content["errors"][0]["errorCode"], + error["errorCode"] if "errorCode" in error else error["returnCode"], resp.status_code, - content["errors"][0]["errorReason"], - " (" + content["errors"][0]["errorSource"] + ")" - if "errorSource" in content["errors"][0] - else "", + ( + error["errorReason"] + if "errorReason" in error + else error["returnStatus"] + ), + " (" + error["errorSource"] + ")" if "errorSource" in error else "", ) ) except requests.exceptions.RetryError as rerr: # pragma: no cover diff --git a/tests/api/connector/unit_rest.py b/tests/api/connector/unit_rest.py index 79d76d2..fb1f54e 100644 --- a/tests/api/connector/unit_rest.py +++ b/tests/api/connector/unit_rest.py @@ -152,6 +152,31 @@ def test_mambu___request_400(self, mock_request_session): ): mcrest.__request("GET", "someURL") + @mock.patch("MambuPy.api.connector.rest.requests.Session") + def test_mambu___request_404(self, mock_request_session): + mock_request_session().request().status_code = 404 + mock_request_session().request().content = b""" +{"returnCode": "66", + "returnStatus": "Kill the Jedi"} +""" + mock_request_session().request().raise_for_status.side_effect = \ + requests.exceptions.HTTPError("404 not found") + + mcrest = rest.MambuConnectorREST() + with self.assertRaisesRegex(MambuError, r"66 \(404\) - Kill the Jedi"): + mcrest.__request("GET", "someURL") + + mock_request_session().request().content = b""" +{"returnCode": "66", + "returnStatus": "Kill the Jedi", + "errorSource": "Palpatine"} +""" + mcrest = rest.MambuConnectorREST() + with self.assertRaisesRegex( + MambuError, r"^66 \(404\) - Kill the Jedi \(Palpatine\)$" + ): + mcrest.__request("GET", "someURL") + @mock.patch("MambuPy.api.connector.rest.requests") def test_mambu___request_retries(self, mock_requests): # everything OK! no retries done diff --git a/tests/api/unit_mambuuser.py b/tests/api/unit_mambuuser.py index 2b5c20d..bed003b 100644 --- a/tests/api/unit_mambuuser.py +++ b/tests/api/unit_mambuuser.py @@ -55,9 +55,7 @@ def test___getattribute__(self, mock_getEntities): self.assertEqual(inspect.isfunction(mu.get_role), True) self.assertEqual( inspect.getsource(mu.get_role).strip(), - """return lambda **kwargs: self.getEntities( - entities=["role"], - **kwargs)[0]""") + """return lambda **kwargs: self.getEntities(entities=["role"], **kwargs)[0]""") mu.get_role() mock_getEntities.assert_called_with(entities=["role"])