Extract wrapped exception class creation
The wrapped exception classes are not created until ThriftClient.new is called with the correct client class. This makes it difficult to write code like `rescue MyThrift::Client::TransportException` because the exception class may not exist yet. Callers may now use ThriftClient.create_wrapped_exception_classes to ensure the desired classes have been created.
This commit is contained in:
parent
d8ad5961ab
commit
3ec894d476
|
@ -35,6 +35,17 @@ class AbstractThriftClient
|
||||||
|
|
||||||
attr_reader :last_client, :client, :client_class, :current_server, :server_list, :options, :client_methods
|
attr_reader :last_client, :client, :client_class, :current_server, :server_list, :options, :client_methods
|
||||||
|
|
||||||
|
def self.create_wrapped_exception_classes(client_class, wrapped_exception_classes = DEFAULT_WRAPPED_ERRORS)
|
||||||
|
wrapped_exception_classes.map do |exception_klass|
|
||||||
|
name = exception_klass.to_s.split('::').last
|
||||||
|
begin
|
||||||
|
client_class.const_get(name)
|
||||||
|
rescue NameError
|
||||||
|
client_class.const_set(name, Class.new(exception_klass))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def initialize(client_class, servers, options = {})
|
def initialize(client_class, servers, options = {})
|
||||||
@options = DEFAULTS.merge(options)
|
@options = DEFAULTS.merge(options)
|
||||||
@options[:server_retry_period] ||= 0
|
@options[:server_retry_period] ||= 0
|
||||||
|
@ -55,14 +66,7 @@ class AbstractThriftClient
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@request_count = 0
|
@request_count = 0
|
||||||
@options[:wrapped_exception_classes].each do |exception_klass|
|
self.class.create_wrapped_exception_classes(@client_class, @options[:wrapped_exception_classes])
|
||||||
name = exception_klass.to_s.split('::').last
|
|
||||||
begin
|
|
||||||
@client_class.const_get(name)
|
|
||||||
rescue NameError
|
|
||||||
@client_class.const_set(name, Class.new(exception_klass))
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# Adds a callback that will be invoked at a certain time. The valid callback types are:
|
# Adds a callback that will be invoked at a certain time. The valid callback types are:
|
||||||
|
|
|
@ -26,6 +26,13 @@ class ThriftClientTest < Test::Unit::TestCase
|
||||||
assert_equal "<ThriftClient(Greeter::Client) @current_server=127.0.0.1:1463>", client.inspect
|
assert_equal "<ThriftClient(Greeter::Client) @current_server=127.0.0.1:1463>", client.inspect
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_create_wrapped_exception_classes
|
||||||
|
client = Module.new
|
||||||
|
ThriftClient.create_wrapped_exception_classes(client, [Thrift::TransportException])
|
||||||
|
assert client.const_defined?(:TransportException)
|
||||||
|
assert client.const_get(:TransportException).new.is_a?(Thrift::TransportException)
|
||||||
|
end
|
||||||
|
|
||||||
def test_live_server
|
def test_live_server
|
||||||
assert_nothing_raised do
|
assert_nothing_raised do
|
||||||
ThriftClient.new(Greeter::Client, @servers.last, @options).greeting("someone")
|
ThriftClient.new(Greeter::Client, @servers.last, @options).greeting("someone")
|
||||||
|
|
Loading…
Reference in New Issue