Skip to content

Commit 7e10954

Browse files
committed
Improved error message on missing token
1 parent 3ba148d commit 7e10954

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

pythonanywhere/api.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,24 @@ class AuthenticationError(Exception):
2323
pass
2424

2525

26+
class NoTokenError(Exception):
27+
pass
28+
29+
2630
def get_api_endpoint():
2731
domain = os.environ.get('PYTHONANYWHERE_DOMAIN', 'pythonanywhere.com')
2832
return 'https://www.{domain}/api/v0/user/{{username}}/webapps/'.format(domain=domain)
2933

3034

3135
def call_api(url, method, **kwargs):
32-
token = os.environ['API_TOKEN']
36+
token = os.environ.get('API_TOKEN')
37+
if token is None:
38+
raise NoTokenError(
39+
"Oops, you don't seem to have an API token. "
40+
"Please go to the 'Account' page on PythonAnywhere, then to the 'API Token' "
41+
"tab. Click the 'Create a new API token' button to create the token, then "
42+
"start a new console and try running this script again."
43+
)
3344
insecure = os.environ.get('PYTHONANYWHERE_INSECURE_API') == 'true'
3445
response = requests.request(
3546
method=method,

0 commit comments

Comments
 (0)