Neat. I've been doing Django since the 0.9.6 days and have never written a 
middleware. I guess there's a first time for everything.

Thanks!

====================================
Steven L Smith, Web Developer
Department of Information Technology Services
Nazareth College of Rochester
585-389-2085 | ssmit...@naz.edu | KC2YTC
http://www.naz.edu/pub/~ssmith46
====================================

----- Original Message -----
From: "Tom Evans" <tevans...@googlemail.com>
To: django-users@googlegroups.com
Sent: Thursday, June 23, 2011 9:28:15 AM GMT -05:00 US/Canada Eastern
Subject: Re: Returning a 503 response instead of a 500 response

On Thu, Jun 23, 2011 at 2:19 PM, Steven L Smith <ssmit...@zimbra.naz.edu> wrote:
> Some of our apps have to talk to databases outside of our control, and their 
> operators don't have the same uptime standards that we do...
>
> What can I do to make Django return a "503 Service Unavailable" instead of a 
> "500 Internal Server Error", when it encounters a DatabaseError or 
> ProgrammingError while trying to access the database? Bonus points if I could 
> make the 503 page say "there was a temporary database communication problem."
>
> I have some ideas of what I *could* modify, but I'd rather incorporate this 
> into a product or setting instead of having to patch the actual django 
> database backend code itself.
>

Exception handling is performed by middleware. When a django view
encounters an un-handled exception, each installed middleware class is
asked if it wants to handle this exception. If none do, Django sends
out it's pre-canned response as it sees fit.

So, simply write your own exception handling middleware that handles
DatabaseError or ProgrammingError, and passes on other errors. See the
docs for more details:

https://docs.djangoproject.com/en/1.3/topics/http/middleware/#process-exception

Cheers

Tom

-- 
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 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

-- 
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 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to