pygx.error_handling¶
Exception classification and contextual error capture for PyGX.
error_handling
¶
Exception classification and contextual error capture for PyGX.
pygx.error_handling bundles the small set of error-flow primitives PyGX
uses internally and exposes to callers:
catch_errors/CatchErrorsContextcontextually suppress and aggregate matching exceptions.match_errortests whether an exception matches a(type, message_pattern)selector.ErrorInfocaptures a structured, JSON-serializable view of an exception chain — apg.Objectso it serializes and renders cleanly.
ErrorInfo
¶
Bases: Object, Extension
Structured, JSON-serializable view of an exception chain.
Source code in pygx/error_handling/_error_info.py
CatchErrorsContext
dataclass
¶
Context for pg.catch_errors.
catch_errors
¶
catch_errors(
errors: (
type[BaseException]
| tuple[type[BaseException], str]
| Sequence[type[BaseException] | tuple[type[BaseException], str]]
),
error_handler: Callable[[BaseException], None] | None = None,
) -> Iterator[CatchErrorsContext]
Context manager for catching user-specified exceptions.
Examples::
with pg.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:
| Name | Type | Description | Default |
|---|---|---|---|
errors
|
type[BaseException] | tuple[type[BaseException], str] | Sequence[type[BaseException] | tuple[type[BaseException], str]]
|
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. |
required |
error_handler
|
Callable[[BaseException], None] | None
|
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 |
None
|
Yields:
| Type | Description |
|---|---|
CatchErrorsContext
|
A CatchErrorsContext object. |
Source code in pygx/error_handling/_error_utils.py
match_error
¶
match_error(
error: BaseException,
errors: (
type[BaseException]
| tuple[type[BaseException], str]
| Sequence[type[BaseException] | tuple[type[BaseException], str]]
| dict[type[BaseException], list[str]]
),
) -> bool
Returns True if the error matches the specification, .
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
error
|
BaseException
|
The error to match. |
required |
errors
|
type[BaseException] | tuple[type[BaseException], str] | Sequence[type[BaseException] | tuple[type[BaseException], str]] | dict[type[BaseException], list[str]]
|
A sequence of exception types or tuples of exception type and error messages (described in regular expression) as the desired exception types to match. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if the error matches the specification, False otherwise. |
Source code in pygx/error_handling/_error_utils.py
options: show_root_heading: false show_root_toc_entry: false members_order: source heading_level: 2