Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rethrow exceptions caught by Java SAX handler #1872

Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 12 additions & 14 deletions ext/java/nokogiri/internals/NokogiriHandler.java
Expand Up @@ -246,27 +246,25 @@ public void endCDATA() {
charactersBuilder.setLength(0);
}

@Override
public void error(SAXParseException ex) {
void handleError(SAXParseException spx) {
adjam marked this conversation as resolved.
Show resolved Hide resolved
try {
final String msg = ex.getMessage();
final String msg = spx.getMessage();
call("error", runtime.newString(msg == null ? "" : msg));
addError(new RaiseException(XmlSyntaxError.createError(runtime, ex), true));
} catch(RaiseException e) {
addError(e);
addError(new RaiseException(XmlSyntaxError.createError(runtime, spx), true));
} catch( RaiseException rx ) {
adjam marked this conversation as resolved.
Show resolved Hide resolved
addError(rx);
throw rx;
}
}

@Override
public void fatalError(SAXParseException ex) {
try {
final String msg = ex.getMessage();
call("error", runtime.newString(msg == null ? "" : msg));
addError(new RaiseException(XmlSyntaxError.createFatalError(runtime, ex), true));
public void error(SAXParseException ex) {
handleError(ex);
}

} catch(RaiseException e) {
addError(e);
}
@Override
public void fatalError(SAXParseException ex) {
handleError(ex);
}

@Override
Expand Down
55 changes: 55 additions & 0 deletions test/xml/sax/test_document_error.rb
@@ -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
1 change: 1 addition & 0 deletions test/xml/sax/test_push_parser.rb
Expand Up @@ -73,6 +73,7 @@ def error msg
rescue => e
actual = e
end
fail 'PushParser should throw error when fed ill-formed data' if actual.nil?

assert_equal actual.message, "parse error"
end
Expand Down