pg.catch_errors

Accessible via pg.catch_errors, pg.object_utils.catch_errors.

catch_errors(errors, error_handler=None)

Context manager for catching user-specified exceptions.

Examples:

with pg.object_utils.catch_errors(
    [
        RuntimeErrror,
        (ValueError, 'Input is wrong.')
    ],
) as error_context:
  do_something()

if error_context.error:
  # Error branch.
  handle_error(error_context.error)
Parameters:
  • errors – A sequence of exception types or tuples of exception type and error messages (described in regular expression) as the desired exception types to catch. If an error is raised within the scope which does not match with the specification, it will be propagated to the outer scope.

  • error_handler – An optional callable object to handle the error on failure. It’s usually provided if the user want to create a context manager based on pg.catch_errors with specific error handling logics.

Yields:

A CatchErrorsContext object.