YUKI Hiroshi
null+****@clear*****
Thu Dec 26 11:45:02 JST 2013
YUKI Hiroshi 2013-12-26 11:45:02 +0900 (Thu, 26 Dec 2013) New Revision: 2bc696c0a8d20b9879f2f608b47f26768b53b085 https://github.com/droonga/fluent-plugin-droonga/commit/2bc696c0a8d20b9879f2f608b47f26768b53b085 Merged d5217b2: Merge branch 'master' of 192.168.1.21:~piro/groonga/fluent-plugin-droonga into HEAD Message: Handle client errors of the "add" command Copied files: lib/droonga/handler_error.rb (from lib/droonga/plugin/handler/add.rb) Modified files: lib/droonga/handler.rb lib/droonga/handler_messenger.rb lib/droonga/plugin/handler/add.rb Modified: lib/droonga/handler.rb (+6 -1) =================================================================== --- lib/droonga/handler.rb 2013-12-26 10:07:09 +0900 (4bfaafd) +++ lib/droonga/handler.rb 2013-12-26 11:45:02 +0900 (944d774) @@ -20,6 +20,7 @@ require "groonga" require "droonga/forwarder" require "droonga/handler_message" require "droonga/handler_messenger" +require "droonga/handler_error" require "droonga/pluggable" require "droonga/handler_plugin" @@ -91,7 +92,11 @@ module Droonga handler_message.validate messenger = HandlerMessenger.new(@forwarder, handler_message, @options) - plugin.process(command, handler_message, messenger) + begin + plugin.process(command, handler_message, messenger) + rescue HandlerError => error + messenger.error(error) + end end def log_tag Copied: lib/droonga/handler_error.rb (+18 -20) 59% =================================================================== --- lib/droonga/plugin/handler/add.rb 2013-12-26 10:07:09 +0900 (6890aa9) +++ lib/droonga/handler_error.rb 2013-12-26 11:45:02 +0900 (28cb2d4) @@ -1,5 +1,3 @@ -# -*- coding: utf-8 -*- -# # Copyright (C) 2013 Droonga Project # # This library is free software; you can redistribute it and/or @@ -15,30 +13,30 @@ # License along with this library; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA -require "groonga" - -require "droonga/handler_plugin" - module Droonga - class AddHandler < Droonga::HandlerPlugin - repository.register("add", self) + class HandlerError + attr_reader :message, :detail, :status - command :add - def add(message, messenger) - outputs = process_add(message.request) - messenger.emit(outputs) + def initialize(message, options={}) + @message = message + @detial = options[:detail] || nil + @status_code = options[:status_code] || default_status_code + end + + def name + self.class.to_s 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"]) + def default_status_code + 500 + end + + class HandlerClientError < self + private + def default_status_code + 400 end - [true] end end end Modified: lib/droonga/handler_messenger.rb (+18 -0) =================================================================== --- lib/droonga/handler_messenger.rb 2013-12-26 10:07:09 +0900 (cfb91d6) +++ lib/droonga/handler_messenger.rb 2013-12-26 11:45:02 +0900 (cef39f9) @@ -51,6 +51,24 @@ module Droonga end end + def error(error) + descendants =****@messa***** + raw_message =****@messa***** + if descendants.empty? + return if raw_message["replyTo"].nil? + response = raw_message.merge("statusCode" => error.status_code, + "body" => { + "name" => error.name, + "message" => error.message, + "detail" => error.detail, + }) + @replier.reply(response) + else + #XXX IMPLEMENT ME!! + raise error + end + end + def forward(message, destination) @forwarder.forward(message, destination) end Modified: lib/droonga/plugin/handler/add.rb (+29 -1) =================================================================== --- lib/droonga/plugin/handler/add.rb 2013-12-26 10:07:09 +0900 (6890aa9) +++ lib/droonga/plugin/handler/add.rb 2013-12-26 11:45:02 +0900 (117cdec) @@ -23,6 +23,30 @@ module Droonga class AddHandler < Droonga::HandlerPlugin repository.register("add", self) + class InvalidRequest < Droonga::HandlerError::HandlerClientError + end + + class MissingTable < InvalidRequest + def initialize(options={}) + super("\"table\" must be specified.", options) + end + end + + class MissingPrimaryKey < InvalidRequest + def initialize(table_name, options={}) + super("\"key\" must be specified. " + + "The table #{table_name.inspect} requires a primary key for a new record.", + options) + end + end + + class UnknownTable < InvalidRequest + def initialize(table_name, options={}) + super("The table #{table_name.inspect} does not exist in the dataset.", + options) + end + end + command :add def add(message, messenger) outputs = process_add(message.request) @@ -31,9 +55,13 @@ module Droonga private def process_add(request) + raise MissingTable.new unless request.include?("table") + table = @context[request["table"]] - return [false] unless table + raise UnknownTable.new(request["table"]) unless table + if table.support_key? + raise MissingPrimaryKey.new(request["table"]) unless request.include?("key") table.add(request["key"], request["values"]) else table.add(request["values"]) -------------- next part -------------- HTML����������������������������...Télécharger