Skip to content

Commit

Permalink
analytics: ignore request.post errors (#3292)
Browse files Browse the repository at this point in the history
Users have been complaining about dvc analytics errors when they are not
connected to the internet.
  • Loading branch information
efiop authored Feb 9, 2020
1 parent 2786a4e commit d305803
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
5 changes: 4 additions & 1 deletion dvc/analytics.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,10 @@ def send(report):
headers = {"content-type": "application/json"}

with open(report, "rb") as fobj:
requests.post(url, data=fobj, headers=headers, timeout=5)
try:
requests.post(url, data=fobj, headers=headers, timeout=5)
except requests.exceptions.RequestException:
logger.debug("failed to send analytics report", exc_info=True)

os.remove(report)

Expand Down
11 changes: 7 additions & 4 deletions tests/unit/test_analytics.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,16 +61,19 @@ def test_runtime_info(tmp_global_config):

@mock.patch("requests.post")
def test_send(mock_post, tmp_path):
import requests

url = "https://analytics.dvc.org"
report = {"name": "dummy report"}
fname = str(tmp_path / "report")
report_file = tmp_path / "report"

with open(fname, "w") as fobj:
json.dump(report, fobj)
report_file.write_text(json.dumps(report))
mock_post.side_effect = requests.exceptions.RequestException

analytics.send(fname)
analytics.send(str(report_file))
assert mock_post.called
assert mock_post.call_args.args[0] == url
assert not report_file.exists()


@pytest.mark.parametrize(
Expand Down

0 comments on commit d305803

Please sign in to comment.