Skip to content

Commit

Permalink
handle PackageNotFoundError
Browse files Browse the repository at this point in the history
  • Loading branch information
viseshrp committed Jan 3, 2019
1 parent e5cbe0e commit b2f462a
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 5 deletions.
1 change: 0 additions & 1 deletion whatsonpypi/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ def main(package):
Example usages:
$ whatsonpypi django
"""
try:
click.secho(get_query_response(package=package), fg='green', bold=True)
Expand Down
9 changes: 6 additions & 3 deletions whatsonpypi/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from requests import Request, Session, hooks

from .constants import PYPI_BASE_URL
from .exceptions import PackageAbsentException
from .exceptions import PackageNotProvidedError, PackageNotFoundError


class WoppResponse:
Expand Down Expand Up @@ -48,7 +48,6 @@ def request(self, timeout=3.1, package=None, version=None):
:return: response serialized by WoppResponse object
"""
url = self._build_url(package, version)

req_kwargs = {
'method': 'GET',
'url': url,
Expand All @@ -63,6 +62,10 @@ def request(self, timeout=3.1, package=None, version=None):
prepared_request,
timeout=timeout,
)

if response.status_code == 404:
raise PackageNotFoundError("Sorry, but that package couldn't be found on PyPI.")

# serialize response
wopp_response = WoppResponse(int(response.status_code), response.json())
return wopp_response
Expand All @@ -76,7 +79,7 @@ def _build_url(self, package=None, version=None):
:return: fully qualified URL
"""
if package is None:
raise PackageAbsentException('A package name is needed to proceed.')
raise PackageNotProvidedError('A package name is needed to proceed.')

if version is not None:
url = "{}/{}/{}/json".format(self.base_url, package, version)
Expand Down
8 changes: 7 additions & 1 deletion whatsonpypi/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,13 @@ class WoppException(Exception):
"""


class PackageAbsentException(WoppException):
class PackageNotProvidedError(WoppException):
"""
Raised when no package is available for the client to request
"""


class PackageNotFoundError(WoppException):
"""
Raised when a package is not found on PyPI
"""
1 change: 1 addition & 0 deletions whatsonpypi/whatsonpypi.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ def get_query_response(package=None, version=None):
"""
client = WoppClient(request_hooks={'response': clean_response})
response = client.request(package=package, version=version)

# returns version by default
return response.latest_version

Expand Down

0 comments on commit b2f462a

Please sign in to comment.