For interoperability with EmberData it seems I need to reply with 422 (Unprocessable Entity
) instead of 400 (Bad Request
) whenever validation errors occur. I have two questions:
And bonus:
The easiest way to change the error style through all the view in your application is to always use serializer. is_valid(raise_exception=True) , and then implement a custom exception handler that defines how the error response is created.
The 4xx class of status code is intended for cases in which the client seems to have erred. Except when responding to a HEAD request, the server SHOULD include an entity containing an explanation of the error situation, and whether it is a temporary or permanent condition.
The generic views use the raise_exception=True flag, which means that you can override the style of validation error responses globally in your API. To do so, use a custom exception handler, as described above. By default this exception results in a response with the HTTP status code "400 Bad Request".
422 is part of the WebDAV DRF which error codes aren't in DRF. This doesn't stop you to use it. They are just a human readable version of the number itself.
One option would be to override rest_framework.exceptions.ValidationError.status_code
and set it to 422.
Edit - Changing the default error code:
# At the top of a views.py file, by the ends of import add:
from rest_framework.exceptions import ValidationError
ValidationError.status_code = 422
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With