diff --git a/visidata/errors.py b/visidata/errors.py index a96cc16a2..59630d308 100644 --- a/visidata/errors.py +++ b/visidata/errors.py @@ -3,6 +3,7 @@ from visidata import vd, VisiData vd.option('debug', False, 'exit on error and display stacktrace') +vd.option('debug_stacktrace_full', False, 'for exceptions, show callstack levels above the catch') class ExpectedException(Exception): @@ -11,11 +12,19 @@ class ExpectedException(Exception): def stacktrace(e=None): + '''if options.debug_stacktrace_full is True, display all callstack + levels above the level where the exception was caught, in addition + to the standard exception.''' if not e: - return traceback.format_exc().strip().splitlines() + if vd.options.debug_stacktrace_full: + trim_levels = 3 # calling function -> stacktrace() -> format_stack() + stack = ''.join(traceback.format_stack()).strip().splitlines() + trace_above = stack[:-2*trim_levels] + else: + trace_above = [] + return trace_above + traceback.format_exc().strip().splitlines() return traceback.format_exception_only(type(e), e) - @VisiData.api def exceptionCaught(vd, exc=None, status=True, **kwargs): 'Add *exc* to list of last errors and add to status history. Show on left status bar if *status* is True. Reraise exception if options.debug is True.'