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:
Ryan Greenberg 2014-03-19 17:46:04 -07:00
parent d8ad5961ab
commit 3ec894d476
2 changed files with 19 additions and 8 deletions

View File

@ -35,6 +35,17 @@ class AbstractThriftClient
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 = {})
@options = DEFAULTS.merge(options)
@options[:server_retry_period] ||= 0
@ -55,14 +66,7 @@ class AbstractThriftClient
end
end
@request_count = 0
@options[:wrapped_exception_classes].each 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
self.class.create_wrapped_exception_classes(@client_class, @options[:wrapped_exception_classes])
end
# Adds a callback that will be invoked at a certain time. The valid callback types are:

View File

@ -26,6 +26,13 @@ class ThriftClientTest < Test::Unit::TestCase
assert_equal "<ThriftClient(Greeter::Client) @current_server=127.0.0.1:1463>", client.inspect
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
assert_nothing_raised do
ThriftClient.new(Greeter::Client, @servers.last, @options).greeting("someone")