From 3ffe26397846e540d4fc1dfe7c4cdf8e98b2e39f Mon Sep 17 00:00:00 2001 From: Tushar Goel Date: Wed, 9 Feb 2022 19:45:34 +0530 Subject: [PATCH] fix: handle invalid urls Signed-off-by: Tushar Goel --- src/fetchcode/package.py | 4 ++-- tests/test_package.py | 9 +++++++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/src/fetchcode/package.py b/src/fetchcode/package.py index 4469b690..12d9247a 100644 --- a/src/fetchcode/package.py +++ b/src/fetchcode/package.py @@ -43,10 +43,10 @@ def get_response(url): Generate `Package` object for a `url` string """ resp = requests.get(url) - if "json" in dir(resp): + if resp.status_code == 200: return resp.json() - raise Exception("Response of this URL does not contain json object") + raise Exception(f'Failed to fetch: {url}') def get_pypi_bugtracker_url(project_urls): diff --git a/tests/test_package.py b/tests/test_package.py index 64b12337..dd639a88 100644 --- a/tests/test_package.py +++ b/tests/test_package.py @@ -15,6 +15,7 @@ # specific language governing permissions and limitations under the License. import json +import pytest from unittest import mock from fetchcode.package import info @@ -97,3 +98,11 @@ def test_rubygems_packages(mock_get): mock_get.side_effect = side_effect packages = list(info(purl)) match_data(packages, expected_data) + + +@mock.patch("fetchcode.package.get_response") +def test_tuby_package_with_invalid_url(mock_get): + with pytest.raises(Exception) as e_info: + purl = "pkg:ruby/file" + packages = list(info(purl)) + assert "Failed to fetch: https://rubygems.org/api/v1/gems/file.json" == e_info