custom exception for 409 error
This commit is contained in:
parent
611c403752
commit
190615491e
22
apps/api/exceptions.py
Normal file
22
apps/api/exceptions.py
Normal file
@ -0,0 +1,22 @@
|
||||
"""
|
||||
|
||||
"""
|
||||
|
||||
from rest_framework.views import exception_handler
|
||||
from rest_framework.response import Response
|
||||
from rest_framework import status
|
||||
|
||||
def conflict_exception_handler(exc, context):
|
||||
response = exception_handler(exc, context)
|
||||
|
||||
if response is not None and isinstance(response.data, dict):
|
||||
for errors in response.data.values():
|
||||
if not isinstance(errors, list):
|
||||
continue
|
||||
|
||||
for error in errors:
|
||||
if hasattr(error, 'code') and error.code == 'unique':
|
||||
response.status_code = status.HTTP_409_CONFLICT
|
||||
return response
|
||||
|
||||
return response
|
@ -247,5 +247,6 @@ REST_FRAMEWORK = {
|
||||
'DEFAULT_THROTTLE_RATES': {
|
||||
'anon': '100/day',
|
||||
'user': '1000/day'
|
||||
}
|
||||
},
|
||||
"EXCEPTION_HANDLER": "apps.api.exceptions.conflict_exception_handler"
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user