From 3689d01f7ecad5c24d9df3c3e0346c3cf2b47c80 Mon Sep 17 00:00:00 2001 From: Christian Dywan Date: Mon, 11 Oct 2021 11:53:10 +0200 Subject: [PATCH 1/2] Catch JSON decoding errors in browser.get See: https://progress.opensuse.org/issues/100709 --- openqa_review/browser.py | 10 ++++++++-- tests/test_openqa_review.py | 12 +++++++++++- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/openqa_review/browser.py b/openqa_review/browser.py index b236718..d208273 100644 --- a/openqa_review/browser.py +++ b/openqa_review/browser.py @@ -148,15 +148,21 @@ def _get(self, url, as_json=False): # pragma: no cover msg = "Request to {} was not successful: {}".format(url, str(e)) log.warn(msg) raise DownloadError(msg) - try: r.raise_for_status() except requests.exceptions.HTTPError as e: msg = "Request to {} failed: {}".format(url, str(e)) log.warn(msg) raise DownloadError(msg) + return self.decode_content(r.content.decode("utf-8"), url, as_json) - content = r.json() if as_json else r.content.decode("utf8") + def _decode_content(self, url, raw, as_json=False): # pragma: no cover + try: + content = json.loads(raw) if as_json else raw + except json.decoder.JSONDecodeError as e: + msg = 'Unable to decode JSON for {}: {} (Content was: "{}")'.format(url, str(e), raw) + log.warn(msg) + raise DownloadError(msg) return content def json_rpc_get(self, url, method, params, cache=True): diff --git a/tests/test_openqa_review.py b/tests/test_openqa_review.py index 3080c7c..21ac081 100644 --- a/tests/test_openqa_review.py +++ b/tests/test_openqa_review.py @@ -22,7 +22,7 @@ sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))) -from openqa_review.browser import filename_to_url +from openqa_review.browser import filename_to_url, DownloadError from openqa_review import openqa_review # SUT @@ -573,3 +573,13 @@ def test_arch_distinguish(): report = str(openqa_review.generate_report(args)) assert "ppc64le" in report + + +def test_browser_get_invalid_json(): + args = cache_test_args_factory() + args.include_softfails = True + browser = browser_factory(args) + + with pytest.raises(DownloadError) as e: + browser._decode_content("http://example.com", "", as_json=True) + assert "Unable to decode JSON" in str(e) From 58543052acb45c216563bbe45fdf17d1c9fec06a Mon Sep 17 00:00:00 2001 From: Christian Dywan Date: Thu, 14 Oct 2021 19:12:32 +0200 Subject: [PATCH 2/2] Add test for report with invalid JSON --- tests/test_openqa_review.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tests/test_openqa_review.py b/tests/test_openqa_review.py index 21ac081..7172464 100644 --- a/tests/test_openqa_review.py +++ b/tests/test_openqa_review.py @@ -532,6 +532,12 @@ def test_reminder_comments_on_referenced_bugs_are_posted(): p, pr = list(report.report.items())[0] report.report[p + 237] = pr + # test invalid JSON + with pytest.raises(DownloadError) as e: + report = str(openqa_review.generate_report(args)) + assert "Unable to decode JSON" in str(e) + assert "Unable to decode JSON" in report + openqa_review.reminder_comment_on_issues(report) args.dry_run = False