From 9d27d52af9b852ac492f391fe69f5dd0a027c3cf Mon Sep 17 00:00:00 2001 From: Wolfgang Müller Date: Fri, 15 Nov 2024 14:13:24 +0100 Subject: backend/plugins: Throw error if E-Hentai response is missing 'gmetadata' It might be that we get a valid (maybe empty) response from the API, in which case we do not want to simply crash because we expect the 'gmetadata' field in the response. Instead, throw a proper ScrapeError for it. --- src/hircine/plugins/scrapers/ehentai_api.py | 2 ++ tests/plugins/scrapers/test_ehentai_api.py | 16 ++++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/src/hircine/plugins/scrapers/ehentai_api.py b/src/hircine/plugins/scrapers/ehentai_api.py index d9d539d..d276c5b 100644 --- a/src/hircine/plugins/scrapers/ehentai_api.py +++ b/src/hircine/plugins/scrapers/ehentai_api.py @@ -71,5 +71,7 @@ class EHentaiAPIScraper(Scraper): yield from handler.scrape(response) except json.JSONDecodeError as err: raise ScrapeError("Could not parse JSON response") from err + except (KeyError, IndexError) as err: + raise ScrapeError("Response is missing 'gmetadata' field") from err else: raise ScrapeError(f"Request failed with status code {request.status_code}") diff --git a/tests/plugins/scrapers/test_ehentai_api.py b/tests/plugins/scrapers/test_ehentai_api.py index dd283e1..c746440 100644 --- a/tests/plugins/scrapers/test_ehentai_api.py +++ b/tests/plugins/scrapers/test_ehentai_api.py @@ -124,6 +124,22 @@ def test_raises_scrape_error_with_invalid_json(requests_mock, gen_comic): assert set(scraper.collect()) == set() +def test_raises_scrape_error_with_missing_field(requests_mock, gen_comic): + comic = next(gen_comic) + comic.url = "https://exhentai.org/g/1025913/fdaabef1a2" + + scraper = ehentai_api.EHentaiAPIScraper(comic) + + requests_mock.post(ehentai_api.API_URL, text="{}") + + assert scraper.is_available + assert scraper.id == 1025913 + assert scraper.token == "fdaabef1a2" + + with pytest.raises(ScrapeError, match="Response is missing 'gmetadata' field"): + assert set(scraper.collect()) == set() + + def test_raises_scrape_error_with_error_code(requests_mock, gen_comic): comic = next(gen_comic) comic.url = "https://exhentai.org/g/1025913/fdaabef1a2" -- cgit v1.2.3-2-gb3c3