Code Monkey home page Code Monkey logo

Comments (3)

zzak avatar zzak commented on September 26, 2024 1

It looks like for channels, only public actions declared by the user are rescue-able and not callbacks like #subscribed:

# Extract the action name from the passed data and process it via the channel.
# The process will ensure that the action requested is a public method on the
# channel declared by the user (so not one of the callbacks like #subscribed).
def perform_action(data)
action = extract_action(data)
if processable_action?(action)
payload = { channel_class: self.class.name, action: action, data: data }
ActiveSupport::Notifications.instrument("perform_action.action_cable", payload) do
dispatch_action(action, data)
end
else
logger.error "Unable to process #{action_signature(action, data)}"
end
end

For connections since the test works, I think there isn't a problem here.
There is already a test for this:

test "accessing exceptions thrown during command execution" do
run_in_eventmachine do
setup_connection
subscribe_to_chat_channel
data = { "content" => "Hello World!", "action" => "throw_exception" }
@subscriptions.execute_command "command" => "message", "identifier" => @chat_identifier, "data" => ActiveSupport::JSON.encode(data)
exception = @connection.exceptions.first
assert_kind_of ChatChannelError, exception
end
end

Maybe we could add a test for not supporting rescue_from in non-user callbacks (like subscribed).

from rails.

Buitragox avatar Buitragox commented on September 26, 2024

only public actions declared by the user are rescue-able

Looks like only #subscribed and #unsubscribed are not rescue-able.

You can still use rescue_from by doing something like this:

def subscribed
    # ...
rescue StandardError => e
    rescue_with_handler(e)
end

But I still find it a bit confusing that these two actions are exceptions to rescue_from, as their behavior is defined inside the channel.

This is the current #subscribe_to_channel method which calls #subscribed:

def subscribe_to_channel
run_callbacks :subscribe do
subscribed
end
reject_subscription if subscription_rejected?
ensure_confirmation_sent
end

Maybe using rescue_with_handler in this method could be a good solution?

def subscribe_to_channel
  run_callbacks :subscribe do
    subscribed
  end
rescue Exception => exception
  rescue_with_handler(exception) || raise
ensure
  reject_subscription if subscription_rejected?
  ensure_confirmation_sent
end

And something similar with #unsubscribe_from_channel.

from rails.

rails-bot avatar rails-bot commented on September 26, 2024

This issue has been automatically marked as stale because it has not been commented on for at least three months.
The resources of the Rails team are limited, and so we are asking for your help.
If you can still reproduce this error on the 7-2-stable branch or on main, please reply with all of the information you have about it in order to keep the issue open.
Thank you for all your contributions.

from rails.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.