plots json: return definition errors unrelated to renderers (#9293)

This commit is contained in:
skshetry
2023-03-31 16:34:26 +05:45
committed by GitHub
parent 1d8b2e5fa6
commit 086d51d9de
2 changed files with 46 additions and 10 deletions
+22 -9
View File
@@ -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]
+24 -1
View File
@@ -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],
}
]
}