From 58c5f356e7139fefad289cffa3f23f12243ee4e0 Mon Sep 17 00:00:00 2001 From: Alexander Zagaynov Date: Wed, 1 Jul 2020 16:57:26 +0200 Subject: [PATCH] fix 'undefined method for nil:NilClass' on wrong xmls --- lib/nori.rb | 2 ++ spec/nori/nori_spec.rb | 5 +++++ 2 files changed, 7 insertions(+) diff --git a/lib/nori.rb b/lib/nori.rb index ba55215..18a6e01 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, might be malformed xml" end private diff --git a/spec/nori/nori_spec.rb b/spec/nori/nori_spec.rb index 52c009f..37a3cff 100644 --- a/spec/nori/nori_spec.rb +++ b/spec/nori/nori_spec.rb @@ -640,6 +640,11 @@ 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