I am trying to throw a custom error in my api.py file with DEBUG = True. It throws the error
{
"error_message": "Sorry, this request could not be processed. Please try again later."
}
This is the default TASTYPIE_CANNED_ERROR message.
I want the error to be something like this:
{"error_message": "{'id': 2671, 'error': 'Duplicate'}"}
I tried overriding _handle_500 method but that seems to return my website html page in response.
I get the required format with status code 400 with:
raise BadRequest({"id": int(attempt[0].id), "error": "Duplicate"})
But I need the status code to be 500.
Use ImmediateHttpResponse and create you error messages dict and then send it in response. And it is also necessary to specify content_type="application/json".
from django.http import HttpResponse
from tastypie.exceptions import ImmediateHttpResponse
// Build your response
response = {"error_message": {'id': 2671, 'error': 'Duplicate'}}
raise ImmediateHttpResponse(response=HttpResponse(json.dumps(response), status=401, content_type="application/json"))