Skip to content

Commit

Permalink
ui: don't show exception for "Too many files" error
Browse files Browse the repository at this point in the history
iterative/dvc.org#875 (comment)

Stops showing the exception in non-verbose mode, but shows full
traceback in verbose mode.

Introducing our custom parameter `tb_only` through standard `extra`
logging parameter.
  • Loading branch information
efiop committed Jan 5, 2020
1 parent 54faf1b commit 6411a3b
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 6 deletions.
12 changes: 7 additions & 5 deletions dvc/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def format(self, record):
return record.msg

if record.levelname == "ERROR" or record.levelname == "CRITICAL":
exception, stack_trace = self._parse_exc(record.exc_info)
exception, stack_trace = self._parse_exc(record)

return (
"{color}{levelname}{nc}: {description}" "{stack_trace}\n"
Expand Down Expand Up @@ -122,13 +122,15 @@ def _walk_exc(self, exc_info):

return exc_list, tb_list

def _parse_exc(self, exc_info):
if not exc_info:
def _parse_exc(self, record):
tb_only = getattr(record, "tb_only", False)

if not record.exc_info:
return (None, "")

exc_list, tb_list = self._walk_exc(exc_info)
exc_list, tb_list = self._walk_exc(record.exc_info)

exception = ": ".join(exc_list)
exception = None if tb_only else ": ".join(exc_list)

if self._current_level() == logging.DEBUG:
stack_trace = (
Expand Down
3 changes: 2 additions & 1 deletion dvc/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ def main(argv=None):
except Exception as exc: # pylint: disable=broad-except
if isinstance(exc, OSError) and exc.errno == errno.EMFILE:
logger.exception(
"too many open files, please increase your `ulimit`"
"too many open files, please increase your `ulimit`",
extra={"tb_only": True},
)
else:
logger.exception("unexpected error")
Expand Down
19 changes: 19 additions & 0 deletions tests/unit/test_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,25 @@ def test_exception_under_verbose(self, caplog):

assert expected == formatter.format(caplog.records[0])

def test_tb_only(self, caplog):
with caplog.at_level(logging.DEBUG, logger="dvc"):
try:
raise Exception("description")
except Exception:
stack_trace = traceback.format_exc()
logger.exception("something", extra={"tb_only": True})

expected = (
"{red}ERROR{nc}: something\n"
"{red}{line}{nc}\n"
"{stack_trace}"
"{red}{line}{nc}\n".format(
line="-" * 60, stack_trace=stack_trace, **colors
)
)

assert expected == formatter.format(caplog.records[0])

def test_nested_exceptions(self, caplog):
with caplog.at_level(logging.DEBUG, logger="dvc"):
try:
Expand Down

0 comments on commit 6411a3b

Please sign in to comment.