Error_Handling Module
Error handling for expected / unexpected exceptions.
The default handler will…
Call
print()
after catching aKeyboardInterrupt
, before exitingExit gracefully after catching a
BrokenPipeError
(often caused by piping output to a tool liketail
)
Note
Parameters defined in a base Command will be processed in the context of that Command. I.e., if a valid subcommand argument was provided, but an Option defined in the parent Command has an invalid value, then the exception that is raised about that invalid value will be raised before transferring control to the subcommand’s error handler.
- author:
Doug Skrypa
- class cli_command_parser.error_handling.ErrorHandler[source]
Bases:
object
- exc_handler_map: dict[Type[BaseException], Handler]
- register(handler: Callable[[BaseException], bool | None], *exceptions: Type[BaseException])[source]
- unregister(*exceptions: Type[BaseException])[source]
- __call__(*exceptions: Type[BaseException])[source]
- classmethod cls_handler(*exceptions: Type[BaseException])[source]
- iter_handlers(exc_type: Type[BaseException], exc: BaseException) Iterator[Callable[[BaseException], bool | None]] [source]
- copy() ErrorHandler [source]
- class cli_command_parser.error_handling.Handler(exc_cls: Type[BaseException], handler: Callable[[BaseException], bool | None])[source]
Bases:
object
Wrapper around an exception class and the handler for it to facilitate sorting to select the most specific handler for a given exception.
- exc_cls
- handler
- cli_command_parser.error_handling.error_handler: ErrorHandler = <ErrorHandler[handlers=2]>
Default base
ErrorHandler
- cli_command_parser.error_handling.handle_kb_interrupt(exc: KeyboardInterrupt) int [source]
Handles
KeyboardInterrupt
by callingprint()
to avoid ending the program in a way that causes the next terminal prompt to be printed on the same line as the last (possibly incomplete) line of output.
- cli_command_parser.error_handling.no_exit_handler: ErrorHandler = <ErrorHandler[handlers=3]>
An
ErrorHandler
that does not callsys.exit()
forCommandParserExceptions
- cli_command_parser.error_handling.extended_error_handler: ErrorHandler = <ErrorHandler[handlers=2]>
The default
ErrorHandler
(extendserror_handler
)