From aafd727b20c9aeed14db26b21183e8f01c63f332 Mon Sep 17 00:00:00 2001 From: Jonathan Green Date: Wed, 23 Oct 2024 14:29:39 -0300 Subject: [PATCH] Turn xml parsing exception into integration error --- src/palace/manager/api/bibliotheca.py | 15 +++++++++++---- tests/manager/api/test_bibliotheca.py | 9 ++++++++- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/src/palace/manager/api/bibliotheca.py b/src/palace/manager/api/bibliotheca.py index fe1aaee69..52d0d5903 100644 --- a/src/palace/manager/api/bibliotheca.py +++ b/src/palace/manager/api/bibliotheca.py @@ -18,7 +18,7 @@ import dateutil.parser from dependency_injector.wiring import Provide, inject from flask_babel import lazy_gettext as _ -from lxml.etree import _Element +from lxml.etree import Error, _Element from pymarc import parse_xml_to_array from sqlalchemy.orm import Session @@ -102,7 +102,7 @@ to_utc, utc_now, ) -from palace.manager.util.http import HTTP +from palace.manager.util.http import HTTP, RemoteIntegrationException from palace.manager.util.problem_detail import BaseProblemDetailException from palace.manager.util.xmlparser import XMLParser, XMLProcessor @@ -422,8 +422,15 @@ def _patron_activity_request(self, patron): def patron_activity(self, patron, pin): response = self._patron_activity_request(patron) - collection = self.collection - return PatronCirculationParser(self.collection).process_all(response.content) + try: + return PatronCirculationParser(self.collection).process_all( + response.content + ) + except Error as e: + # XML parse error from remote. + raise RemoteIntegrationException( + response.url, "Unable to parse response XML." + ) from e TEMPLATE = "<%(request_type)s>%(item_id)s%(patron_id)s" diff --git a/tests/manager/api/test_bibliotheca.py b/tests/manager/api/test_bibliotheca.py index 8d279e183..ac0aa62fc 100644 --- a/tests/manager/api/test_bibliotheca.py +++ b/tests/manager/api/test_bibliotheca.py @@ -65,7 +65,7 @@ from palace.manager.sqlalchemy.model.resource import Hyperlink, Representation from palace.manager.sqlalchemy.model.work import Work from palace.manager.util.datetime_helpers import datetime_utc, utc_now -from palace.manager.util.http import BadResponseException +from palace.manager.util.http import BadResponseException, RemoteIntegrationException from palace.manager.util.web_publication_manifest import AudiobookManifest from tests.mocks.analytics_provider import MockAnalyticsProvider from tests.mocks.bibliotheca import MockBibliothecaAPI @@ -488,6 +488,13 @@ def test_sync_patron_activity(self, bibliotheca_fixture: BibliothecaAPITestFixtu assert datetime_utc(2015, 5, 27, 17, 5, 34) == h2.end assert 0 == h2.position + # Test the case where we get bad data in response + bibliotheca_fixture.api.queue_response(200, content="") + with pytest.raises( + RemoteIntegrationException, match="Unable to parse response XML" + ): + bibliotheca_fixture.api.sync_patron_activity(patron, "dummy pin") + def test_place_hold(self, bibliotheca_fixture: BibliothecaAPITestFixture): db = bibliotheca_fixture.db patron = db.patron()