diff --git a/dvc/api.py b/dvc/api.py index 9d6a321d9..85c5c0777 100644 --- a/dvc/api.py +++ b/dvc/api.py @@ -10,7 +10,7 @@ import ruamel.yaml from voluptuous import Schema, Required, Invalid from dvc.repo import Repo -from dvc.exceptions import DvcException +from dvc.exceptions import DvcException, NotDvcRepoError from dvc.external_repo import external_repo @@ -43,13 +43,27 @@ class SummonError(DvcException): pass +class UrlNotDvcRepoError(DvcException): + """Thrown if given url is not a DVC repository. + + Args: + url (str): url to the repository. + """ + + def __init__(self, url): + super().__init__("URL '{}' is not a dvc repository.".format(url)) + + def get_url(path, repo=None, rev=None, remote=None): """Returns an url of a resource specified by path in repo""" - with _make_repo(repo, rev=rev) as _repo: - abspath = os.path.join(_repo.root_dir, path) - out, = _repo.find_outs_by_path(abspath) - remote_obj = _repo.cloud.get_remote(remote) - return str(remote_obj.checksum_to_path_info(out.checksum)) + try: + with _make_repo(repo, rev=rev) as _repo: + abspath = os.path.join(_repo.root_dir, path) + out, = _repo.find_outs_by_path(abspath) + remote_obj = _repo.cloud.get_remote(remote) + return str(remote_obj.checksum_to_path_info(out.checksum)) + except NotDvcRepoError: + raise UrlNotDvcRepoError(repo) def open(path, repo=None, rev=None, remote=None, mode="r", encoding=None):