Catch/handle error from __repr__
Opened this issue · 3 comments
blueyed commented
repr()
might trigger database queries in Django, and then e.g. pytest-django
might throw an exception that disallows this.
Therefore when using better-exceptions from a debugger it might crash it there.
I think the call to repr
should be protected against this.
.venv/lib/python3.7/site-packages/pdb.py:1325: in _format_extra_exception
fmt_exc = list(better_formatter.format_exception(etype, evalue, tb))
../../Vcs/better-exceptions/better_exceptions/formatter.py:322: in format_exception
for line in self._format_exception(value, tb):
../../Vcs/better-exceptions/better_exceptions/formatter.py:311: in _format_exception
formatted, colored_source = self.format_traceback(exc_traceback)
../../Vcs/better-exceptions/better_exceptions/formatter.py:274: in format_traceback
formatted, colored = self.format_traceback_frame(tb)
../../Vcs/better-exceptions/better_exceptions/formatter.py:234: in format_traceback_frame
filename, lineno, function, source, color_source, relevant_values = self.get_traceback_information(tb)
../../Vcs/better-exceptions/better_exceptions/formatter.py:227: in get_traceback_information
relevant_values = self.get_relevant_values(source, frame, tree)
../../Vcs/better-exceptions/better_exceptions/formatter.py:135: in get_relevant_values
values.append((text, col, self.format_value(val)))
../../Vcs/better-exceptions/better_exceptions/formatter.py:117: in format_value
v = repr(v)
../../Vcs/django/django/db/models/query.py:250: in __repr__
data = list(self[:REPR_OUTPUT_SIZE + 1])
../../Vcs/django/django/db/models/query.py:274: in __iter__
self._fetch_all()
../../Vcs/django/django/db/models/query.py:1242: in _fetch_all
self._result_cache = list(self._iterable_class(self))
../../Vcs/django/django/db/models/query.py:55: in __iter__
results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)
../../Vcs/django/django/db/models/sql/compiler.py:1098: in execute_sql
cursor = self.connection.cursor()
../../Vcs/django/django/db/backends/base/base.py:260: in cursor
return self._cursor()
../../Vcs/django/django/db/backends/base/base.py:235: in _cursor
self.ensure_connection()
E Failed: Database access not allowed, use the "django_db" mark, or the "db" or "transactional_db" fixtures to enable it.
Delgan commented
Good catch!
I think this is implemented but not yet released.
Qix- commented
I can do a release sometime today :)
blueyed commented
Note that pytest's Fail
exception is derived from BaseException
, so would not be caught there.