MCPcopy Index your code
hub / github.com/python-cmd2/cmd2 / format_exception

Method format_exception

cmd2/cmd2.py:1643–1684  ·  view source on GitHub ↗

Format an exception for printing. If `debug` is true, a full traceback is included, if one exists. :param exception: the exception to be printed. :return: a formatted exception string

(self, exception: BaseException)

Source from the content-addressed store, hash-verified

1641 )
1642
1643 def format_exception(self, exception: BaseException) -> str:
1644 """Format an exception for printing.
1645
1646 If `debug` is true, a full traceback is included, if one exists.
1647
1648 :param exception: the exception to be printed.
1649 :return: a formatted exception string
1650 """
1651 console = Cmd2ExceptionConsole(file=sys.stderr)
1652 with console.capture() as capture:
1653 # Only print a traceback if we're in debug mode and one exists.
1654 if self.debug and sys.exc_info() != (None, None, None):
1655 traceback = Traceback(**self.traceback_kwargs)
1656 console.print(traceback, end="")
1657
1658 else:
1659 # Print the exception in the same style Rich uses after a traceback.
1660 exception_str = str(exception)
1661
1662 if exception_str:
1663 highlighter = ReprHighlighter()
1664
1665 final_msg = Text.assemble(
1666 (f"{type(exception).__name__}: ", "traceback.exc_type"),
1667 highlighter(exception_str),
1668 )
1669 else:
1670 final_msg = Text(f"{type(exception).__name__}", style="traceback.exc_type")
1671
1672 # If not in debug mode and the 'debug' setting is available,
1673 # inform the user how to enable full tracebacks.
1674 if not self.debug and "debug" in self.settables:
1675 help_msg = Text.assemble(
1676 "\n\n",
1677 ("To enable full traceback, run the following command: ", Cmd2Style.WARNING),
1678 ("set debug true", Cmd2Style.COMMAND_LINE),
1679 )
1680 final_msg.append(help_msg)
1681
1682 console.print(final_msg)
1683
1684 return capture.get()
1685
1686 def pexcept(
1687 self,

Callers 2

pexceptMethod · 0.95
completeMethod · 0.95

Calls 4

printMethod · 0.80
appendMethod · 0.80
getMethod · 0.45

Tested by

no test coverage detected