lib/saorin/adapters/servers/base.rb in saorin-0.1.4 vs lib/saorin/adapters/servers/base.rb in saorin-0.2.0

- old
+ new

@@ -25,11 +25,11 @@ responses.empty? ? nil : responses else handle_request(request) end rescue Saorin::Error => e - Response.new(nil, e) + Response.new(:error => e) end response && MultiJson.dump(response) end @@ -41,32 +41,33 @@ def handle_request(hash) begin request = Request.from_hash(hash) request.validate - result = dispatch_request request - response = Response.new(result, nil, request.id) - notify?(hash) ? nil : response + dispatch_request_with_trap_notification request rescue Saorin::InvalidRequest => e - Response.new(nil, e) + Response.new(:error => e) rescue Saorin::Error => e - Response.new(nil, e, request.id) + Response.new(:error => e, :id => request.id) rescue Exception => e - p e - Response.new(nil, Saorin::InternalError.new, request && request.id) + options = {:error => Saorin::InternalError.new} + options[:id] = request.id if request + Response.new(options) end end - def notify?(hash) - hash.is_a?(::Hash) && !hash.has_key?('id') + def dispatch_request_with_trap_notification(request) + result = dispatch_request request + Response.new(:result => result, :id => request.id) + ensure + return nil if request.notify? end def dispatch_request(request) method = request.method.to_s params = request.params || [] - unless @allowed_methods.include?(method) && - @handler.respond_to?(method) + unless @allowed_methods.include?(method) && @handler.respond_to?(method) raise Saorin::MethodNotFound end begin if params.is_a?(::Hash)