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

Implement error handling in Nokogiri parser #81

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/nori.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require "nori/xml_utility_node"

class Nori
class ParseError < StandardError; end

def self.hash_key(name, options = {})
name = name.tr("-", "_") if options[:convert_dashes_to_underscores]
Expand Down
5 changes: 5 additions & 0 deletions lib/nori/parser/nokogiri.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ module Nokogiri

class Document < ::Nokogiri::XML::SAX::Document
attr_accessor :options
attr_accessor :last_error

def stack
@stack ||= []
Expand Down Expand Up @@ -44,13 +45,17 @@ def characters(string)

alias cdata_block characters

def error(message)
@last_error = message
end
end

def self.parse(xml, options)
document = Document.new
document.options = options
parser = ::Nokogiri::XML::SAX::Parser.new document
parser.parse xml
raise ParseError, document.last_error if document.last_error
document.stack.length > 0 ? document.stack.pop.to_hash : {}
end

Expand Down
6 changes: 5 additions & 1 deletion lib/nori/parser/rexml.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ def self.parse(xml, options)
parser = ::REXML::Parsers::BaseParser.new(xml)

while true
raw_data = parser.pull
begin
raw_data = parser.pull
rescue ::REXML::ParseException => error
raise Nori::ParseError, error.message
end
event = unnormalize(raw_data)
case event[0]
when :end_document
Expand Down
4 changes: 4 additions & 0 deletions spec/nori/nori_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -640,6 +640,10 @@
expect(parse(' ')).to eq({})
end

it "raises error on missing end tag" do
expect { parse('<foo><bar>foo bar</foo>') }.to raise_error(Nori::ParseError)
end

end
end

Expand Down