Repository: iterative/dvc

Issue:
daemon: Throws "Unexpected error" when it's not possible to connect to the API
I'm not sure about the behavior on this one, but is a little bit confusing that you are running other commands and then an error shows on your terminal related to a connection error that you didn't trigger explicitly. (e.g. `dvc init`).

In my opinion, it should fail and die silently.

To reproduce it, disable your Wi-Fi connection (e.g. `systemctl stop wpa_supplicant`) and run `dvc init`

Traceback:
```
  File "/home/mroutis/src/iterative/dvc/.venv/lib/python3.7/site-packages/urllib3/connectionpool.py", line 384, in _make_request
    six.raise_from(e, None)
  File "<string>", line 2, in raise_from
  File "/home/mroutis/src/iterative/dvc/.venv/lib/python3.7/site-packages/urllib3/connectionpool.py", line 380, in _make_request
    httplib_response = conn.getresponse()
  File "/usr/lib/python3.7/http/client.py", line 1321, in getresponse
    response.begin()
  File "/usr/lib/python3.7/http/client.py", line 296, in begin
    version, status, reason = self._read_status()
  File "/usr/lib/python3.7/http/client.py", line 257, in _read_status
    line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1")
  File "/usr/lib/python3.7/socket.py", line 589, in readinto
    return self._sock.recv_into(b)
  File "/usr/lib/python3.7/ssl.py", line 1052, in recv_into
    return self.read(nbytes, buffer)
  File "/usr/lib/python3.7/ssl.py", line 911, in read
    return self._sslobj.read(len, buffer)
socket.timeout: The read operation timed out

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/mroutis/src/iterative/dvc/.venv/lib/python3.7/site-packages/requests/adapters.py", line 449, in send
    timeout=timeout
  File "/home/mroutis/src/iterative/dvc/.venv/lib/python3.7/site-packages/urllib3/connectionpool.py", line 638, in urlopen
    _stacktrace=sys.exc_info()[2])
  File "/home/mroutis/src/iterative/dvc/.venv/lib/python3.7/site-packages/urllib3/util/retry.py", line 367, in increment
    raise six.reraise(type(error), error, _stacktrace)
  File "/home/mroutis/src/iterative/dvc/.venv/lib/python3.7/site-packages/urllib3/packages/six.py", line 686, in reraise
    raise value
  File "/home/mroutis/src/iterative/dvc/.venv/lib/python3.7/site-packages/urllib3/connectionpool.py", line 600, in urlopen
    chunked=chunked)
  File "/home/mroutis/src/iterative/dvc/.venv/lib/python3.7/site-packages/urllib3/connectionpool.py", line 386, in _make_request
    self._raise_timeout(err=e, url=url, timeout_value=read_timeout)
  File "/home/mroutis/src/iterative/dvc/.venv/lib/python3.7/site-packages/urllib3/connectionpool.py", line 306, in _raise_timeout
    raise ReadTimeoutError(self, url, "Read timed out. (read timeout=%s)" % timeout_value)
urllib3.exceptions.ReadTimeoutError: HTTPSConnectionPool(host='4ki8820rsf.execute-api.us-east-2.amazonaws.com', port=443): Read timed out. (read timeout=10)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/mroutis/src/iterative/dvc/dvc/main.py", line 22, in main
    ret = cmd.run_cmd()
  File "/home/mroutis/src/iterative/dvc/dvc/command/daemon.py", line 9, in run_cmd
    return self.run()
  File "/home/mroutis/src/iterative/dvc/dvc/command/daemon.py", line 19, in run
    updater.fetch(detach=False)
  File "/home/mroutis/src/iterative/dvc/dvc/updater.py", line 70, in fetch
    self._get_latest_version()
  File "/home/mroutis/src/iterative/dvc/dvc/updater.py", line 75, in _get_latest_version
    r = requests.get(self.URL, timeout=self.TIMEOUT_GET)
  File "/home/mroutis/src/iterative/dvc/.venv/lib/python3.7/site-packages/requests/api.py", line 75, in get
    return request('get', url, params=params, **kwargs)
  File "/home/mroutis/src/iterative/dvc/.venv/lib/python3.7/site-packages/requests/api.py", line 60, in request
    return session.request(method=method, url=url, **kwargs)
  File "/home/mroutis/src/iterative/dvc/.venv/lib/python3.7/site-packages/requests/sessions.py", line 524, in request
    resp = self.send(prep, **send_kwargs)
  File "/home/mroutis/src/iterative/dvc/.venv/lib/python3.7/site-packages/requests/sessions.py", line 637, in send
    r = adapter.send(request, **kwargs)
  File "/home/mroutis/src/iterative/dvc/.venv/lib/python3.7/site-packages/requests/adapters.py", line 529, in send
    raise ReadTimeout(e, request=request)
requests.exceptions.ReadTimeout: HTTPSConnectionPool(host='4ki8820rsf.execute-api.us-east-2.amazonaws.com', port=443): Read timed out. (read timeout=10)

Error: Unexpected error: HTTPSConnectionPool(host='4ki8820rsf.execute-api.us-east-2.amazonaws.com', port=443): Read timed out. (read timeout=10)
```


Discussion:

Resolving patch:
diff --git a/dvc/daemon.py b/dvc/daemon.py
--- a/dvc/daemon.py
+++ b/dvc/daemon.py
@@ -48,7 +48,7 @@ def __call__(self, name):
         cmd = [sys.executable]
         if not is_binary():
             cmd += ['-m', 'dvc']
-        cmd += ['daemon', name, '-v']
+        cmd += ['daemon', name, '-q']
 
         Logger.debug("Trying to spawn '{}'".format(cmd))
 
diff --git a/dvc/updater.py b/dvc/updater.py
--- a/dvc/updater.py
+++ b/dvc/updater.py
@@ -72,8 +72,14 @@ def fetch(self, detach=True):
     def _get_latest_version(self):
         import json
 
-        r = requests.get(self.URL, timeout=self.TIMEOUT_GET)
-        info = r.json()
+        try:
+            r = requests.get(self.URL, timeout=self.TIMEOUT_GET)
+            info = r.json()
+        except requests.exceptions.RequestException as exc:
+            msg = "Failed to retrieve latest version: {}"
+            Logger.debug(msg.format(exc))
+            return
+
         with open(self.updater_file, 'w+') as fobj:
             json.dump(info, fobj)
 


Test patch: