Skip to content

Commit

Permalink
bugfix: REST connector httperror for different Mambu error responses.
Browse files Browse the repository at this point in the history
  • Loading branch information
jstitch committed Apr 17, 2024
1 parent 15a9a34 commit 558484b
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 9 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion MambuPy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
16 changes: 11 additions & 5 deletions MambuPy/api/connector/rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
25 changes: 25 additions & 0 deletions tests/api/connector/unit_rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 1 addition & 3 deletions tests/api/unit_mambuuser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"])

Expand Down

0 comments on commit 558484b

Please sign in to comment.