[Groonga-commit] ranguba/chupa-text at 96f5b4f [master] xml: handle error

Back to archive index
Kouhei Sutou null+****@clear*****
Sat Mar 2 05:32:43 JST 2019


Kouhei Sutou	2019-03-02 05:32:43 +0900 (Sat, 02 Mar 2019)

  Revision: 96f5b4f9607b89e312c9e272d865863788240ea4
  https://github.com/ranguba/chupa-text/commit/96f5b4f9607b89e312c9e272d865863788240ea4

  Message:
    xml: handle error

  Modified files:
    lib/chupa-text/decomposers/xml.rb
    test/decomposers/test-xml.rb

  Modified: lib/chupa-text/decomposers/xml.rb (+19 -3)
===================================================================
--- lib/chupa-text/decomposers/xml.rb    2019-03-01 19:05:49 +0900 (8c4689e)
+++ lib/chupa-text/decomposers/xml.rb    2019-03-02 05:32:43 +0900 (498f9d3)
@@ -1,4 +1,4 @@
-# Copyright (C) 2013  Kouhei Sutou <kou****@clear*****>
+# Copyright (C) 2013-2019  Kouhei Sutou <kou****@clear*****>
 #
 # This library is free software; you can redistribute it and/or
 # modify it under the terms of the GNU Lesser General Public
@@ -20,6 +20,8 @@ require "rexml/streamlistener"
 module ChupaText
   module Decomposers
     class XML < Decomposer
+      include Loggable
+
       registry.register("xml", self)
 
       def target?(data)
@@ -31,13 +33,27 @@ module ChupaText
         text = ""
         listener = Listener.new(text)
         data.open do |input|
-          parser = REXML::Parsers::StreamParser.new(input, listener)
-          parser.parse
+          begin
+            parser = REXML::Parsers::StreamParser.new(input, listener)
+            parser.parse
+          rescue REXML::ParseException => xml_error
+            error do
+              message = "#{log_tag} Failed to parse XML: "
+              message << "#{xml_error.class}: #{xml_error.message}\n"
+              message << xml_error.backtrace.join("\n")
+              message
+            end
+            return
+          end
         end
         text_data = TextData.new(text, :source_data => data)
         yield(text_data)
       end
 
+      private
+      def log_tag
+        "[decomposer][xml]"
+      end
       class Listener
         include REXML::StreamListener
 

  Modified: test/decomposers/test-xml.rb (+16 -1)
===================================================================
--- test/decomposers/test-xml.rb    2019-03-01 19:05:49 +0900 (bf53e64)
+++ test/decomposers/test-xml.rb    2019-03-02 05:32:43 +0900 (05697ff)
@@ -1,4 +1,4 @@
-# Copyright (C) 2013  Kouhei Sutou <kou****@clear*****>
+# Copyright (C) 2013-2019  Kouhei Sutou <kou****@clear*****>
 #
 # This library is free software; you can redistribute it and/or
 # modify it under the terms of the GNU Lesser General Public
@@ -41,6 +41,21 @@ class TestDecomposersXML < Test::Unit::TestCase
                    decompose(xml).collect(&:body))
     end
 
+    def test_invalid
+      messages = capture_log do
+        assert_equal([], decompose("<root x=/>"))
+      end
+      assert_equal([
+                     [
+                       :error,
+                       "[decomposer][xml] Failed to parse XML: " +
+                       "REXML::ParseException: " +
+                       "Missing attribute value start quote: <x>",
+                     ],
+                   ],
+                   messages)
+    end
+
     private
     def decompose(xml)
       data = ChupaText::Data.new
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.osdn.me/mailman/archives/groonga-commit/attachments/20190302/8749a203/attachment-0001.html>


More information about the Groonga-commit mailing list
Back to archive index