diff --git a/src/nbmake/pytest_items.py b/src/nbmake/pytest_items.py index ed69d9b..1ea5ffc 100644 --- a/src/nbmake/pytest_items.py +++ b/src/nbmake/pytest_items.py @@ -6,6 +6,7 @@ import nbformat import pytest from _pytest._code.code import ReprFileLocation, TerminalRepr, TerminalWriter +from nbformat.reader import NotJSONError from pygments import highlight from pygments.formatters import TerminalTrueColorFormatter from pygments.lexers import Python3TracebackLexer @@ -75,7 +76,14 @@ def create_internal_err() -> Any: "NBMAKE INTERNAL ERROR", ) - if type(excinfo.value) != NotebookFailedException: + exc_type = type(excinfo.value) + + if exc_type is NotJSONError: + return NbMakeFailureRepr( + "This file is not a valid json document", excinfo.value.args[0] + ) + + if exc_type != NotebookFailedException: return create_internal_err() res: NotebookResult = excinfo.value.args[0] diff --git a/tests/test_pytest_plugin.py b/tests/test_pytest_plugin.py index ad6f363..2749b47 100644 --- a/tests/test_pytest_plugin.py +++ b/tests/test_pytest_plugin.py @@ -195,3 +195,15 @@ def test_when_no_import_errs_then_pass(pytester: Pytester, testdir2: Never): hook_recorder = pytester.inline_run("--nbmake", "--nbmake-find-import-errors") assert hook_recorder.ret == ExitCode.OK + + +def test_when_not_json_then_correct_err_msg(pytester: Pytester, testdir2: Never): + + (Path(pytester.path) / "a.ipynb").write_text("invalid json") + + hook_recorder = pytester.inline_run("--nbmake") + + longrepr = hook_recorder.listoutcomes()[2][0].longrepr + assert longrepr is not None + assert "NBMAKE INTERNAL ERROR" not in longrepr.term + assert "json" in longrepr.term