Skip to content

Commit

Permalink
Turn xml parsing exception into integration error
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathangreen committed Oct 23, 2024
1 parent 371492c commit aafd727
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
15 changes: 11 additions & 4 deletions src/palace/manager/api/bibliotheca.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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><ItemId>%(item_id)s</ItemId><PatronId>%(patron_id)s</PatronId></%(request_type)s>"

Expand Down
9 changes: 8 additions & 1 deletion tests/manager/api/test_bibliotheca.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit aafd727

Please sign in to comment.