From 086d51d9de53e7b85f0236b148fb7c127612c088 Mon Sep 17 00:00:00 2001 From: skshetry <18718008+skshetry@users.noreply.github.com> Date: Fri, 31 Mar 2023 16:34:26 +0545 Subject: [PATCH] plots json: return definition errors unrelated to renderers (#9293) --- dvc/commands/plots.py | 31 ++++++++++++++++++++++--------- tests/unit/command/test_plots.py | 25 ++++++++++++++++++++++++- 2 files changed, 46 insertions(+), 10 deletions(-) diff --git a/dvc/commands/plots.py b/dvc/commands/plots.py index 9382ce9c2..b114ecf43 100644 --- a/dvc/commands/plots.py +++ b/dvc/commands/plots.py @@ -1,9 +1,9 @@ import argparse import logging import os -from typing import TYPE_CHECKING, Dict, List +from typing import TYPE_CHECKING, Dict, List, Optional -from funcy import compact, first +from funcy import compact, first, get_in from dvc.cli import completion from dvc.cli.command import CmdBase @@ -19,17 +19,21 @@ if TYPE_CHECKING: logger = logging.getLogger(__name__) -def _show_json(renderers_with_errors: List["RendererWithErrors"], split=False): +def _show_json( + renderers_with_errors: List["RendererWithErrors"], + split=False, + errors: Optional[Dict[str, Exception]] = None, +): from dvc.render.convert import to_json from dvc.utils.serialize import encode_exception - errors: List[Dict] = [] + all_errors: List[Dict] = [] data = {} for renderer, src_errors, def_errors in renderers_with_errors: name = renderer.name data[name] = to_json(renderer, split) - errors.extend( + all_errors.extend( { "name": name, "rev": rev, @@ -39,7 +43,7 @@ def _show_json(renderers_with_errors: List["RendererWithErrors"], split=False): for rev, per_rev_src_errors in src_errors.items() for source, e in per_rev_src_errors.items() ) - errors.extend( + all_errors.extend( { "name": name, "rev": rev, @@ -48,7 +52,11 @@ def _show_json(renderers_with_errors: List["RendererWithErrors"], split=False): for rev, e in def_errors.items() ) - ui.write_json(compact({"errors": errors, "data": data}), highlight=False) + # these errors are not tied to any renderers + errors = errors or {} + all_errors.extend({"rev": rev, **encode_exception(e)} for rev, e in errors.items()) + + ui.write_json(compact({"errors": all_errors, "data": data}), highlight=False) def _adjust_vega_renderers(renderers): @@ -151,14 +159,19 @@ class CmdPlots(CmdBase): ) renderers_out = out if self.args.json else os.path.join(out, "static") - renderers_with_errors = match_defs_renderers( data=plots_data, out=renderers_out, templates_dir=self.repo.plots.templates_dir, ) if self.args.json: - _show_json(renderers_with_errors, self.args.split) + errors = compact( + { + rev: get_in(data, ["definitions", "error"]) + for rev, data in plots_data.items() + } + ) + _show_json(renderers_with_errors, self.args.split, errors=errors) return 0 renderers = [r.renderer for r in renderers_with_errors] diff --git a/tests/unit/command/test_plots.py b/tests/unit/command/test_plots.py index 1c9e5d461..ed9e0775d 100644 --- a/tests/unit/command/test_plots.py +++ b/tests/unit/command/test_plots.py @@ -4,10 +4,12 @@ import posixpath from pathlib import Path import pytest +from funcy import set_in from dvc.cli import parse_args from dvc.commands.plots import CmdPlotsDiff, CmdPlotsShow, CmdPlotsTemplates from dvc.render.match import RendererWithErrors +from dvc.utils.serialize import YAMLFileCorruptedError @pytest.fixture @@ -316,7 +318,7 @@ def test_plots_diff_json(dvc, mocker, capsys): assert cmd.run() == 0 - show_json_mock.assert_called_once_with(renderers, True) + show_json_mock.assert_called_once_with(renderers, True, errors={}) render_mock.assert_not_called() @@ -379,3 +381,24 @@ def test_show_json_no_renderers(capsys): out, _ = capsys.readouterr() assert json.dumps({}) in out + + +def test_show_json_with_error(dvc, mocker, capsys): + cli_args = parse_args(["plots", "show", "--json"]) + cmd = cli_args.func(cli_args) + + e = YAMLFileCorruptedError("dvc.yaml") + data = set_in({}, ["workspace", "definitions", "error"], e) + cmd._func = mocker.MagicMock(return_value=data) + + cmd.run() + out, _ = capsys.readouterr() + assert json.loads(out) == { + "errors": [ + { + "rev": "workspace", + "type": type(e).__name__, + "msg": e.args[0], + } + ] + }