On Thu, Feb 6, 2014 at 8:53 AM, Yury Selivanov <[email protected]>wrote:

> As discussed with Guido in issue #80
> (https://code.google.com/p/tulip/issues/detail?id=80),
> I'm proposing a strawman design for having a user definable event loop
> handler for unhandled exceptions.
>
> Two new API points:
>
>   - "loop.set_exception_handler(callback)"
>     Sets 'callback' as a new unhandled error handler for the event loop
>     'loop'. The signature of callback should be '(loop, exception,
> context)'
>     where: 'loop' parameter is the current event loop running; 'exception'
>     is the unhandled exception object; 'context' is a string (str),
>     that will be the basic context information, like
>     "unhandled exception in add_reader callback" (the kind of information
>     that usually goes to the log title).
>

I'd rename the context arg to 'message', then you don't have to explain
what it is. :-)

Also I'd steer clear from calling the handler 'callback' -- maybe just name
it 'exception_handler'?


>     It's OK to call 'loop.stop()' or 'loop.call_*' methods from the
> handler.
>

Cool.


>     If a user set handler raises an exception, that exception will be
>     handled by the default event loop exception handler (that will
>     simply log it)
>

Nice.


>   - "loop.restore_default_exception_handler()"
>     Restores exception handler to the default one.
>     Default exception handler just logs the error with
>     'loop.logger.exception()'
>

Maybe instead of having a separate API for this rarely-used action, this
could be spelled as loop.set_exception_handler(None)?

A few questions:

- Do we need an API to ask the loop for its current exception handler? If
so, what should it return if no handler is set explicitly?

- Should we allow multiple exception handlers, or just one? (I'd prefer
just one.)

- Should we have a way to invoke the default handler explicitly? E.g. if
the user's handler doesn't want to do anything special, they could pass it
to the default handler instead of having to figure out how the default
handler logs things.

- Should we have a way to invoke the current handler explicitly? E.g. if
there's user code that has encountered an exception and it wants that
exception to be treated the same as the loop handles other exceptions.

-- 
--Guido van Rossum (python.org/~guido)

Reply via email to