def json_api_exception_handler(exc, context):
 from rest_framework.views import exception_handler
 response = exception_handler(exc, context)
 errors = []
 if response:
  message = response.data
  if isinstance(exc, TwoFactorRequiredError):
   response['X-OSF-OTP'] = 'required; app'
  if isinstance(exc, JSONAPIException):
   errors.extend([{'source': (exc.source or {}), 'detail': exc.detail, 'meta': (exc.meta or {})}])
  elif isinstance(message, dict):
   errors.extend(dict_error_formatting(message, None))
  else:
   if isinstance(message, basestring):
    message = [message]
   for (index, error) in enumerate(message):
    if isinstance(error, dict):
     errors.extend(dict_error_formatting(error, index))
    else:
     errors.append({'detail': error})
  response.data = {'errors': errors}
 return response

def json_api_exception_handler ( exc , context ) : from rest_framework . views import exception_handler response = exception_handler ( exc , context ) errors = [ ] if response : message = response . data if isinstance ( exc , TwoFactorRequiredError ) : response [ 'X-OSF-OTP' ] = 'required; app' if isinstance ( exc , JSONAPIException ) : errors . extend ( [ { 'source' : ( exc . source or { } ) , 'detail' : exc . detail , 'meta' : ( exc . meta or { } ) } ] ) elif isinstance ( message , dict ) : errors . extend ( dict_error_formatting ( message , None ) ) else : if isinstance ( message , basestring ) : message = [ message ] for ( index , error ) in enumerate ( message ) : if isinstance ( error , dict ) : errors . extend ( dict_error_formatting ( error , index ) ) else : errors . append ( { 'detail' : error } ) response . data = { 'errors' : errors } return response
custom exception handler that returns errors object as an array .