[Groonga-commit] droonga/fluent-plugin-droonga at 5b8a021 [error-handling] Implement error handling for `add` command

Back to archive index

Yoji Shidara null+****@clear*****
Wed Dec 18 16:41:12 JST 2013


Yoji Shidara	2013-12-18 16:41:12 +0900 (Wed, 18 Dec 2013)

  New Revision: 5b8a021470a1da6c00746875ed0c1b80e3c1b229
  https://github.com/droonga/fluent-plugin-droonga/commit/5b8a021470a1da6c00746875ed0c1b80e3c1b229

  Message:
    Implement error handling for `add` command

  Added files:
    test/command/suite/add/nonexistent-table.expected
    test/command/suite/add/nonexistent-table.test
  Modified files:
    lib/droonga/forwarder.rb
    lib/droonga/handler.rb
    lib/droonga/handler_plugin.rb
    lib/droonga/plugin/handler/add.rb
    test/unit/plugin/handler/test_add.rb

  Modified: lib/droonga/forwarder.rb (+1 -1)
===================================================================
--- lib/droonga/forwarder.rb    2013-12-18 00:15:52 +0900 (fee5c67)
+++ lib/droonga/forwarder.rb    2013-12-18 16:41:12 +0900 (7969371)
@@ -74,7 +74,7 @@ module Droonga
       if command =~ /\.result$/
         message = {
           inReplyTo: envelope["id"],
-          statusCode: 200,
+          statusCode: envelope["statusCode"] || 200,
           type: command,
           body: body
         }

  Modified: lib/droonga/handler.rb (+5 -0)
===================================================================
--- lib/droonga/handler.rb    2013-12-18 00:15:52 +0900 (67cbb7f)
+++ lib/droonga/handler.rb    2013-12-18 16:41:12 +0900 (fefdc4b)
@@ -82,6 +82,11 @@ module Droonga
       @output_values[name] = value
     end
 
+    def emit_error(status_code, value, name = nil)
+      envelope["statusCode"] = status_code
+      emit(value, name)
+    end
+
     def post(message, destination)
       @forwarder.forward(envelope, message, destination)
     end

  Modified: lib/droonga/handler_plugin.rb (+4 -0)
===================================================================
--- lib/droonga/handler_plugin.rb    2013-12-18 00:15:52 +0900 (91bdfd8)
+++ lib/droonga/handler_plugin.rb    2013-12-18 16:41:12 +0900 (1cba5c4)
@@ -35,6 +35,10 @@ module Droonga
       @handler.emit(value, name)
     end
 
+    def emit_error(value, name=nil)
+      @handler.emit_error(value, name)
+    end
+
     def post(body, destination=nil)
       @handler.post(body, destination)
     end

  Modified: lib/droonga/plugin/handler/add.rb (+11 -8)
===================================================================
--- lib/droonga/plugin/handler/add.rb    2013-12-18 00:15:52 +0900 (38afeb5)
+++ lib/droonga/plugin/handler/add.rb    2013-12-18 16:41:12 +0900 (f8bc799)
@@ -25,20 +25,23 @@ module Droonga
 
     command :add
     def add(request)
-      outputs = process_add(request)
-      emit(outputs)
-    end
+      table_name = request["table"]
+      table = @context[table_name]
+      unless table
+        emit_error 404, {
+          "name" => "TableNotFound",
+          "message" => "Table <#{table_name}> is not found"
+        }
+        return
+      end
 
-    private
-    def process_add(request)
-      table = @context[request["table"]]
-      return [false] unless table
       if table.support_key?
         table.add(request["key"], request["values"])
       else
         table.add(request["values"])
       end
-      [true]
+
+      emit([true])
     end
   end
 end

  Added: test/command/suite/add/nonexistent-table.expected (+13 -0) 100644
===================================================================
--- /dev/null
+++ test/command/suite/add/nonexistent-table.expected    2013-12-18 16:41:12 +0900 (f761c4b)
@@ -0,0 +1,13 @@
+[
+  "droonga.message",
+  0,
+  {
+    "inReplyTo": null,
+    "statusCode": 404,
+    "type": "add.result",
+    "body": {
+      "name": "TableNotFound",
+      "message": "Table <NonExistent> is not found"
+    }
+  }
+]

  Added: test/command/suite/add/nonexistent-table.test (+10 -0) 100644
===================================================================
--- /dev/null
+++ test/command/suite/add/nonexistent-table.test    2013-12-18 16:41:12 +0900 (cef590d)
@@ -0,0 +1,10 @@
+{
+  "type": "add",
+  "dataset": "Droonga",
+  "body": {
+    "table": "NonExistent",
+    "key": "key",
+    "values": {
+    }
+  }
+}

  Modified: test/unit/plugin/handler/test_add.rb (+4 -1)
===================================================================
--- test/unit/plugin/handler/test_add.rb    2013-12-18 00:15:52 +0900 (3945a68)
+++ test/unit/plugin/handler/test_add.rb    2013-12-18 16:41:12 +0900 (d6582c4)
@@ -116,7 +116,10 @@ class AddHandlerTest < Test::Unit::TestCase
         "table"  => "Nonexistent",
         "values" => {},
       }
-      mock(@handler).emit([false])
+      mock(@handler).emit_error(404, {
+        "name" => "TableNotFound",
+        "message" => "Table <Nonexistent> is not found"
+      })
       @handler.add(request)
     end
   end
-------------- next part --------------
HTML����������������������������...
다운로드 



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