Skip to content

Commit b9f9b71

Browse files
committed
Preventing exceptions, handling too many requests.
1 parent 4c7b293 commit b9f9b71

File tree

3 files changed

+28
-3
lines changed

3 files changed

+28
-3
lines changed

safety/cli.py

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from safety.formatter import report, license_report
88
import itertools
99
from safety.util import read_requirements, read_vulnerabilities, get_proxy_dict, get_packages_licenses
10-
from safety.errors import DatabaseFetchError, DatabaseFileNotFoundError, InvalidKeyError
10+
from safety.errors import DatabaseFetchError, DatabaseFileNotFoundError, InvalidKeyError, TooManyRequestsError
1111

1212
try:
1313
from json.decoder import JSONDecodeError
@@ -151,7 +151,26 @@ def license(key, db, cache, files, proxyprotocol, proxyhost, proxyport):
151151
]
152152

153153
proxy_dictionary = get_proxy_dict(proxyprotocol, proxyhost, proxyport)
154-
licenses_db = safety.get_licenses(key, db, cache, proxy_dictionary)
154+
try:
155+
licenses_db = safety.get_licenses(key, db, cache, proxy_dictionary)
156+
except InvalidKeyError:
157+
click.secho("Your API Key '{key}' is invalid. See {link}".format(
158+
key=key, link='https://goo.gl/O7Y1rS'),
159+
fg="red",
160+
file=sys.stderr)
161+
sys.exit(-1)
162+
except DatabaseFileNotFoundError:
163+
click.secho("Unable to load licenses database from {db}".format(db=db), fg="red", file=sys.stderr)
164+
sys.exit(-1)
165+
except TooManyRequestsError:
166+
click.secho("Unable to load licenses database (Too many requests, please wait before another request)",
167+
fg="red",
168+
file=sys.stderr
169+
)
170+
sys.exit(-1)
171+
except DatabaseFetchError:
172+
click.secho("Unable to load licenses database", fg="red", file=sys.stderr)
173+
sys.exit(-1)
155174
filtered_packages_licenses = get_packages_licenses(packages, licenses_db)
156175
output_report = license_report(packages=packages, licenses=filtered_packages_licenses)
157176
click.secho(output_report, nl=True)

safety/errors.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,7 @@ class DatabaseFileNotFoundError(DatabaseFetchError):
88

99
class InvalidKeyError(DatabaseFetchError):
1010
pass
11+
12+
13+
class TooManyRequestsError(DatabaseFetchError):
14+
pass

safety/safety.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from .constants import (API_MIRRORS, CACHE_FILE, CACHE_LICENSES_VALID_SECONDS,
1212
CACHE_VALID_SECONDS, OPEN_MIRRORS, REQUEST_TIMEOUT)
1313
from .errors import (DatabaseFetchError, DatabaseFileNotFoundError,
14-
InvalidKeyError)
14+
InvalidKeyError, TooManyRequestsError)
1515
from .util import RequirementFile
1616

1717

@@ -95,6 +95,8 @@ def fetch_database_url(mirror, db_name, key, cached, proxy):
9595
return data
9696
elif r.status_code == 403:
9797
raise InvalidKeyError()
98+
elif r.status_code == 429:
99+
raise TooManyRequestsError()
98100

99101

100102
def fetch_database_file(path, db_name):

0 commit comments

Comments
 (0)