Skip to content

Commit

Permalink
Fix getnikola/plugins#282 — more informative errors for JSON parse fa…
Browse files Browse the repository at this point in the history
…ilures

Signed-off-by: Chris Warrick <kwpolska@gmail.com>
  • Loading branch information
Kwpolska committed Jul 24, 2018
1 parent f28f419 commit e023540
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 13 deletions.
2 changes: 2 additions & 0 deletions CHANGES.txt
Expand Up @@ -8,6 +8,8 @@ Features

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

Bugfixes
--------
Expand Down
22 changes: 16 additions & 6 deletions nikola/plugins/command/plugin.py
Expand Up @@ -27,6 +27,7 @@
"""Manage plugins."""

import io
import json.decoder
import os
import sys
import shutil
Expand Down Expand Up @@ -338,10 +339,19 @@ def get_json(self, url):
"""Download the JSON file with all plugins."""
if self.json is None:
try:
self.json = requests.get(url).json()
except requests.exceptions.SSLError:
LOGGER.warning("SSL error, using http instead of https (press ^C to abort)")
time.sleep(1)
url = url.replace('https', 'http', 1)
self.json = requests.get(url).json()
try:
self.json = requests.get(url).json()
except requests.exceptions.SSLError:
LOGGER.warning("SSL error, using http instead of https (press ^C to abort)")
time.sleep(1)
url = url.replace('https', 'http', 1)
self.json = requests.get(url).json()
except json.decoder.JSONDecodeError as e:
LOGGER.error("Failed to decode JSON data in response from server.")
LOGGER.error("JSON error encountered: " + str(e))
LOGGER.error("This issue might be caused by server-side issues, or by to unusual activity in your "
"network (as determined by CloudFlare). Please visit https://plugins.getnikola.com/ in "
"a browser.")
sys.exit(2)

return self.json
24 changes: 17 additions & 7 deletions nikola/plugins/command/theme.py
Expand Up @@ -26,8 +26,9 @@

"""Manage themes."""

import os
import io
import json.decoder
import os
import shutil
import time
import requests
Expand Down Expand Up @@ -374,10 +375,19 @@ def get_json(self, url):
"""Download the JSON file with all plugins."""
if self.json is None:
try:
self.json = requests.get(url).json()
except requests.exceptions.SSLError:
LOGGER.warning("SSL error, using http instead of https (press ^C to abort)")
time.sleep(1)
url = url.replace('https', 'http', 1)
self.json = requests.get(url).json()
try:
self.json = requests.get(url).json()
except requests.exceptions.SSLError:
LOGGER.warning("SSL error, using http instead of https (press ^C to abort)")
time.sleep(1)
url = url.replace('https', 'http', 1)
self.json = requests.get(url).json()
except json.decoder.JSONDecodeError as e:
LOGGER.error("Failed to decode JSON data in response from server.")
LOGGER.error("JSON error encountered:" + str(e))
LOGGER.error("This issue might be caused by server-side issues, or by to unusual activity in your "
"network (as determined by CloudFlare). Please visit https://themes.getnikola.com/ in "
"a browser.")
sys.exit(2)

return self.json

0 comments on commit e023540

Please sign in to comment.