VRE Backend API and Scheduler
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

53 lines
1.9 KiB

# custom handler
from rest_framework.response import Response
from rest_framework.views import exception_handler
def json_schema_error_message(exc, context):
# We want json schema error messages. So we need the format:
# [
# {
# "keyword": "type",
# "dataPath": ".phone",
# "schemaPath": "#/properties/phone/type",
# "params": {
# "type": "string"
# },
# "message": "should be string"
# },
# {
# "keyword": "format",
# "dataPath": ".email",
# "schemaPath": "#/properties/email/format",
# "params": {
# "format": "email"
# },
# "message": "should match format \"email\""
# }
# ]
try:
system_errors = exc.get_full_details()
except Exception as ex:
system_errors = [{'exception' : {'code' : 'exception', 'message' : 'Something went wrong'}}]
if isinstance(system_errors, list):
system_errors = {'_no_field' : system_errors}
elif system_errors.get('message') is not None:
# If there is a message keyword in the main system errors, then it is not a form error, but a more general error
system_errors = {'_no_field' : [system_errors]}
json_errors = []
for errorfield, errors in system_errors.items():
for error in errors:
json_errors.append({'keyword' : error.get('code', None),
'dataPath' : f'.{errorfield}',
'schemaPath' : None,
'params' : {},
'message' : error.get('message',None)
})
# Here we overrule the output of the error message with our JSON error schema format
exc.detail = json_errors
response = exception_handler(exc, context)
return response