This is just an idea - I haven't tested it at all - but perhaps a context manager can do what you want. For example, if you want a particular query to raise the original exception, then use with raise_orig(): query().
from contextlib import contextmanager
from sqlalchemy.exc import StatementError
@contextmanager
def raise_orig():
try:
yield
except StatementError as stmt_exc:
raise stmt_exc.orig
def query():
raise StatementError(
message="Message from SQLAlchemy",
statement="SELECT 1",
params=(),
orig=RuntimeError("The original exception instance"),
)
if __name__ == "__main__":
with raise_orig():
query()
here is the traceback that this raises:
Traceback (most recent call last):
File "raise_orig.py", line 7, in raise_orig
yield
File "raise_orig.py", line 23, in <module>
query()
File "raise_orig.py", line 17, in query
orig=RuntimeError("The original exception instance"),
sqlalchemy.exc.StatementError: Message from SQLAlchemy
[SQL: SELECT 1]
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "raise_orig.py", line 23, in <module>
query()
File "C:\Users\peter_000\AppData\Local\Programs\Python\Python36\Lib\contextlib.py", line 99, in __exit__
self.gen.throw(type, value, traceback)
File "raise_orig.py", line 9, in raise_orig
raise stmt_exc.orig
RuntimeError: The original exception instance
I chose to test for StatementError as that is the most primary exception type that has the orig attribute.
Not perfect as the tracebacks will be even more complicated than they already are, but should at least allow you to catch the specific type.
You could also just use the context manager pattern to abstract away the exception type checking and handling, so you don't need to repeat it everywhere (then it only needs to look ugly in one place!):
@contextmanager
def handle_orig():
try:
yield
except StatementError as stmt_exc:
if isinstance(stmt_exc.orig, SomeError):
do_this()
elif isinstance(stmt_exc.orig, SomeOtherError):
do_that()
...
raise # or return True to suppress exc.
with handle_orig():
query()