In my code for an API it made sense to me to import and raise
`HttpResponseBadRequest ` when the API was given bad data to work
with. I'm importing and raising the error like this:
from django.http import Http404, HttpResponseBadRequest
raise HttpResponseBadRequest, "Invalid data"
But when I do that, I get a TypeError:
Exception Type: TypeError at /api/v1/clips/new/
Exception Value: exceptions must be classes, instances, or strings
(deprecated), not type
I thought this was similar to Http404 until I looked at the code in
django/http/__init__.py and see that the Http404 is subclassing
Exception while most of the other "errors" subclass HttpResponse. And
there is even a HttpResponseNotFound that has a status_code of 404 to
confuse matters even more.
I also grep'd the codebase to see what was the proper way to raise an
error and saw two ways:
1) raise Http404("Error message here.")
2) raise Http404, "Error message here."
I tried both for my `HttpResponseBadRequest` and still got a type
error. Following the logic that this is just a subclasses
HttpResponse I changed it to a return:
return HttpResponseBadRequest("Error message here.")
and that worked.
In the end I was a bit confused by all of this and came away wondering:
* Shouldn't all HTTP error code raising function similarly? Shouldn't
I be able to raise a 400 error the same as a 404?
* Shouldn't Django be consistent in its own raising of errors and
choose one way to do it (e.g. choose #1 above over #2)? Out of
curiosity I grep'd the code to count how many chose the paren method
vs the comma method:
~/sandbox/django/django_trunk/django > egrep -R 'raise \w+\(.*' * |
grep -v .svn | wc -l # paren method
371
~/sandbox/django/django_trunk/django > egrep -R 'raise \w+,.*' * |
grep -v .svn | wc -l # comma method
184
-Rob
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---