From 650c9e5853ea57b985df1e9d97288a7d879a2e4f Mon Sep 17 00:00:00 2001 From: Scott Bahling Date: Thu, 13 Feb 2020 11:43:10 +0100 Subject: Implement general exception handling We trap the low level OBS api calls via a callback and check for success. If error returned, parse the OBS error response and raise an exception. --- obsapi/core.py | 88 ++++++++++++++++++++++++++++++++++--------------------- obsapi/httpapi.py | 2 -- test/test_api.py | 18 ++++++++++++ 3 files changed, 72 insertions(+), 36 deletions(-) diff --git a/obsapi/core.py b/obsapi/core.py index 209aab2..e91c408 100644 --- a/obsapi/core.py +++ b/obsapi/core.py @@ -17,6 +17,7 @@ SourceInfo = namedtuple('SourceInfo', 'package rev vrev srcmd5 verifymd5') Binary = namedtuple('Binary', 'filename size mtime') User = namedtuple('User', 'userid role') Group = namedtuple('Group', 'groupid role') +CallStatus = namedtuple('CallStatus', 'code, summary details') REGEX_NS = 'http://exslt.org/regular-expressions' DEFAULTAPIURL = 'https://api.opensuse.org' @@ -52,11 +53,59 @@ class ObsApi(object): def _api_callback(self, api_instance): self.lastapi = api_instance + print(self.response.text) + if not self.success: + self._raise_error() + + def _raise_error(self): + msg = self.status_message + raise Exception(msg) + + @property + def call_status(self): + + if not self.response.text.strip().startswith('""" +@pytest.fixture +def error_status(): + return """ + This is the summary +
Here are the details
+
+ """ + + def test_callback(requests_mock): """ Test that api callback works and returns a reference to @@ -218,6 +227,15 @@ def test_callback(requests_mock): assert api.lastapi is api.source +def test_call_status_on_error(requests_mock, error_status): + requests_mock.get(rmock.ANY, text=error_status, status_code=404) + with pytest.raises(Exception): + api.ls() + assert api.call_status.code == "Error" + assert api.call_status.summary == "This is the summary" + assert api.call_status.details == "Here are the details" + + def test_get_project_meta(requests_mock): matcher = prj_meta_re text = '' -- cgit v1.2.3