-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refactor circulation exceptions (PP-991) #1694
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,7 +30,21 @@ | |
LoanInfo, | ||
PatronActivityCirculationAPI, | ||
) | ||
from api.circulation_exceptions import * | ||
from api.circulation_exceptions import ( | ||
AlreadyCheckedOut, | ||
AlreadyOnHold, | ||
CannotHold, | ||
CannotLoan, | ||
CannotReleaseHold, | ||
CurrentlyAvailable, | ||
NoAvailableCopies, | ||
NoLicenses, | ||
NotCheckedOut, | ||
NotOnHold, | ||
PatronHoldLimitReached, | ||
PatronLoanLimitReached, | ||
RemoteInitiatedServerError, | ||
) | ||
from api.selftest import HasCollectionSelfTests, SelfTestResult | ||
from api.web_publication_manifest import FindawayManifest, SpineItem | ||
from core.analytics import Analytics | ||
|
@@ -79,6 +93,7 @@ | |
from core.util import base64 | ||
from core.util.datetime_helpers import datetime_utc, strptime_utc, to_utc, utc_now | ||
from core.util.http import HTTP | ||
from core.util.problem_detail import BaseProblemDetailException | ||
from core.util.xmlparser import XMLParser, XMLProcessor | ||
|
||
|
||
|
@@ -894,23 +909,7 @@ def date_from_subtag(self, tag, key, required=True): | |
return self.parse_date(value) | ||
|
||
|
||
class BibliothecaException(Exception): | ||
pass | ||
|
||
|
||
class WorkflowException(BibliothecaException): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This wasn't being used anywhere. |
||
def __init__(self, actual_status, statuses_that_would_work): | ||
self.actual_status = actual_status | ||
self.statuses_that_would_work = statuses_that_would_work | ||
|
||
def __str__(self): | ||
return "Book status is {}, must be: {}".format( | ||
self.actual_status, | ||
", ".join(self.statuses_that_would_work), | ||
) | ||
|
||
|
||
class ErrorParser(BibliothecaParser[Exception]): | ||
class ErrorParser(BibliothecaParser[BaseProblemDetailException]): | ||
"""Turn an error document from the Bibliotheca web service into a CheckoutException""" | ||
|
||
wrong_status = re.compile( | ||
|
@@ -930,13 +929,20 @@ class ErrorParser(BibliothecaParser[Exception]): | |
def xpath_expression(self) -> str: | ||
return "//Error" | ||
|
||
def process_first(self, string: str | bytes) -> Exception: | ||
def process_first(self, string: str | bytes) -> BaseProblemDetailException: | ||
try: | ||
return_val = super().process_first(string) | ||
except Exception as e: | ||
# The server sent us an error with an incorrect or | ||
# nonstandard syntax. | ||
return RemoteInitiatedServerError(string, BibliothecaAPI.SERVICE_NAME) | ||
if isinstance(string, bytes): | ||
try: | ||
debug = string.decode("utf-8") | ||
except UnicodeDecodeError: | ||
debug = "Unreadable error message (Unicode decode error)." | ||
else: | ||
debug = string | ||
return RemoteInitiatedServerError(debug, BibliothecaAPI.SERVICE_NAME) | ||
|
||
if return_val is None: | ||
# We were not able to interpret the result as an error. | ||
|
@@ -950,7 +956,7 @@ def process_first(self, string: str | bytes) -> Exception: | |
|
||
def process_one( | ||
self, error_tag: _Element, namespaces: dict[str, str] | None | ||
) -> Exception: | ||
) -> BaseProblemDetailException: | ||
message = self.text_of_optional_subtag(error_tag, "Message") | ||
if not message: | ||
return RemoteInitiatedServerError( | ||
|
@@ -982,7 +988,7 @@ def process_one( | |
|
||
m = self.wrong_status.search(message) | ||
if not m: | ||
return BibliothecaException(message) | ||
return RemoteInitiatedServerError(message, BibliothecaAPI.SERVICE_NAME) | ||
actual, expected = m.groups() | ||
expected = expected.split(",") | ||
|
||
|
@@ -1010,7 +1016,7 @@ def process_one( | |
if "CAN_LOAN" in expected: | ||
return CannotLoan(message) | ||
|
||
return BibliothecaException(message) | ||
return RemoteInitiatedServerError(message, BibliothecaAPI.SERVICE_NAME) | ||
|
||
|
||
class PatronCirculationParser(XMLParser): | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Replaced this exception with
RemoteInitiatedServerError
, so that we get problem details from all the exceptions thrown by the Bibliotecha API.