-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #759 from fecgov/feature/689
689 - Update output to print to sysout while refusing all errors or exceptions that are not validation related from going to the front end
- Loading branch information
Showing
2 changed files
with
15 additions
and
9 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,33 @@ | ||
from rest_framework.exceptions import ValidationError | ||
from django.http import HttpResponseServerError | ||
from fecfiler.authentication.views import delete_user_logged_in_cookies | ||
from rest_framework.views import exception_handler | ||
# from django.http import HttpResponseServerError | ||
# from fecfiler.settings import DEBUG | ||
import structlog | ||
|
||
logger = structlog.get_logger(__name__) | ||
|
||
|
||
def custom_exception_handler(exc, context): | ||
# Call REST framework's default exception handler first, | ||
# to get the standard error response. | ||
response = exception_handler(exc, context) | ||
# if response is None: | ||
# return HttpResponseServerError() | ||
|
||
if response is None: | ||
return HttpResponseServerError() | ||
|
||
# Delete user cookies on forbidden http response. | ||
# this will ensure that when the user is redirected | ||
# to the login page due to the 403, any cookies | ||
# (such as indicating committee id) are removed to | ||
# allow for a clean new login. | ||
if response is not None and response.status_code == 403: | ||
if response.status_code == 403: | ||
delete_user_logged_in_cookies(response) | ||
|
||
# Do not allow an error response body | ||
# if getattr(response, 'data'): | ||
# response.data = None | ||
# Do not allow an error response body unless validation | ||
data = getattr(response, 'data') | ||
exception_type = type(exc) | ||
logger.error(f"Error: {data}") | ||
if data and exception_type is not ValidationError: | ||
response.data = None | ||
|
||
return response |