Rails 6.1 adds rescue_from to ActionCable::Connection::Base


ActionCable by default, catches any unhandled exceptions and logs it to Rails logger.

Rails 6.1 added rescue_from on ActionCable::Connection::Base, which allows intercepting these global uncaught exceptions on the websocket, so we can intercept them for other purposes like better error reporting or integrating with other tools.

Here is an example of usage:

module ApplicationCable
  class Connection < ActionCable::Connection::Base
    rescue_from UnhandledError, with: :error_handler

    def connect
      #
    end

    private

    def error_handler(e)
      # logging, reporting or custom handling exception
    end
  end
end

We can also handle exceptions using rescue_from on specific channel, just like ActionCable::Connection::Base

class CommentaryChannel < ApplicationCable::Channel
  rescue_from UnhandledError, with: :error_handler

  def subscribed
    #
  end

  private

  def error_handler
    # handle error
  end
end