Permalink
Browse files

Adds better error reporting for Post YAML parsing.

This was contributed by nix (Nick Thompson) here:
EnigmaCurry/blogofile#78

This was moved over to the new blogofile_blog plugin, so sorry that he
didn't get the commit credit.
  • Loading branch information...
EnigmaCurry committed Mar 10, 2011
1 parent 3424a9d commit 4d54e5d117c8385c5e93f1e802c04c06417dc7ba
Showing with 13 additions and 2 deletions.
  1. +13 −2 blogofile_blog/site_src/_controllers/blog/post.py
@@ -108,7 +108,7 @@ def __parse(self):
yaml_sep = re.compile("^---$", re.MULTILINE)
content_parts = yaml_sep.split(self.source, maxsplit=2)
if len(content_parts) < 2:
- raise PostParseException(u"{0}: Post has no YAML section".format(
+ raise PostParseException(u"Post has no YAML section: {0}".format(
self.filename))
else:
#Extract the yaml at the top
@@ -191,7 +191,18 @@ def __post_process(self):
logger.debug(u"Permalink: {0}".format(self.permalink))
def __parse_yaml(self, yaml_src):
- y = yaml.load(yaml_src)
+ try:
+ y = yaml.load(yaml_src)
+ except yaml.YAMLError as e:
+ linenum = 1
+ if getattr(e, 'context_mark', None):
+ linenum = 1 + e.context_mark.line
+ raise PostParseException(
+ u"Post has bad YAML section: {0}:{1}".format(
+ self.filename, linenum, str(e)))
+ if not isinstance(y, dict):
+ raise PostParseException(
+ u"Post has bad YAML section: {0}".format(self.filename))
# Load all the fields that require special processing first:
fields_need_processing = ('permalink', 'guid', 'date', 'updated',
'categories', 'tags', 'draft')

0 comments on commit 4d54e5d

Please sign in to comment.