python-trio / trio-websocket

WebSocket client and server implementation for Python Trio
MIT License
70 stars 26 forks source link

Hide MultiErrors #132

Closed mehaase closed 1 week ago

mehaase commented 4 years ago

Several of the APIs in this library create a "hidden" nursery that runs the connection's background task. This has the surprising side effect that something that looks like single-task code can actually raise a MultiError. Nathaniel has a good thread describing this. I'll just steal a snippet to illustrate the problem here:

async with open_websocket("https://...") as ws:
    raise ValueError

but if open_websocket has a nursery hidden inside it, then you'll get a MultiError([ValueError]) instead of a ValueError, and that's going to surprise people, because it's mostly an implementation detail that open_websocket creates a nursery.

The proposed solution is to design the library to avoid raising exceptions on the background task. This would allow us to catch MultiError in the hidden nursery, unwrap the nested exception, and raise it to the caller. If the background task does raise an error, then we wrap* the entire MultiError inside of a new class TrioWebsocketInternalError. This wrapper class is a signal to the caller that the bug is in this library, not in their code.

*Nathaniel's post suggests that the internal error either is-a or has-a MultiError. We can also look at TrioInternalError for inspiration.