Message for yaml error #718

Merged
merged 3 commits into from Dec 31, 2012
@@ -25,14 +25,16 @@ def to_s
#
# Returns nothing.
def read_yaml(base, name)
- self.content = File.read(File.join(base, name))
-
begin
@ixti

ixti Dec 29, 2012

Member

there's no need for begin in this case. it can be something like:

def foo(bar, baz)
  self.content = File.read(File.join(base, name))

  if self.content =~ /\A(---\s*\n.*?\n?)^(---\s*$\n?)/m
    self.content = $POSTMATCH
    self.data = YAML.load($1)
  end
rescue SyntaxErrot => e
  puts "YAML Exception reading #{name}: #{e.message}"
rescue => e
  puts "YAML Exception reading #{name}: #{e.message}"
end
@stereobooster

stereobooster Dec 29, 2012

Contributor
    def read_yaml(base, name)
      begin
        self.content = File.read(File.join(base, name))

        if self.content =~ /\A(---\s*\n.*?\n?)^(---\s*$\n?)/m
          self.content = $POSTMATCH
          self.data = YAML.load($1)
        end
      rescue => e
        puts "Error reading file #{name}: #{e.message}"
      rescue SyntaxError => e
        puts "YAML Exception reading #{name}: #{e.message}"
      end

      self.data ||= {}
    end
@ixti

ixti Dec 29, 2012

Member
def foo(bar, baz)
  self.content = File.read(File.join(base, name))
  self.data = {}

  if self.content =~ /\A(---\s*\n.*?\n?)^(---\s*$\n?)/m
    self.content = $POSTMATCH
    self.data = YAML.load($1)
  end
rescue SyntaxErrot => e
  puts "YAML Exception reading #{name}: #{e.message}"
rescue => e
  puts "YAML Exception reading #{name}: #{e.message}"
end

:D

@stereobooster

stereobooster Dec 29, 2012

Contributor

What about return value in case of rescue?

rescue SyntaxErrot => e
  puts "YAML Exception reading #{name}: #{e.message}"
  self.data
rescue => e
  puts "YAML Exception reading #{name}: #{e.message}"
  self.data
end
@ixti

ixti Dec 29, 2012

Member

Ahhh. Sorry. Yes. In this case it's better leave it is in your original pull request.

+ self.content = File.read(File.join(base, name))
+
if self.content =~ /\A(---\s*\n.*?\n?)^(---\s*$\n?)/m
self.content = $POSTMATCH
self.data = YAML.load($1)
end
rescue => e
+ puts "Error reading file #{name}: #{e.message}"
+ rescue SyntaxError => e
puts "YAML Exception reading #{name}: #{e.message}"
end
@@ -0,0 +1,4 @@
+---
+bad yaml: [
+---
+Real content starts here
@@ -0,0 +1,7 @@
+---
+test: good
+---
+Real content starts here
+
+Ðóññêèé òåêñò
+
View
@@ -31,4 +31,14 @@ def source_dir(*subdirs)
def clear_dest
FileUtils.rm_rf(dest_dir)
end
+
+ def capture_stdout
+ $old_stdout = $stdout
+ $stdout = StringIO.new
+ yield
+ $stdout.rewind
+ return $stdout.string
+ ensure
+ $stdout = $old_stdout
+ end
end
View
@@ -18,5 +18,23 @@ class TestConvertible < Test::Unit::TestCase
ret = @convertible.read_yaml(@base, 'broken_front_matter1.erb')
assert_equal({}, ret)
end
+
+ should "not parse if there is syntax error in front-matter" do
+ out = capture_stdout do
+ ret = @convertible.read_yaml(@base, 'broken_front_matter2.erb')
+ assert_equal({}, ret)
+ end
+ assert_match(/YAML Exception|syntax error/, out)
+ end
+
+ if RUBY_VERSION >= '1.9.2'
+ should "not parse if there is encoding error in file" do
+ out = capture_stdout do
+ ret = @convertible.read_yaml(@base, 'broken_front_matter3.erb')
+ assert_equal({}, ret)
+ end
+ assert_match(/invalid byte sequence in UTF-8/, out)
+ end
+ end
end
end