Skip to content

Commit e023540

Browse files
committedJul 24, 2018
Fix getnikola/plugins#282 — more informative errors for JSON parse failures
Signed-off-by: Chris Warrick <kwpolska@gmail.com>
1 parent f28f419 commit e023540

File tree

3 files changed

+35
-13
lines changed

3 files changed

+35
-13
lines changed
 

‎CHANGES.txt

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ Features
88

99
* Show the filename of the missing file when ``nikola serve`` can't
1010
find a file (i.e. when an 404 error occurs).
11+
* Better error messages for JSON download failures in ``nikola
12+
plugin`` and ``nikola theme`` (Issue getnikola/plugins#282)
1113

1214
Bugfixes
1315
--------

‎nikola/plugins/command/plugin.py

+16-6
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
"""Manage plugins."""
2828

2929
import io
30+
import json.decoder
3031
import os
3132
import sys
3233
import shutil
@@ -338,10 +339,19 @@ def get_json(self, url):
338339
"""Download the JSON file with all plugins."""
339340
if self.json is None:
340341
try:
341-
self.json = requests.get(url).json()
342-
except requests.exceptions.SSLError:
343-
LOGGER.warning("SSL error, using http instead of https (press ^C to abort)")
344-
time.sleep(1)
345-
url = url.replace('https', 'http', 1)
346-
self.json = requests.get(url).json()
342+
try:
343+
self.json = requests.get(url).json()
344+
except requests.exceptions.SSLError:
345+
LOGGER.warning("SSL error, using http instead of https (press ^C to abort)")
346+
time.sleep(1)
347+
url = url.replace('https', 'http', 1)
348+
self.json = requests.get(url).json()
349+
except json.decoder.JSONDecodeError as e:
350+
LOGGER.error("Failed to decode JSON data in response from server.")
351+
LOGGER.error("JSON error encountered: " + str(e))
352+
LOGGER.error("This issue might be caused by server-side issues, or by to unusual activity in your "
353+
"network (as determined by CloudFlare). Please visit https://plugins.getnikola.com/ in "
354+
"a browser.")
355+
sys.exit(2)
356+
347357
return self.json

‎nikola/plugins/command/theme.py

+17-7
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,9 @@
2626

2727
"""Manage themes."""
2828

29-
import os
3029
import io
30+
import json.decoder
31+
import os
3132
import shutil
3233
import time
3334
import requests
@@ -374,10 +375,19 @@ def get_json(self, url):
374375
"""Download the JSON file with all plugins."""
375376
if self.json is None:
376377
try:
377-
self.json = requests.get(url).json()
378-
except requests.exceptions.SSLError:
379-
LOGGER.warning("SSL error, using http instead of https (press ^C to abort)")
380-
time.sleep(1)
381-
url = url.replace('https', 'http', 1)
382-
self.json = requests.get(url).json()
378+
try:
379+
self.json = requests.get(url).json()
380+
except requests.exceptions.SSLError:
381+
LOGGER.warning("SSL error, using http instead of https (press ^C to abort)")
382+
time.sleep(1)
383+
url = url.replace('https', 'http', 1)
384+
self.json = requests.get(url).json()
385+
except json.decoder.JSONDecodeError as e:
386+
LOGGER.error("Failed to decode JSON data in response from server.")
387+
LOGGER.error("JSON error encountered:" + str(e))
388+
LOGGER.error("This issue might be caused by server-side issues, or by to unusual activity in your "
389+
"network (as determined by CloudFlare). Please visit https://themes.getnikola.com/ in "
390+
"a browser.")
391+
sys.exit(2)
392+
383393
return self.json

0 commit comments

Comments
 (0)
Please sign in to comment.