Skip to content

Commit

Permalink
fix: handle invalid urls
Browse files Browse the repository at this point in the history
Signed-off-by: Tushar Goel <[email protected]>
  • Loading branch information
TG1999 committed Feb 9, 2022
1 parent 4c8b749 commit 3ffe263
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/fetchcode/package.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
9 changes: 9 additions & 0 deletions tests/test_package.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

0 comments on commit 3ffe263

Please sign in to comment.