diff --git a/shub/exceptions.py b/shub/exceptions.py new file mode 100644 index 00000000..67b39a47 --- /dev/null +++ b/shub/exceptions.py @@ -0,0 +1,5 @@ +from click import ClickException + + +class NoCredentialsException(ClickException): + pass diff --git a/shub/utils.py b/shub/utils.py index 45a53140..91c8d2b0 100644 --- a/shub/utils.py +++ b/shub/utils.py @@ -10,6 +10,7 @@ import requests from shub.click_utils import log +from shub.exceptions import NoCredentialsException SCRAPY_CFG_FILE = os.path.expanduser("~/.scrapy.cfg") OS_WIN = True if os.name == 'nt' else False @@ -28,10 +29,20 @@ def missing_modules(*modules): def find_api_key(): - """Finds and returns the Scrapy Cloud APIKEY""" + """Finds and returns the Scrapy Cloud APIKEY + + Raises: + NoCredentialsException: if no API key is found + """ key = os.getenv("SHUB_APIKEY") if not key: key = get_key_netrc() + + if not key: + err = ("Your credentials haven't been defined.\n" + "Use 'shub login' or set the SHUB_APIKEY environment variable.") + raise NoCredentialsException(err) + return key