diff --git a/dvc/command/metrics.py b/dvc/command/metrics.py index 748b3bff7..73bd9ae9f 100644 --- a/dvc/command/metrics.py +++ b/dvc/command/metrics.py @@ -2,6 +2,7 @@ from __future__ import unicode_literals import argparse import logging +import itertools from dvc.exceptions import DvcException from dvc.command.base import CmdBase, fix_subparsers, append_doc_link @@ -46,6 +47,24 @@ class CmdMetricsShow(CmdBase): ) show_metrics(metrics, self.args.all_branches, self.args.all_tags) + + found = set( + itertools.chain.from_iterable( + metric.keys() for metric in metrics.values() + ) + ) + + missing = set(self.args.targets) - found + + if missing: + logger.error( + "the following metrics do not exists, " + "are not metric files or are malformed: {paths}".format( + paths=", ".join("'{}'".format(x) for x in missing) + ) + ) + return 1 + except DvcException: logger.exception("failed to show metrics") return 1 diff --git a/dvc/exceptions.py b/dvc/exceptions.py index 508b1fb05..e8fd2df29 100644 --- a/dvc/exceptions.py +++ b/dvc/exceptions.py @@ -195,16 +195,6 @@ class ReproductionError(DvcException): super(ReproductionError, self).__init__(msg, cause=ex) -class BadMetricError(DvcException): - def __init__(self, paths): - super(BadMetricError, self).__init__( - "the following outputs do not exist, are not a metric file " - "or are malformed: {paths}".format( - paths=", ".join(relpath(path) for path in paths) - ) - ) - - class NoMetricsError(DvcException): def __init__(self): super(NoMetricsError, self).__init__( diff --git a/dvc/repo/metrics/show.py b/dvc/repo/metrics/show.py index b7c2645bb..0facfa8ae 100644 --- a/dvc/repo/metrics/show.py +++ b/dvc/repo/metrics/show.py @@ -8,7 +8,7 @@ import logging from jsonpath_ng.ext import parse -from dvc.exceptions import OutputNotFoundError, BadMetricError, NoMetricsError +from dvc.exceptions import OutputNotFoundError, NoMetricsError from dvc.utils.compat import builtin_str, open, StringIO, csv_reader NO_METRICS_FILE_AT_REFERENCE_WARNING = ( @@ -282,9 +282,7 @@ def show( if metrics: res[branch] = metrics - if not res: - if targets: - raise BadMetricError(targets) + if not res and not targets: raise NoMetricsError() return res diff --git a/tests/func/test_metrics.py b/tests/func/test_metrics.py index a452dabe9..46440ffdf 100644 --- a/tests/func/test_metrics.py +++ b/tests/func/test_metrics.py @@ -7,7 +7,7 @@ import logging from dvc.repo import Repo as DvcRepo from dvc.main import main -from dvc.exceptions import DvcException, BadMetricError, NoMetricsError +from dvc.exceptions import DvcException, NoMetricsError from dvc.repo.metrics.show import NO_METRICS_FILE_AT_REFERENCE_WARNING from dvc.stage import Stage from dvc.utils import relpath @@ -412,10 +412,10 @@ class TestMetricsRecursive(TestDvcGit): self.dvc.scm.checkout("master") def test(self): - with self.assertRaises(BadMetricError): - self.dvc.metrics.show( - ["nested"], all_branches=True, recursive=False - ) + ret = self.dvc.metrics.show( + ["nested"], all_branches=True, recursive=False + ) + self.assertEqual(len(ret), 0) ret = self.dvc.metrics.show( ["nested"], all_branches=True, recursive=True