* Moved GlobalSettings to core app * Moved bonus program logic from User to BonusProgram class * Worked on error handling a bit
24 lines
835 B
Python
24 lines
835 B
Python
import logging
|
|
|
|
from django.core.exceptions import ValidationError as DjangoValidationError
|
|
|
|
from rest_framework.exceptions import ValidationError as DRFValidationError
|
|
from rest_framework.views import exception_handler as drf_exception_handler
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
def exception_handler(exc, context):
|
|
""" Handle Django ValidationError as an accepted exception """
|
|
logger.error(exc)
|
|
|
|
if isinstance(exc, DjangoValidationError):
|
|
if hasattr(exc, 'message_dict'):
|
|
exc = DRFValidationError(detail={'error': exc.message_dict})
|
|
elif hasattr(exc, 'message'):
|
|
exc = DRFValidationError(detail={'error': exc.message})
|
|
elif hasattr(exc, 'messages'):
|
|
exc = DRFValidationError(detail={'error': exc.messages})
|
|
|
|
return drf_exception_handler(exc, context)
|