get_object_or_404 and rc.NOT_FOUND

39 views
Skip to first unread message

Paul Carduner

unread,
Sep 10, 2009, 1:04:35 AM9/10/09
to django-piston
I just started playing around with piston tonight after hearing good
things about the talk at djangocon (though I didn't go... shame on
me).

I've encountered something that seems a bit confusing to me? Maybe I
am doing it wrong.

In several of my handlers, I need to fetch an object from the database
based on a url slug and then do some additional processing to build
the result set. I'd like to use get_object_or_404 to do the initial
query, but it seems like piston is interpreting the Http404 exception
that gets raised as a regular internal exception and turns it into a
500 internal error. I was expecting the Http404 exception to be
caught in the Resource calling the handler and turned into an
rc.NOT_FOUND response.

Is this the intended behavior? And if so, what is the reasoning
behind it?

For now I've created a decorator to catch the 404s but it seems like
this should be handled directly in piston.

def allow_404(func):
"""
decorator that catches Http404 exceptions and safely returns
piston style 404 responses (rc.NOT_FOUND).
"""
def wrapper(*args, **kwargs):
try:
return func(*args, **kwargs)
except Http404:
return rc.NOT_FOUND
return wrapper

jespern

unread,
Sep 10, 2009, 4:19:25 AM9/10/09
to django-piston
On Sep 10, 8:04 am, Paul Carduner <paulcardu...@gmail.com> wrote:
> I just started playing around with piston tonight after hearing good
> things about the talk at djangocon (though I didn't go... shame on
> me).
>
> I've encountered something that seems a bit confusing to me?  Maybe I
> am doing it wrong.
>
> In several of my handlers, I need to fetch an object from the database
> based on a url slug and then do some additional processing to build
> the result set.  I'd like to use get_object_or_404 to do the initial
> query, but it seems like piston is interpreting the Http404 exception
> that gets raised as a regular internal exception and turns it into a
> 500 internal error.  I was expecting the Http404 exception to be
> caught in the Resource calling the handler and turned into an
> rc.NOT_FOUND response.
>
> Is this the intended behavior?  And if so, what is the reasoning
> behind it?

This is a missing feature. We should of course catch Http404's, to let
people use the get_object_or_404 shortcut. I'll fix this in my fork
later today, which will be the coming 0.2.3 release. I've had good
results from people running their testsuite on it, so it should be
100% backwards compatible.

Thanks for reporting this!


Jesper

Reply all
Reply to author
Forward
0 new messages