From 72c16340bff9a79eecc2dd5e9291b199f5ae32ea Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Mon, 22 Jun 2015 16:20:06 +0200 Subject: Extract execute_command method and centralize exception handling --- lib/action_cable/connection/base.rb | 22 ++++++++++------------ lib/action_cable/connection/subscriptions.rb | 2 -- 2 files changed, 10 insertions(+), 14 deletions(-) (limited to 'lib/action_cable/connection') diff --git a/lib/action_cable/connection/base.rb b/lib/action_cable/connection/base.rb index 5951198f36..aa3eb6472d 100644 --- a/lib/action_cable/connection/base.rb +++ b/lib/action_cable/connection/base.rb @@ -39,15 +39,7 @@ module ActionCable def receive(data_in_json) if websocket_alive? - data = decode_json data_in_json - - case data['command'] - when 'subscribe' then subscriptions.add data - when 'unsubscribe' then subscriptions.remove data - when 'message' then process_message data - else - logger.error "Received unrecognized command in #{data.inspect}" - end + execute_command decode_json(data_in_json) else logger.error "Received data without a live websocket (#{data.inspect})" end @@ -113,10 +105,16 @@ module ActionCable end - def process_message(message) - subscriptions.find(message['identifier']).perform_action decode_json(message['data']) + def execute_command(data) + case data['command'] + when 'subscribe' then subscriptions.add data + when 'unsubscribe' then subscriptions.remove data + when 'message' then subscriptions.find(message['identifier']).perform_action decode_json(message['data']) + else + logger.error "Received unrecognized command in #{data.inspect}" + end rescue Exception => e - logger.error "Could not process message (#{message.inspect})" + logger.error "Could not execute command from #{data.inspect})" log_exception(e) end diff --git a/lib/action_cable/connection/subscriptions.rb b/lib/action_cable/connection/subscriptions.rb index 888b93a652..dbba8eca1d 100644 --- a/lib/action_cable/connection/subscriptions.rb +++ b/lib/action_cable/connection/subscriptions.rb @@ -19,8 +19,6 @@ module ActionCable else connection.logger.error "Subscription class not found (#{data.inspect})" end - rescue Exception => e - connection.logger.error "Could not subscribe to channel (#{data.inspect}) due to '#{e}': #{e.backtrace.join(' - ')}" end def remove(data) -- cgit v1.2.3