Navigation Menu

Skip to content

Commit

Permalink
Add ability to handle errors
Browse files Browse the repository at this point in the history
  • Loading branch information
piroor committed Apr 15, 2015
1 parent 332369a commit 6d0eb2e
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
9 changes: 9 additions & 0 deletions lib/droonga/client.rb
Expand Up @@ -31,6 +31,8 @@ class Client
DEFAULT_TAG = "droonga"
DEFAULT_DATASET = "Default"

attr_writer :on_error

class << self
# Opens a new connection and yields a {Client} object to use the
# connection. The client is closed after the given block is
Expand Down Expand Up @@ -74,6 +76,9 @@ def open(options={})
# Do or do not validate input messages.
def initialize(options={})
@connection = create_connection(options)
@connection.on_error = lambda do |error|
on_error(error)
end

@completion = options[:completion] != false
@validation = options[:validation] != false
Expand Down Expand Up @@ -135,5 +140,9 @@ def do_validation(message, options={})
end
@validator.validate(message)
end

def on_error(error)
@on_error.call(error) if @on_error
end
end
end
9 changes: 9 additions & 0 deletions lib/droonga/client/connection/droonga-protocol.rb
Expand Up @@ -21,12 +21,17 @@ module Droonga
class Client
module Connection
class DroongaProtocol
attr_writer :on_error

def initialize(options={})
@host = options[:host] || "127.0.0.1"
@port = options[:port] || 24224
@tag = options[:tag] || "droonga"
@options = options
@backend = create_backend
@backend.on_error = lambda do |error|
on_error(error)
end
end

# Sends a request message and receives one or more response
Expand Down Expand Up @@ -121,6 +126,10 @@ def create_backend
backend_class = self.class.const_get(backend_name)
backend_class.new(@host, @port, @tag, @options)
end

def on_error(error)
@on_error.call(error) if @on_error
end
end
end
end
Expand Down
6 changes: 6 additions & 0 deletions lib/droonga/client/connection/http.rb
Expand Up @@ -26,6 +26,8 @@ module Droonga
class Client
module Connection
class HTTP
attr_writer :on_error

class InvalidHTTPMethodError < Error
attr_reader :http_method
attr_reader :request_message
Expand Down Expand Up @@ -179,6 +181,10 @@ def build_path(message, parameters)
"#{base_path}?#{Rack::Utils.build_nested_query(parameters)}"
end
end

def on_error(error)
@on_error.call(error) if @on_error
end
end
end
end
Expand Down

0 comments on commit 6d0eb2e

Please sign in to comment.