From ff7a1a8788965166d5fe88f70758f47c629871ea Mon Sep 17 00:00:00 2001 From: Jose Ricardo Date: Fri, 19 Jun 2015 17:12:27 -0400 Subject: [PATCH] Raising NoCredentialsException on utils#find_api_key NoCredentialsException is a ClickException, which is handled nicely by click, displaying the error msg and returning an exit code != 0. --- shub/exceptions.py | 5 +++++ shub/utils.py | 13 ++++++++++++- 2 files changed, 17 insertions(+), 1 deletion(-) create mode 100644 shub/exceptions.py 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