Show XML parsing errors when using IO as input.

Previously this wouldn't display anything due to the IO object being exhausted.
To fix this the input has to be wound back to the start, which means re-reading
it. Sadly I can't think of a way around this that doesn't require buffering
lines while parsing them (which massively increases memory usage).
This commit is contained in:
Yorick Peterse 2014-09-03 22:52:59 +02:00
parent d67f43508d
commit dc5874f5aa
2 changed files with 39 additions and 21 deletions

View File

@ -230,16 +230,21 @@ end
def on_error(type, value, stack) def on_error(type, value, stack)
name = token_to_str(type) name = token_to_str(type)
index = @line - 1 index = @line - 1
lines = @data.each_line.to_a index_range = (index - 5)..(index + 5)
code = '' code = ''
# Show up to 5 lines before and after the offending line (if they exist). # For IO we sadly have to re-read the input :<
(-5..5).each do |offset| if @data.respond_to?(:rewind)
line = lines[index + offset] @data.rewind
number = @line + offset end
if line and number > 0 # Show up to 5 lines before and after the offending line (if they exist).
if offset == 0 @data.each_line.with_index do |line, line_index|
next unless index_range.cover?(line_index)
number = line_index + 1
if line_index == index
prefix = '=> ' prefix = '=> '
else else
prefix = ' ' prefix = ' '
@ -253,7 +258,6 @@ end
code << "#{prefix}#{number}: #{line}\n" code << "#{prefix}#{number}: #{line}\n"
end end
end
raise Racc::ParseError, <<-EOF.strip raise Racc::ParseError, <<-EOF.strip
Unexpected #{name} with value #{value.inspect} on line #{@line}: Unexpected #{name} with value #{value.inspect} on line #{@line}:

View File

@ -16,7 +16,7 @@ describe Oga::XML::Parser do
expect { parse(@invalid_xml) }.to raise_error(Racc::ParseError) expect { parse(@invalid_xml) }.to raise_error(Racc::ParseError)
end end
example 'display a more meaningful error message' do example 'include the offending input when using String as input' do
# Racc basically reports errors at the last moment instead of where they # Racc basically reports errors at the last moment instead of where they
# *actually* occur. # *actually* occur.
partial = <<-EOF.strip partial = <<-EOF.strip
@ -29,5 +29,19 @@ describe Oga::XML::Parser do
parse_error(@invalid_xml).should =~ /#{partial}/ parse_error(@invalid_xml).should =~ /#{partial}/
end end
example 'include the offending input when using IO as input' do
# Racc basically reports errors at the last moment instead of where they
# *actually* occur.
partial = <<-EOF.strip
1. <person>
2. <name>Alice</name>
3. <age>25
4. <nationality>Dutch</nationality>
=> 5. </person>
EOF
parse_error(StringIO.new(@invalid_xml)).should =~ /#{partial}/
end
end end
end end