Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f6528d4

Browse files
ujihisakou
authored andcommitted
Message less confusing error to human (#16)
* Message less confusing error to human * Problem: Following error message is not helpful, because you have to reason that '' actually means it's in the top-level, and the 'div' (not '</div>') is an end tag require "rexml/parsers/lightparser" REXML::Parsers::LightParser.new('</div>').parse #=> Missing end tag for '' (got 'div') * Solution: add a special case in error handling just to change the error message require "rexml/parsers/lightparser" REXML::Parsers::LightParser.new('</div>').parse #=> Unexpected top-level end tag (got 'div') * Refactor by removing unnecessary `md` check * Thanks @a_matsuda to review this at asakusa.rb!
1 parent 9f2908d commit f6528d4

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

lib/rexml/parsers/baseparser.rb

+4
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,10 @@ def pull_event
335335
@nsstack.shift
336336
last_tag = @tags.pop
337337
md = @source.match( CLOSE_MATCH, true )
338+
if md and !last_tag
339+
message = "Unexpected top-level end tag (got '#{md[1]}')"
340+
raise REXML::ParseException.new(message, @source)
341+
end
338342
if md.nil? or last_tag != md[1]
339343
message = "Missing end tag for '#{last_tag}'"
340344
message << " (got '#{md[1]}')" if md

test/rexml/parse/test_element.rb

+13
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,19 @@ def parse(xml)
88
end
99

1010
class TestInvalid < self
11+
def test_top_level_end_tag
12+
exception = assert_raise(REXML::ParseException) do
13+
parse("</a>")
14+
end
15+
assert_equal(<<-DETAIL.chomp, exception.to_s)
16+
Unexpected top-level end tag (got 'a')
17+
Line: 1
18+
Position: 4
19+
Last 80 unconsumed characters:
20+
21+
DETAIL
22+
end
23+
1124
def test_no_end_tag
1225
exception = assert_raise(REXML::ParseException) do
1326
parse("<a></")

0 commit comments

Comments
 (0)