[Groonga-commit] groonga/groonga-query-log at 6bca9a0 [master] Fix unexpected NoMethodError (#42)

Back to archive index
Kentaro Hayashi null+****@clear*****
Tue Mar 12 18:04:56 JST 2019


Kentaro Hayashi	2019-03-12 18:04:56 +0900 (Tue, 12 Mar 2019)

  Revision: 6bca9a00071ab788ea19a62c577c4d8e1b090e9a
  https://github.com/groonga/groonga-query-log/commit/6bca9a00071ab788ea19a62c577c4d8e1b090e9a

  Message:
    Fix unexpected NoMethodError (#42)
    
    * Fix unexpected NoMethodError
    
    When access to document root "/" is recorded, command is set to
    nil. It causes the following error when calling command.arguments in
     #to_hash method because to_hash doesn't care command==nil case.
    
      NoMethodError: undefined method `arguments' for nil:NilClass
    
    * Remove needless assignment

  Modified files:
    lib/groonga-query-log/statistic.rb
    test/test-parser.rb

  Modified: lib/groonga-query-log/statistic.rb (+16 -7)
===================================================================
--- lib/groonga-query-log/statistic.rb    2019-02-22 12:41:58 +0900 (d7c324b)
+++ lib/groonga-query-log/statistic.rb    2019-03-12 18:04:56 +0900 (38072b4)
@@ -121,14 +121,17 @@ module GroongaQueryLog
         "return_code" => return_code,
         "slow" => slow?,
       }
-      arguments = command.arguments.collect do |key, value|
-        {"key" => key, "value" => value}
+      if command
+        data["command"] = {
+          "raw" => raw_command,
+          "name" => command.name,
+          "parameters" => command_arguments,
+        }
+      else
+        data["command"] = {
+          "raw" => raw_command
+        }
       end
-      data["command"] = {
-        "raw" => raw_command,
-        "name" => command.name,
-        "parameters" => arguments,
-      }
       operations = []
       each_operation do |operation|
         operation_data = {}
@@ -192,5 +195,11 @@ module GroongaQueryLog
     def slow_operation?(elapsed)
       elapsed >= @slow_operation_threshold
     end
+
+    def command_arguments
+      command.arguments.collect do |key, value|
+        {"key" => key, "value" => value}
+      end
+    end
   end
 end

  Modified: test/test-parser.rb (+11 -0)
===================================================================
--- test/test-parser.rb    2019-02-22 12:41:58 +0900 (9e63862)
+++ test/test-parser.rb    2019-03-12 18:04:56 +0900 (c020d96)
@@ -43,6 +43,17 @@ class ParserTest < Test::Unit::TestCase
     assert_equal([nil], statistics.collect(&:command))
   end
 
+  def test_no_command_to_hash
+    statistics = parse(<<-LOG)
+2012-12-13 11:15:20.628105|0x7fff148c8a50|>/
+2012-12-13 11:15:21.645119|0x7fff148c8a50|<000000017041150 rc=0
+    LOG
+    expected = {
+      "raw" => "/"
+    }
+    assert_equal(expected, statistics[0].to_hash["command"])
+  end
+
   private
   def parse(log)
     statistics = []
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.osdn.me/mailman/archives/groonga-commit/attachments/20190312/279d1209/attachment-0001.html>


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