Re: How can we map a specific HTTP error to a python exception?

2007-09-20 Thread Forest Bond
Hi,

On Thu, Sep 20, 2007 at 09:39:05AM -0700, shabda wrote:
> Would writing a middleware for this be a good idea? Then if we get the
> DNE, we just return 404 from process_exception. And during development
> we can just keep our middleware out, so that no real bugs beome 404.
> When we deploy, we can just switch this middleware on.

That would work, but I think that you should handle bugs in production
explicitly, rather than masking them.  If it's a bug, you want to return HTTP
500 (internal server error) and send an e-mail to the server admin notifying him
of the problem.  Returning a 404 is confusing, and your 404 could be cached (!).

404s should be 404s.  There are a variety of ways to turn DNE to 404.  Handling
DNE->404 explicitly prevents your program from masking a bug.  I think that this
is a good thing.

Does this really save that much code?  The negative impact (masking bugs) is
significant.

-Forest
-- 
Forest Bond
http://www.alittletooquiet.net


signature.asc
Description: Digital signature


Re: How can we map a specific HTTP error to a python exception?

2007-09-20 Thread shabda

Would writing a middleware for this be a good idea? Then if we get the
DNE, we just return 404 from process_exception. And during development
we can just keep our middleware out, so that no real bugs beome 404.
When we deploy, we can just switch this middleware on.

On Sep 20, 9:12 pm, Forest Bond <[EMAIL PROTECTED]> wrote:
> Hi,
>
> On Thu, Sep 20, 2007 at 08:47:57AM -0700, shabda wrote:
> > I have a number of pages which can potentially raise the
> > self.model.DoesNotExist exception. Whenever this exception is raised,
> > it maps to a HTTP 404 for me. (That object does not exist in the
> > database, so I cant show this, so I want to return a 404 exception).
> > So apart from manually adding try-except to all my views, how can I
> > return a 404 response on the self.model.DoesNotExist.
>
> You probably don't want to do this in your model.  How about:
>
> from django.core.exceptions import ObjectDoesNotExist
> from django.core.http import Http404
>
> def 404_on_dne(wrapped):
> def wrapper(*args, **kwargs):
> try:
> return wrapped(*args, **kwargs)
> except ObjectDoesNotExist:
> raise Http404
> return wrapper
>
> Then you can wrap your views:
>
> @404_on_dne
> def my_view(request, arg1, arg2):
>obj = Model.objects.get(...)
># do something
>
> I'm not entirely sure that you shouldn't just explicitly handle these 
> exceptions
> in your views (you can end up masking some DNE exceptions that are really 
> bugs),
> but that's a way to do what you want.
>
> Disclaimer: I haven't tried any of this code.  Might have bugs.
>
> -Forest
> --
> Forest Bondhttp://www.alittletooquiet.net
>
>  signature.asc
> 1KDownload


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
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
-~--~~~~--~~--~--~---



Re: How can we map a specific HTTP error to a python exception?

2007-09-20 Thread Forest Bond
Hi,

On Thu, Sep 20, 2007 at 08:47:57AM -0700, shabda wrote:
> I have a number of pages which can potentially raise the
> self.model.DoesNotExist exception. Whenever this exception is raised,
> it maps to a HTTP 404 for me. (That object does not exist in the
> database, so I cant show this, so I want to return a 404 exception).
> So apart from manually adding try-except to all my views, how can I
> return a 404 response on the self.model.DoesNotExist.

You probably don't want to do this in your model.  How about:

from django.core.exceptions import ObjectDoesNotExist
from django.core.http import Http404

def 404_on_dne(wrapped):
def wrapper(*args, **kwargs):
try:
return wrapped(*args, **kwargs)
except ObjectDoesNotExist:
raise Http404
return wrapper

Then you can wrap your views:

@404_on_dne
def my_view(request, arg1, arg2):
   obj = Model.objects.get(...)
   # do something

I'm not entirely sure that you shouldn't just explicitly handle these exceptions
in your views (you can end up masking some DNE exceptions that are really bugs),
but that's a way to do what you want.

Disclaimer: I haven't tried any of this code.  Might have bugs.

-Forest
-- 
Forest Bond
http://www.alittletooquiet.net


signature.asc
Description: Digital signature


How can we map a specific HTTP error to a python exception?

2007-09-20 Thread shabda

I have a number of pages which can potentially raise the
self.model.DoesNotExist exception. Whenever this exception is raised,
it maps to a HTTP 404 for me. (That object does not exist in the
database, so I cant show this, so I want to return a 404 exception).
So apart from manually adding try-except to all my views, how can I
return a 404 response on the self.model.DoesNotExist.


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
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
-~--~~~~--~~--~--~---