-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathexceptions.py
More file actions
38 lines (26 loc) · 1.09 KB
/
exceptions.py
File metadata and controls
38 lines (26 loc) · 1.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import logging
from rest_framework.compat import set_rollback
from rest_framework.exceptions import APIException, status
from rest_framework.response import Response
from rest_framework.views import exception_handler
class HealthcheckException(APIException):
"""Exception class used for when the application's health check fails"""
pass
class DeisException(APIException):
status_code = 400
class AlreadyExists(APIException):
status_code = 409
class ServiceUnavailable(APIException):
status_code = 503
default_detail = 'Service temporarily unavailable, try again later.'
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)
# No response means DRF couldn't handle it
# Output a generic 500 in a JSON format
if response is None:
logging.exception('Uncaught Exception', exc_info=exc)
set_rollback()
return Response({'detail': 'Server Error'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
return response