summaryrefslogtreecommitdiff
path: root/cmd2
diff options
context:
space:
mode:
authorKevin Van Brunt <kmvanbrunt@gmail.com>2021-03-02 10:26:45 -0500
committerKevin Van Brunt <kmvanbrunt@gmail.com>2021-03-02 10:26:45 -0500
commit7024aa0e705e522e8e0cc9b4555bb17a5a35ed45 (patch)
treef27278d32dbc5d9ee19dbb2f5dd929b943df4789 /cmd2
parent6b14aa6579d9c5bdd32bf8c3d67f11db8b575bb0 (diff)
downloadcmd2-git-exception_passthrough.tar.gz
Added cmd2.exceptions.PassThroughExceptionexception_passthrough
Diffstat (limited to 'cmd2')
-rw-r--r--cmd2/cmd2.py5
-rw-r--r--cmd2/exceptions.py15
2 files changed, 20 insertions, 0 deletions
diff --git a/cmd2/cmd2.py b/cmd2/cmd2.py
index 0b198111..d01dfaa0 100644
--- a/cmd2/cmd2.py
+++ b/cmd2/cmd2.py
@@ -95,6 +95,7 @@ from .exceptions import (
CompletionError,
EmbeddedConsoleExit,
EmptyStatement,
+ PassThroughException,
RedirectionError,
SkipPostcommandHooks,
)
@@ -2259,6 +2260,8 @@ class Cmd(cmd.Cmd):
raise ex
except SystemExit:
stop = True
+ except PassThroughException as ex:
+ raise ex.wrapped_ex
except Exception as ex:
self.pexcept(ex)
finally:
@@ -2269,6 +2272,8 @@ class Cmd(cmd.Cmd):
raise ex
except SystemExit:
stop = True
+ except PassThroughException as ex:
+ raise ex.wrapped_ex
except Exception as ex:
self.pexcept(ex)
diff --git a/cmd2/exceptions.py b/cmd2/exceptions.py
index 39b7e333..b45167d1 100644
--- a/cmd2/exceptions.py
+++ b/cmd2/exceptions.py
@@ -62,6 +62,21 @@ class CompletionError(Exception):
super().__init__(*args)
+class PassThroughException(Exception):
+ """
+ Normally all unhandled exceptions raised during commands get printed to the user.
+ This class is used to wrap an exception that should be raised instead of printed.
+ """
+
+ def __init__(self, *args, wrapped_ex: BaseException):
+ """
+ Initializer for PassThroughException
+ :param wrapped_ex: the exception that will be raised
+ """
+ self.wrapped_ex = wrapped_ex
+ super().__init__(*args)
+
+
############################################################################################################
# The following exceptions are NOT part of the public API and are intended for internal use only.
############################################################################################################