-
Notifications
You must be signed in to change notification settings - Fork 17
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 #75 from JeffreyMFarley/improve-error-handling
Improve error handling
- Loading branch information
Showing
9 changed files
with
98 additions
and
114 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,31 @@ | ||
import logging | ||
from rest_framework import status | ||
from rest_framework.response import Response | ||
from elasticsearch import TransportError | ||
|
||
log = logging.getLogger(__name__) | ||
|
||
|
||
def catch_es_error(function): | ||
def wrap(request, *args, **kwargs): | ||
try: | ||
return function(request, *args, **kwargs) | ||
except TransportError as e: | ||
status_code = e.status_code if isinstance(e.status_code, int) \ | ||
else status.HTTP_400_BAD_REQUEST | ||
except TransportError as te: | ||
log.error(te) | ||
|
||
status_code = 424 # HTTP_424_FAILED_DEPENDENCY | ||
res = { | ||
"error": 'There was an error calling Elasticsearch' | ||
} | ||
return Response(res, status=status_code) | ||
except Exception as e: | ||
log.error(e) | ||
|
||
status_code = status.HTTP_500_INTERNAL_SERVER_ERROR | ||
res = { | ||
"error": 'Elasticsearch error: ' + e.error | ||
"error": 'There was a problem retrieving your request' | ||
} | ||
return Response(res, status=status_code) | ||
wrap.__doc__ = function.__doc__ | ||
wrap.__name__ = function.__name__ | ||
return wrap | ||
return wrap |
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
Oops, something went wrong.