[Groonga-commit] ranguba/groonga-client at e7d4629 [master] load response: support errors

Zurück zum Archiv-Index

Kouhei Sutou null+****@clear*****
Fri Feb 3 17:25:59 JST 2017


Kouhei Sutou	2017-02-03 17:25:59 +0900 (Fri, 03 Feb 2017)

  New Revision: e7d4629dc465b7217093ca81b9522e828cd4b654
  https://github.com/ranguba/groonga-client/commit/e7d4629dc465b7217093ca81b9522e828cd4b654

  Message:
    load response: support errors

  Modified files:
    lib/groonga/client/response/load.rb
    test/response/test-load.rb

  Modified: lib/groonga/client/response/load.rb (+18 -0)
===================================================================
--- lib/groonga/client/response/load.rb    2017-02-03 09:55:38 +0900 (3c04193)
+++ lib/groonga/client/response/load.rb    2017-02-03 17:25:59 +0900 (307e3ba)
@@ -37,6 +37,16 @@ module Groonga
         # @since 0.3.3
         attr_accessor :loaded_ids
 
+        # @return [::Array<Groonga::Client::Response::Load::Error>]
+        #   The errors of loaded records. `error.return_code` isn't
+        #   `0` if the corresponding record is failed to load.
+        #
+        #   If you don't specify `yes` to `output_errors` `load`
+        #   parameter, this is always an empty array.
+        #
+        # @since 0.4.1
+        attr_accessor :errors
+
         def body=(body)
           super(body)
           parse_body(body)
@@ -47,11 +57,19 @@ module Groonga
           if body.is_a?(::Hash)
             @n_loaded_records = body["n_loaded_records"]
             @loaded_ids = body["loaded_ids"] || []
+            @errors = (body["errors"] || []).collect do |error|
+              Error.new(error["return_code"] || 0,
+                        error["message"])
+            end
           else
             @n_loaded_records = body
             @loaded_ids = []
+            @errors = []
           end
         end
+
+        class Error < Struct.new(:return_code, :message)
+        end
       end
     end
   end

  Modified: test/response/test-load.rb (+35 -2)
===================================================================
--- test/response/test-load.rb    2017-02-03 09:55:38 +0900 (f8116ca)
+++ test/response/test-load.rb    2017-02-03 17:25:59 +0900 (7d84f68)
@@ -1,4 +1,4 @@
-# Copyright (C) 2016  Kouhei Sutou <kou �� clear-code.com>
+# Copyright (C) 2016-2017  Kouhei Sutou <kou �� clear-code.com>
 #
 # This library is free software; you can redistribute it and/or
 # modify it under the terms of the GNU Lesser General Public
@@ -52,7 +52,7 @@ class TestResponseLoad < Test::Unit::TestCase
     end
 
     sub_test_case("command_version=3") do
-      test("no output_ids") do
+      test("no output_*") do
         command = Groonga::Command::Load.new("load",
                                              {"command_version" => "3"})
         response = create_response(command, {"n_loaded_records" => 29})
@@ -73,6 +73,39 @@ class TestResponseLoad < Test::Unit::TestCase
                                    })
         assert_equal(loaded_ids, response.loaded_ids)
       end
+
+      test("output_errors=yes") do
+        command = Groonga::Command::Load.new("load",
+                                             {
+                                               "command_version" => "3",
+                                               "output_errors" => "yes",
+                                             })
+        raw_errors = [
+          {
+            "return_code" => 0,
+            "message" => nil,
+          },
+          {
+            "return_code" => -22,
+            "message" => "invalid argument",
+          },
+          {
+            "return_code" => 0,
+            "message" => nil,
+          },
+        ]
+        errors = [
+          Groonga::Client::Response::Load::Error.new(0, nil),
+          Groonga::Client::Response::Load::Error.new(-22, "invalid argument"),
+          Groonga::Client::Response::Load::Error.new(0, nil),
+        ]
+        response = create_response(command,
+                                   {
+                                     "n_loaded_records" => 3,
+                                     "errors" => raw_errors,
+                                   })
+        assert_equal(errors, response.errors)
+      end
     end
   end
 end
-------------- next part --------------
HTML����������������������������...
Download 



More information about the Groonga-commit mailing list
Zurück zum Archiv-Index