Skip to content

Commit

Permalink
Extract execute_command method and centralize exception handling
Browse files Browse the repository at this point in the history
  • Loading branch information
dhh committed Jun 22, 2015
1 parent 82f1344 commit 72c1634
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 14 deletions.
22 changes: 10 additions & 12 deletions lib/action_cable/connection/base.rb
Expand Up @@ -39,15 +39,7 @@ def process

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
Expand Down Expand Up @@ -113,10 +105,16 @@ def on_close
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

Expand Down
2 changes: 0 additions & 2 deletions lib/action_cable/connection/subscriptions.rb
Expand Up @@ -19,8 +19,6 @@ def add(data)
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)
Expand Down

0 comments on commit 72c1634

Please sign in to comment.