diff --git a/dvc/analytics.py b/dvc/analytics.py index 7c0a693105..76dbc335c2 100644 --- a/dvc/analytics.py +++ b/dvc/analytics.py @@ -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) diff --git a/tests/unit/test_analytics.py b/tests/unit/test_analytics.py index 3179aeff0f..9413e781bc 100644 --- a/tests/unit/test_analytics.py +++ b/tests/unit/test_analytics.py @@ -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(