-
Notifications
You must be signed in to change notification settings - Fork 23
Fix integrity error handling #536
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 hidden or 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 |
|---|---|---|
|
|
@@ -6,4 +6,4 @@ | |
| """ | ||
|
|
||
| # The version for the entire repository | ||
| __version__ = "0.39.0" | ||
| __version__ = "0.39.1" | ||
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| """ | ||
| Tagging REST API exception handling utilities. | ||
| """ | ||
|
|
||
| import logging | ||
| import traceback | ||
|
|
||
| from django.conf import settings | ||
| from django.http import Http404 | ||
| from rest_framework import status | ||
| from rest_framework.exceptions import APIException, PermissionDenied | ||
| from rest_framework.response import Response | ||
| from rest_framework.views import exception_handler | ||
|
|
||
| log = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| def _custom_exception_handler(exc: Exception, context: dict) -> Response | None: | ||
| """ | ||
| Return standard DRF errors for APIException and a generic 500 otherwise. | ||
| This exception handler should eventually be replaced by a more top-level | ||
| exception handler in the openedx-platform repo after the ADR for it is accepted: | ||
| https://github.com/openedx/openedx-platform/pull/38246 | ||
| """ | ||
| # For exceptions expected by DRF return the standard DRF error response: | ||
| # Instances of APIException, subclasses of APIException, Django's Http404 exception, | ||
| # and Django's PermissionDenied exception. | ||
| is_expected_exception = isinstance( | ||
| exc, (APIException, Http404, PermissionDenied) | ||
| ) | ||
| if is_expected_exception: | ||
| return exception_handler(exc, context) | ||
|
|
||
| # DRF always calls exception handlers from within an `except:` block, so we can assume | ||
| # that `log.exception` will automatically insert those exception details and a stack trace. | ||
| log.exception("Unexpected exception while handling API request") | ||
|
|
||
| if settings.DEBUG: | ||
| description_with_traceback = f"{exc.__class__.__name__}: {str(exc)}\n\nTraceback:\n{traceback.format_exc()}" | ||
|
|
||
| return Response( | ||
| {"detail": description_with_traceback}, | ||
| status=status.HTTP_500_INTERNAL_SERVER_ERROR, | ||
| ) | ||
|
|
||
| return Response( | ||
| {"detail": "An unexpected error occurred while processing the request."}, | ||
| status=status.HTTP_500_INTERNAL_SERVER_ERROR, | ||
| ) | ||
|
|
||
|
|
||
| class TaggingExceptionHandlerMixin: | ||
| """ | ||
| Scope custom exception handling to tagging API views only. | ||
| """ | ||
|
|
||
| def get_exception_handler(self): | ||
| return _custom_exception_handler |
This file contains hidden or 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 hidden or 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 hidden or 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.
Sorry @jesperhodge , I only just thought of this after merging:
Do these validation strings appear in the end user UI? If so, they need to be internationalized.
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.
Can you give me an example of how we do that?
As far as I see, we are not internationalizing a single error response in the whole repository.
I think since this is ubiquitous that would call for a new issue that has a bit wider scope and would include internationalizing other error messages like this. What do you think?
Is there any prior art in edx-platform?
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.
Hmm I had a quick look at edx-platform.
It seems like that in edx-platform, ValidationError messages forwarded to the frontend on edx-platform are also not internationalized, for example https://github.com/openedx/openedx-platform/blob/77293cdf8e97eddcb3db32624b690dff8fe5d0df/lms/djangoapps/badges/models.py#L15
Uh oh!
There was an error while loading. Please reload this page.
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.
@jesperhodge
Ack, you're right, and it's worse than that--nothing in this repo is internationalized. And I guess we've noticed that before, too: #483 . No need for you to do anything here--we'll do a big sweep of internationalization at some point in the future. Thanks for checking on that.
I think the rule of thumb is "If the user will see it, it must be internationalized" rather than a blanket "all exceptions strings do/don't need to be internationalized".
Yes, we use the standard gettext function for all translation. Usually we alias it to
_: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.
That message is internationalized. The
_function returns the localized string, if it available.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.
Oh! That's good