lib/leadlight/service.rb in leadlight-0.0.7 vs lib/leadlight/service.rb in leadlight-0.1.0
- old
+ new
@@ -5,14 +5,15 @@
module Leadlight
module Service
extend Forwardable
- attr_reader :service_options
- fattr(:logger) { service_options.fetch(:logger) { ::Logger.new($stderr) } }
- fattr(:tints) { self.class.tints }
- fattr(:codec) { service_options.fetch(:codec) { Codec.new } }
+ attr_reader :service_options
+ attr_accessor :error_handler
+ fattr(:logger) { service_options.fetch(:logger) { ::Logger.new($stderr) } }
+ fattr(:tints) { self.class.tints }
+ fattr(:codec) { service_options.fetch(:codec) { Codec.new } }
fattr(:type_map) { TypeMap.new }
def_delegators :codec, :encode, :decode
def_delegators 'self.class', :types, :type_for_name, :request_class, :http_adapter
def_delegators :Leadlight, :common_connection_stack
@@ -20,10 +21,15 @@
def initialize(service_options={})
@service_options = service_options
execute_hook(:on_init, self)
end
+ def on_error(&error_handler)
+ self.error_handler = error_handler
+ self
+ end
+
def root
get('/') do |r|
return r
end
end
@@ -55,9 +61,10 @@
private
def perform_request(url, http_method, body=nil, options={}, &representation_handler)
req = request_class.new(self, connection, url, http_method, body, options)
+ req.on_error(&error_handler) if error_handler
if representation_handler
req.submit_and_wait(&representation_handler)
end
req
end