Skip to content

New API for handling concurrent exceptions #1683

@bob1de

Description

@bob1de

Hi,

I wasn't sure whether this should go into the exceptiongroup repo or not, so I'm opening the issue here because I implemented and use this with MultiError in current trio. However, it can be ported to ExceptionGroup easily.

I needed to catch concurrent exceptions at various places and found it quite unergonomic to use MultiError.catch the way handlers need to be implemented right now.

So I thought back and forth about it and came up with a context manager-based approach where the code inside the context is guarded by exception handling and handlers for individual exceptions can be registered using decorators. Both sync and async handlers are supported, and it also allows registering handlers for the else and finally branches so that one can avoid another try block + the extra indentation.

The code can be found here:
https://github.com/efficiosoft/trio_catcher

Docs and some thoughts are provided in README and docstrings.

I'd be very interested in what you think about such an approach. Imho, at least it's the cleanest-looking I've found so far. :-)

Best regards
Robert

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions