From 75deb6eed9c9f951092ed4a980f93b50e36d9378 Mon Sep 17 00:00:00 2001 From: Alexander Zagaynov Date: Wed, 1 Jul 2020 17:06:45 +0200 Subject: [PATCH] fix 'undefined method for nil:NilClass' on wrong xmls --- lib/nori.rb | 2 ++ spec/nori/nori_spec.rb | 4 ++++ 2 files changed, 6 insertions(+) diff --git a/lib/nori.rb b/lib/nori.rb index ba55215..a1f3236 100644 --- a/lib/nori.rb +++ b/lib/nori.rb @@ -46,6 +46,8 @@ def parse(xml) parser = load_parser @options[:parser] parser.parse(cleaned_xml, @options) + rescue NoMethodError => e + raise ArgumentError, "Parsing error (#{e.message}), might be malformed xml" end private diff --git a/spec/nori/nori_spec.rb b/spec/nori/nori_spec.rb index 52c009f..36415e6 100644 --- a/spec/nori/nori_spec.rb +++ b/spec/nori/nori_spec.rb @@ -640,6 +640,10 @@ expect(parse(' ')).to eq({}) end + it "handle incorrect xml string" do + incorrect_xml = "\n\n 0\n\n" + expect { parse(incorrect_xml) }.to raise_error(ArgumentError, /Parsing error/) + end end end