Skip to content

Commit

Permalink
Nicer error handling (Fix #2771)
Browse files Browse the repository at this point in the history
  • Loading branch information
Roberto Alsina committed May 18, 2017
1 parent 1d03f5f commit 1f144c3
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 13 deletions.
1 change: 1 addition & 0 deletions CHANGES.txt
Expand Up @@ -4,6 +4,7 @@ New in master
Features
--------

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

Expand Down
25 changes: 17 additions & 8 deletions nikola/__main__.py
Expand Up @@ -275,13 +275,19 @@ def load_tasks(self, cmd, opt_values, pos_args):
}
DOIT_CONFIG['default_tasks'] = ['render_site', 'post_render']
DOIT_CONFIG.update(self.nikola._doit_config)
tasks = generate_tasks(
'render_site',
self.nikola.gen_tasks('render_site', "Task", 'Group of tasks to render the site.'))
latetasks = generate_tasks(
'post_render',
self.nikola.gen_tasks('post_render', "LateTask", 'Group of tasks to be executed after site is rendered.'))
signal('initialized').send(self.nikola)
try:
tasks = generate_tasks(
'render_site',
self.nikola.gen_tasks('render_site', "Task", 'Group of tasks to render the site.'))
latetasks = generate_tasks(
'post_render',
self.nikola.gen_tasks('post_render', "LateTask", 'Group of tasks to be executed after site is rendered.'))
signal('initialized').send(self.nikola)
except Exception:
LOGGER.error('Error loading tasks')
if self.nikola.debug:
raise
return {}, {}
return tasks + latetasks, DOIT_CONFIG


Expand Down Expand Up @@ -363,7 +369,10 @@ def run(self, cmd_args):
LOGGER.error("This command needs to run inside an "
"existing Nikola site.")
return 3
return super(DoitNikola, self).run(cmd_args)
try:
return super(DoitNikola, self).run(cmd_args)
except Exception:
pass

@staticmethod
def print_version():
Expand Down
9 changes: 6 additions & 3 deletions nikola/nikola.py
Expand Up @@ -1086,8 +1086,7 @@ def init_plugins(self, commands_only=False, load_all=False):
# Remove blacklisted plugins
if p[-1].name in self.config['DISABLED_PLUGINS']:
bad_candidates.add(p)
utils.LOGGER.debug('Not loading disabled plugin {}', p[-1].name)
# Remove compilers we don't use
utils.LOGGER.debug('Not loading disabled plugin {}', p[-1].name) # Remove compilers we don't use
if p[-1].details.has_option('Nikola', 'PluginCategory') and p[-1].details.get('Nikola', 'PluginCategory') in ('Compiler', 'PageCompiler'):
bad_candidates.add(p)
self.disabled_compilers[p[-1].name] = p
Expand Down Expand Up @@ -2125,7 +2124,11 @@ def scan_posts(self, really=False, ignore_quit=False, quiet=False):
self.pages = []

for p in sorted(self.plugin_manager.getPluginsOfCategory('PostScanner'), key=operator.attrgetter('name')):
timeline = p.plugin_object.scan()
try:
timeline = p.plugin_object.scan()
except Exception:
utils.LOGGER.error('Error reading timeline')
raise
# FIXME: can there be conflicts here?
self.timeline.extend(timeline)

Expand Down
11 changes: 9 additions & 2 deletions nikola/post.py
Expand Up @@ -198,7 +198,12 @@ def __init__(
try:
self.date = to_datetime(self.meta[self.default_lang]['date'], tzinfo)
except ValueError:
raise ValueError("Invalid date '{0}' in file {1}".format(self.meta[self.default_lang]['date'], source_path))
if not self.meta[self.default_lang]['date']:
msg = 'Missing date in file {}'.format(source_path)
else:
msg = "Invalid date '{0}' in file {1}".format(self.meta[self.default_lang]['date'], source_path)
LOGGER.error(msg)
raise ValueError(msg)

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

Expand Down

0 comments on commit 1f144c3

Please sign in to comment.