Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: handle invalid urls #63 #73

Merged
merged 1 commit into from
Feb 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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