jfhbrook / pyee

A rough port of Node.js's EventEmitter to Python with a few tricks of its own
https://github.com/jfhbrook/public
MIT License
371 stars 39 forks source link

Additional error context with UnhandledError #143

Closed jfhbrook closed 12 months ago

jfhbrook commented 12 months ago

This PR creates a new error called an UnhandledError, which wraps errors going through pyee's internal error handling code. This error has additional context properties:

Making this work is a bit of a challenge. The interface for the error event handler (Callable[[Any], Any]) doesn't allow for passing extra args and kwargs, so I can't attach them to arbitrary error events. The best I can do is manually wrap raised exceptions in an UnhandledError in _call_handlers implementations. The upshot is that this is doable for exceptions raised in asyncio, trio and executors, but not in the base EventEmitter. Twisted also has some challenges, because of the way it sends failures through the "failure" event.

The twisted event emitter could be refactored to better support this context, but the base EventEmitter is DOA as far as that goes. This is probably OK, since the base EventEmitter has extremely naive error handling as compared to the other subclasses.

Now that I'm not entirely happy with this implementation:

Tests are passing, though the executor tests could use better coverage.

See also #142, which removes deprecated APIs and introduces a PyeeError - this PR is structured based on the assumption that this change goes through.

This is intended to close #102.

jfhbrook commented 12 months ago

I realized that there are actually ways to make this work - that's currently implemented. Keeping this open as a draft, cause I'm not totally happy with the implementation.

jfhbrook commented 12 months ago

As mentioned in #102, I've convinced myself this is a bad idea. C'est la vie.