Skip to content

MultiError.catch should take an exception type, like except: does #408

@njsmith

Description

@njsmith

In regular Python, a catch-all exception handler looks like:

try:
    ...
except Exception as exc:  # Don't catch KeyboardInterrupt, etc.
    logger.error(..., exc_info=exc)

In Trio we need to handle MultiErrors, which might contain a mix of exceptions we want to catch and ones we don't, etc. So you have to use MultiError.catch, and the above becomes:

def handler(exc):
    if isinstance(exc, Exception):
        logger.error(..., exc_info=exc)
        return None  # swallow this exception
    else:
        return exc  # let other exceptions propagate

with MultiError.catch(handler):
    ...

This is pointlessly cumbersome. We should make it look like this:

def handler(exc):
    logger.error(..., exc_info=exc)

with MultiError.catch(Exception, handler):  # Note the extra argument
    ...

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions