I like the idea, but not the addition of yet another setting. Maybe
an on_exception signal could be added which can do its own thing and
return False if the default action shouldn't be taken?
In handlers.base.get_response, theres:
"
receivers = dispatcher.send(signal=signals.got_request_exception,
request=request)
"
Return value cases:
In [2]: l = [] #no receivers
In [3]: l2 = [None] #only old receivers
In [4]: l3 = [False] #new receiver asking not to default handle
In [5]: l4 = [None, False] #old and new receiver
In [6]: l5 = [True, False] #mix of new receivers, one asking not to
default handle
The odd edge case is where a different signal handlers return both
True and False (where existing code would return None). What to do
there is a design choice; I'd vote for False wins.
So I'd add:
"
if receivers:
default_handler = not bool([ret_value for ret_value in receivers
if ret_value == False])
else:
default_handler = True
if default_handler:
...existing handling code here...
callback, param_dict = resolver.resolve500()
return callback(request, **param_dict)
"
So the reason handle_uncaught_exception is in its own method is, as
noted in the docstring for that method, so that you can have exactly
this type of control. I have one client, for example, who want to use
Python's logging module instead of sending email and no doubt lots of
other customisations are possible.
The idea is that you create your own subclass of which
django/core/handlers/* class you want (wsgi or modpython, typically) and
override the handle_uncaught_exception method. After all, you specify
the handler class to use when setting things up to talk to the
webserver, so it's just as easy to use your own subclass as one of
Django's built in versions.
This is done via subclassing rather than a setting to avoid settings
proliferation for something where there's no real common case of
customisations and so the ultimate configuration is provided: you write
Python code to do whatever you want. It's also meant to impose a slight
barrier to entry in the sense that you have to be able to write Python
code in order to customise this, which is reasonable for something as
core-functionality oriented as handling uncaught exceptions (and no
problems for people on django-dev, obviously).
Regards,
Malcolm
We're working it around by not having settings.ADMINS set in production
environment. Instead we log unhandled exceptions by listening to
`signals.got_request_exception`. I don't even consider it as a
workaround actually but as a normal solution :-)
Well, not really. You have to set the Apache configuration once to point
to the right thing. Just like you do now.
> Also, I suspect that a large number of Django developers use
> "manage.py runserver" (which is hardcoded to use WSGIHandler) for
> development and testing and use ModPythonHandler on the release
> servers. To minimize unexpected bugs in the release environment that
> never show up in the test environment, it's best not to introduce
> additional differences between these environments. A custom
> implementation of handle_uncaught_exception would be hard for a
> developer using "manage.py runserver" to test.
Patches welcome to fix that if you see it as a real problem.
> My hunch is that even though there isn't a common way that users would
> want to customize uncaught exceptions, it's probably common that users
> would want to customize it in some way or another. So subclassing the
> handler seems like a rather high barrier to entry.
No it's not. Since there's no common way to customize, sub-classing is
the perfect way to do it, since people can then do whatever they want.
How is asking somebody to write a subclass in Python a high barrier to
entry? We're assuming knowledge of the language, but that's all.
I don't see any of your objections here a real issues except the need
for being able to simulate things in the development server.
Regards,
Malcolm
No, this isn't correct. You already have to specify which handler to
run. You still have to do that exactly once in each case.
> and didn't introduce differences between the test and
> release environments.
There's already differences between the test and release environments if
you mean runserver versus anything running with a real webserver.
Expecting complete parity there is a false goal and you absolutely must
do real production-preparedness tests against the real webserver you're
going to use in production. That's hardly a surprise. A patch to allow
something like --with-handler='my.handler.class' (making up an API on
the spot; that needs to be thought about) would be a good idea for
completeness, though, you're right.
> I mean, really, the notification method for errors should have nothing
> to do with the type of web server interface you have (WSGI /
> mod_python).
The method is called handle_uncaught_exception, not
notify_about_uncaught_exception. Right now it only happens to do
notification, but because it's extensible it could do stuff that is
handler dependent.
Malcolm