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/base.py | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) (limited to 'lib/sqlalchemy/sql/base.py') diff --git a/lib/sqlalchemy/sql/base.py b/lib/sqlalchemy/sql/base.py index a7324c45f..2d336360f 100644 --- a/lib/sqlalchemy/sql/base.py +++ b/lib/sqlalchemy/sql/base.py @@ -128,8 +128,8 @@ class _DialectArgView(util.collections_abc.MutableMapping): def _key(self, key): try: dialect, value_key = key.split("_", 1) - except ValueError: - raise KeyError(key) + except ValueError as err: + util.raise_(KeyError(key), replace_context=err) else: return dialect, value_key @@ -138,17 +138,20 @@ class _DialectArgView(util.collections_abc.MutableMapping): try: opt = self.obj.dialect_options[dialect] - except exc.NoSuchModuleError: - raise KeyError(key) + except exc.NoSuchModuleError as err: + util.raise_(KeyError(key), replace_context=err) else: return opt[value_key] def __setitem__(self, key, value): try: dialect, value_key = self._key(key) - except KeyError: - raise exc.ArgumentError( - "Keys must be of the form _" + except KeyError as err: + util.raise_( + exc.ArgumentError( + "Keys must be of the form _" + ), + replace_context=err, ) else: self.obj.dialect_options[dialect][value_key] = value @@ -634,17 +637,17 @@ class ColumnCollection(object): def __getitem__(self, key): try: return self._index[key] - except KeyError: + except KeyError as err: if isinstance(key, util.int_types): - raise IndexError(key) + util.raise_(IndexError(key), replace_context=err) else: raise def __getattr__(self, key): try: return self._index[key] - except KeyError: - raise AttributeError(key) + except KeyError as err: + util.raise_(AttributeError(key), replace_context=err) def __contains__(self, key): if key not in self._index: -- cgit v1.2.1