-
-
Notifications
You must be signed in to change notification settings - Fork 898
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1872 from adjam/issue-1847-jruby-swallows-sax-exc…
…eptions Rethrow exceptions caught by Java SAX handler
- Loading branch information
Showing
3 changed files
with
65 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
require 'helper' | ||
|
||
module Nokogiri | ||
module XML | ||
module SAX | ||
|
||
# raises an exception when underlying parser | ||
# encounters an XML parsing error | ||
class ThrowingErrorDocument < Document | ||
def error(msg) | ||
raise(StandardError, "parsing did not complete: #{msg}") | ||
end | ||
end | ||
|
||
# only warns when underlying parser encounters | ||
# an XML parsing error | ||
class WarningErrorDocument < Document | ||
def error(msg) | ||
errors << msg | ||
end | ||
|
||
def errors | ||
@errors ||= [] | ||
end | ||
end | ||
|
||
class TestErrorHandling < Nokogiri::SAX::TestCase | ||
def setup | ||
super | ||
@error_parser = Parser.new(ThrowingErrorDocument.new) | ||
@warning_parser = Parser.new(WarningErrorDocument.new) | ||
end | ||
|
||
def test_error_throwing_document_raises_exception | ||
begin | ||
@error_parser.parse("<xml>") # no closing element | ||
fail '#parse should not complete successfully when document #error throws exception' | ||
rescue StandardError => e | ||
assert_match /parsing did not complete/, e.message | ||
end | ||
end | ||
|
||
def test_warning_document_encounters_error_but_terminates_normally | ||
begin | ||
@warning_parser.parse("<xml>") | ||
assert !@warning_parser.document.errors.empty?, 'error collector did not collect an error' | ||
rescue StandardError => e | ||
warn(e) | ||
fail '#parse should complete successfully unless document #error throws exception (#{e}' | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters