jeansnkicks / pyev

Automatically exported from code.google.com/p/pyev
0 stars 0 forks source link

Add exception handler function #19

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
Right now it's up to the callbacks to deal with exceptions. Even with those 
which are truly... well.. exceptional. It is pretty common not to handle 
exceptions you don't expect to occur. Usually they'd just cause the process to 
terminate.
However, in a server environment that's bad - you already mention this in the 
docs. But pyev should have a callback that is executed whenever it gets an 
exception from a callback. This would allow *clean* exception handling, unlike 
the decoration solution suggested in the docs.

Original issue reported on code.google.com by adr...@planetcoding.net on 7 Oct 2012 at 1:17

GoogleCodeExporter commented 8 years ago
AFAICT the decorator solution is as *clean* as it gets :)

That said, I would agree that pyev's error handling might be made simpler by 
adding an error callback to the loop. But you need to understand that it will 
only move the main problem of _you_ handling errors from the watcher callback 
to the loop error callback.

What happens now during invocation of pending watchers (in pseudo python code):

try:
    watcher.callback()
except Exception as e:
    if loop.debug:
        loop.stop()
    else:
        print(e)

The solution I would propose could be expressed as:

try:
    watcher.callback()
except Exception as e:
    if loop.on_error:
        try:
            loop.on_error(e)
        except Exception as x:
            if loop.debug:
                loop.stop()
            else:
                print(x)
    else:
        if loop.debug:
            loop.stop()
        else:
            print(e)

Hmm... that might make the overall loop invocation error handling (slightly) 
slower. Well, if you have a better idea... (maybe I didn't understand what you 
meant).

lekma

Original comment by lekma...@gmail.com on 8 Oct 2012 at 10:27

GoogleCodeExporter commented 8 years ago
If the error handler raises an exception it would probably a good idea to 
terminate the loop no matter if debug mode is active or not. That usually means 
something is very wrong - and ignoring the error is not a good idea then IMO.

Another option besides the custom error handler would be using a standard 
python logger to log the exception. This would allow the exception to be logged 
properly (i.e. with a stacktrace etc.) without touching any of the loop code.

Original comment by adr...@planetcoding.net on 8 Oct 2012 at 10:30