Skip to content

Commit 1f144c3

Browse files
author
Roberto Alsina
committedMay 18, 2017
Nicer error handling (Fix #2771)
1 parent 1d03f5f commit 1f144c3

File tree

4 files changed

+33
-13
lines changed

4 files changed

+33
-13
lines changed
 

‎CHANGES.txt

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ New in master
44
Features
55
--------
66

7+
* Better error handling when posts can't be parsed (Issue #2771)
78
* New ``add_header_permalinks`` filter, for Sphinx-style header links
89
(Issue #2636)
910

‎nikola/__main__.py

+17-8
Original file line numberDiff line numberDiff line change
@@ -275,13 +275,19 @@ def load_tasks(self, cmd, opt_values, pos_args):
275275
}
276276
DOIT_CONFIG['default_tasks'] = ['render_site', 'post_render']
277277
DOIT_CONFIG.update(self.nikola._doit_config)
278-
tasks = generate_tasks(
279-
'render_site',
280-
self.nikola.gen_tasks('render_site', "Task", 'Group of tasks to render the site.'))
281-
latetasks = generate_tasks(
282-
'post_render',
283-
self.nikola.gen_tasks('post_render', "LateTask", 'Group of tasks to be executed after site is rendered.'))
284-
signal('initialized').send(self.nikola)
278+
try:
279+
tasks = generate_tasks(
280+
'render_site',
281+
self.nikola.gen_tasks('render_site', "Task", 'Group of tasks to render the site.'))
282+
latetasks = generate_tasks(
283+
'post_render',
284+
self.nikola.gen_tasks('post_render', "LateTask", 'Group of tasks to be executed after site is rendered.'))
285+
signal('initialized').send(self.nikola)
286+
except Exception:
287+
LOGGER.error('Error loading tasks')
288+
if self.nikola.debug:
289+
raise
290+
return {}, {}
285291
return tasks + latetasks, DOIT_CONFIG
286292

287293

@@ -363,7 +369,10 @@ def run(self, cmd_args):
363369
LOGGER.error("This command needs to run inside an "
364370
"existing Nikola site.")
365371
return 3
366-
return super(DoitNikola, self).run(cmd_args)
372+
try:
373+
return super(DoitNikola, self).run(cmd_args)
374+
except Exception:
375+
pass
367376

368377
@staticmethod
369378
def print_version():

‎nikola/nikola.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -1086,8 +1086,7 @@ def init_plugins(self, commands_only=False, load_all=False):
10861086
# Remove blacklisted plugins
10871087
if p[-1].name in self.config['DISABLED_PLUGINS']:
10881088
bad_candidates.add(p)
1089-
utils.LOGGER.debug('Not loading disabled plugin {}', p[-1].name)
1090-
# Remove compilers we don't use
1089+
utils.LOGGER.debug('Not loading disabled plugin {}', p[-1].name) # Remove compilers we don't use
10911090
if p[-1].details.has_option('Nikola', 'PluginCategory') and p[-1].details.get('Nikola', 'PluginCategory') in ('Compiler', 'PageCompiler'):
10921091
bad_candidates.add(p)
10931092
self.disabled_compilers[p[-1].name] = p
@@ -2125,7 +2124,11 @@ def scan_posts(self, really=False, ignore_quit=False, quiet=False):
21252124
self.pages = []
21262125

21272126
for p in sorted(self.plugin_manager.getPluginsOfCategory('PostScanner'), key=operator.attrgetter('name')):
2128-
timeline = p.plugin_object.scan()
2127+
try:
2128+
timeline = p.plugin_object.scan()
2129+
except Exception:
2130+
utils.LOGGER.error('Error reading timeline')
2131+
raise
21292132
# FIXME: can there be conflicts here?
21302133
self.timeline.extend(timeline)
21312134

‎nikola/post.py

+9-2
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,12 @@ def __init__(
198198
try:
199199
self.date = to_datetime(self.meta[self.default_lang]['date'], tzinfo)
200200
except ValueError:
201-
raise ValueError("Invalid date '{0}' in file {1}".format(self.meta[self.default_lang]['date'], source_path))
201+
if not self.meta[self.default_lang]['date']:
202+
msg = 'Missing date in file {}'.format(source_path)
203+
else:
204+
msg = "Invalid date '{0}' in file {1}".format(self.meta[self.default_lang]['date'], source_path)
205+
LOGGER.error(msg)
206+
raise ValueError(msg)
202207

203208
if 'updated' not in default_metadata:
204209
default_metadata['updated'] = default_metadata.get('date', None)
@@ -976,7 +981,9 @@ def get_metadata_from_file(source_path, config=None, lang=None):
976981
meta_data = [x.strip() for x in meta_file.readlines()]
977982
return _get_metadata_from_file(meta_data)
978983
except (UnicodeDecodeError, UnicodeEncodeError):
979-
raise ValueError('Error reading {0}: Nikola only supports UTF-8 files'.format(source_path))
984+
msg = 'Error reading {0}: Nikola only supports UTF-8 files'.format(source_path)
985+
LOGGER.error(msg)
986+
raise ValueError(msg)
980987
except Exception: # The file may not exist, for multilingual sites
981988
return {}
982989

0 commit comments

Comments
 (0)
Please sign in to comment.