-
Notifications
You must be signed in to change notification settings - Fork 21.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
rescue_from not working with subscribed method on ActionCable channel #51855
Comments
It looks like for channels, only public actions declared by the user are rescue-able and not callbacks like rails/actioncable/lib/action_cable/channel/base.rb Lines 173 to 187 in 965b8c3
For connections since the test works, I think there isn't a problem here. rails/actioncable/test/connection/subscriptions_test.rb Lines 116 to 127 in 965b8c3
Maybe we could add a test for not supporting |
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 This is the current #subscribe_to_channel method which calls #subscribed: rails/actioncable/lib/action_cable/channel/base.rb Lines 191 to 198 in 965b8c3
Maybe using 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. |
When an error is raised in the subscribed method of an ActionCable channel, it does not get handled by the method registered in rescue_from. Other methods do get handled by their registered methods with rescue_from.
When running a Rails server, I found that the exception gets handled by the connection in the execute_command method, so using rescue_from for the exception in
ApplicationCable::Connection
catches the exception. I was not able to replicate this behavior in the test below.Steps to reproduce
Raise an exception in
Channel#subscribed
and register a handler for this exception using rescue_from, just like the documentation says.Expected behavior
The exception raised in the
subscribed
method should be handled byhandle_bad_subscribe
, so the subscription is rejected.Actual behavior
The exception is not caught and the test fails.
System configuration
Rails version: 7.1.3.2
Ruby version: 3.3.0
The text was updated successfully, but these errors were encountered: