From 57dc36a01b2b334a996f73f6a78b3bfbe4d9f2ec Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Sat, 29 Feb 2020 14:40:45 -0500 Subject: Ensure all nested exception throws have a cause Applied an explicit "cause" to most if not all internally raised exceptions that are raised from within an internal exception catch, to avoid misleading stacktraces that suggest an error within the handling of an exception. While it would be preferable to suppress the internally caught exception in the way that the ``__suppress_context__`` attribute would, there does not as yet seem to be a way to do this without suppressing an enclosing user constructed context, so for now it exposes the internally caught exception as the cause so that full information about the context of the error is maintained. Fixes: #4849 Change-Id: I55a86b29023675d9e5e49bc7edc5a2dc0bcd4751 --- lib/sqlalchemy/sql/compiler.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) (limited to 'lib/sqlalchemy/sql/compiler.py') diff --git a/lib/sqlalchemy/sql/compiler.py b/lib/sqlalchemy/sql/compiler.py index 9c1f50ce1..d31cf67f8 100644 --- a/lib/sqlalchemy/sql/compiler.py +++ b/lib/sqlalchemy/sql/compiler.py @@ -1074,7 +1074,7 @@ class SQLCompiler(Compiled): col = only_froms[element.element] else: col = with_cols[element.element] - except KeyError: + except KeyError as err: coercions._no_text_coercion( element.element, extra=( @@ -1082,6 +1082,7 @@ class SQLCompiler(Compiled): "GROUP BY / DISTINCT etc." ), exc_cls=exc.CompileError, + err=err, ) else: kwargs["render_label_as_label"] = col @@ -1671,8 +1672,11 @@ class SQLCompiler(Compiled): else: try: opstring = OPERATORS[operator_] - except KeyError: - raise exc.UnsupportedCompilationError(self, operator_) + except KeyError as err: + util.raise_( + exc.UnsupportedCompilationError(self, operator_), + replace_context=err, + ) else: return self._generate_generic_binary( binary, opstring, from_linter=from_linter, **kw @@ -3286,11 +3290,12 @@ class DDLCompiler(Compiled): if column.primary_key: first_pk = True except exc.CompileError as ce: - util.raise_from_cause( + util.raise_( exc.CompileError( util.u("(in table '%s', column '%s'): %s") % (table.description, column.name, ce.args[0]) - ) + ), + from_=ce, ) const = self.create_table_constraints( -- cgit v1.2.1