Skip to content
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 1 commit into from
Feb 26, 2024

Conversation

jonathangreen
Copy link
Member

@jonathangreen jonathangreen commented Feb 23, 2024

Description

This refactors how we are handling CirculationExceptions, so they all inherit from BaseProblemDetailException and are able to be turned into a ProblemDetail. This lets us simplify the exception handlers in the Loans controller to catch any CirculationException that the Circulation APIs might throw, and return the appropriate ProblemDetail, instead of continuing to grow the huge list of different Exceptions and actions we were handling.

Motivation and Context

Reviewing the list of unhandled exceptions we are seeing in production, multiple in the top 10 were CirculationExceptions that were not in the Except block, or were handled for some actions like borrow but not others like hold. This PR makes the handling we have here more consistent.

Having a base class BaseProblemDetailException also makes it easier to know if we are able to turn an exception into a ProblemDetail or not.

How Has This Been Tested?

  • Running tests

Checklist

  • I have updated the documentation accordingly.
  • All new and existing tests passed.

Copy link

codecov bot commented Feb 23, 2024

Codecov Report

Attention: Patch coverage is 94.94949% with 10 lines in your changes are missing coverage. Please review.

Project coverage is 90.21%. Comparing base (20d6e55) to head (0da3ac8).
Report is 2 commits behind head on main.

Files Patch % Lines
core/user_profile.py 42.85% 2 Missing and 2 partials ⚠️
core/util/http.py 88.88% 2 Missing and 1 partial ⚠️
core/util/problem_detail.py 80.00% 1 Missing and 1 partial ⚠️
core/opds2_import.py 0.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1694      +/-   ##
==========================================
+ Coverage   90.08%   90.21%   +0.12%     
==========================================
  Files         245      245              
  Lines       28170    28182      +12     
  Branches     6422     6446      +24     
==========================================
+ Hits        25377    25424      +47     
+ Misses       1848     1822      -26     
+ Partials      945      936       -9     
Flag Coverage Δ
Api 75.99% <90.90%> (+0.19%) ⬆️
Core 59.81% <61.11%> (+0.01%) ⬆️
migration 26.95% <54.49%> (+0.05%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@jonathangreen jonathangreen force-pushed the feature/refactor-circulation-exceptions branch from c488ce0 to cce6f32 Compare February 23, 2024 13:27
pass


class WorkflowException(BibliothecaException):
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This wasn't being used anywhere.

@@ -894,23 +909,7 @@ def date_from_subtag(self, tag, key, required=True):
return self.parse_date(value)


class BibliothecaException(Exception):
Copy link
Member Author

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.

@@ -416,7 +424,7 @@ def checkout(
"User validation against Enki server with %s / %s was unsuccessful."
% (patron.authorization_identifier, pin)
)
raise AuthorizationFailedException()
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was the only place that we used AuthorizationFailedException, everywhere else used PatronAuthorizationFailedException and it had the same problem detail, so I removed AuthorizationFailedException.

@@ -57,7 +57,7 @@

NO_ACCEPTABLE_FORMAT = pd(
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The status codes here were changed because the only place that used these ProblemDetails was the loans controller exception handler, where they were called like this:

CANNOT_FULFILL.with_debug(str(e), status_code=e.status_code)

So they always had the status code of the exception, rather then the status code associated with the ProblemDetail. This let me drop the status code on the exceptions and just use the one on the ProblemDetail.

@jonathangreen jonathangreen marked this pull request as ready for review February 23, 2024 16:21
@jonathangreen jonathangreen requested a review from a team February 23, 2024 16:21
This refactors how we are handling CirculationExceptions, so they all inherit from BaseProblemDetailException and are able to be turned into a ProblemDetail.
Copy link
Contributor

@dbernstein dbernstein left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good.

@jonathangreen jonathangreen added the bug Something isn't working label Feb 26, 2024
@jonathangreen jonathangreen merged commit c3d7cdc into main Feb 26, 2024
28 checks passed
@jonathangreen jonathangreen deleted the feature/refactor-circulation-exceptions branch February 26, 2024 23:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants