Error Handling
When an unhandled exception is raised inside a component, LiveCable replaces the component in the DOM with an error message and cleans up the server-side component and all of its children.
Where Errors Are Caught
LiveCable catches errors in these places:
- Actions — unhandled exceptions raised inside action methods
live-reactivewrites — exceptions raised by a writable variable's setterbefore_dispatchcallbacks — exceptions raised while authorizing a client message- Streaming callbacks — exceptions raised inside
stream_fromblocks - Subscribe and render — exceptions raised while the component is being set up, or while it renders
Errors from the first four are offered to the component's rescue_from handlers before the error box is shown. A message asking for an action the component doesn't list in actions, or a write to a variable that isn't writable: true, raises LiveCable::Forbidden, which is never offered to rescue_from.
Default Behaviour
In development and test environments (verbose_errors is true by default), the error message shows the component class name, the exception class and message, and a full backtrace:
MyComponent - RuntimeError: something went wrong
app/live/my_component.rb:12:in 'do_something'
...In production (verbose_errors defaults to false), a generic message is shown with no internal details:
An error occurredAll errors are also reported via Rails.error.report, so they will appear in your configured error tracker (Sentry, Honeybadger, etc.) regardless of the verbose_errors setting.
Configuration
Override the default in an initializer:
# config/initializers/live_cable.rb
LiveCable.configure do |config|
config.verbose_errors = false # never show details
# or
config.verbose_errors = true # always show details (not recommended in production)
endCleanup Behaviour
When an error is broadcast:
- All child components are destroyed first, so their
_status: destroymessages arrive before the error. - The error HTML is sent to the client via a
_errorbroadcast. - The client replaces the component element with the error HTML and unsubscribes.
- The unsubscribe triggers
LiveChannel#unsubscribed, which callsdisconnecton the server-side component to complete cleanup.
Best Practices
Unhandled errors are a last resort. Where possible, handle expected error cases gracefully inside the component:
def submit(params)
result = MyService.call(params)
if result.success?
self.status = :done
else
self.error_message = result.error
end
rescue MyService::NetworkError => e
self.error_message = "Service temporarily unavailable"
Rails.logger.warn("MyService failed: #{e.message}")
endHandling Errors with rescue_from
For error handling that spans multiple actions, components support rescue_from (from ActiveSupport::Rescuable, the same API used by ActionController::Base). Declare a handler for an exception class and, when a matching exception is raised by the component's own code - an action, a live-reactive write, or a stream_from callback - the component handles it instead of being replaced with the default error markup:
module Live
class Dashboard < LiveCable::Component
reactive :error_message, -> { nil }
rescue_from MyService::NetworkError do |error|
self.error_message = "Service temporarily unavailable"
Rails.logger.warn("MyService failed: #{error.message}")
end
actions :refresh
def refresh
self.data = MyService.fetch # may raise MyService::NetworkError
end
end
endAny reactive state the handler changes is re-rendered in the same cycle, so the component stays alive and shows your error UI rather than the fallback _error message. A handled error still answers the message, so the loading state clears even when the handler changes nothing. If no handler matches, LiveCable falls back to the default behaviour described above.
A failure while subscribing or rendering is not offered to rescue_from: there is no working render to show your error UI in, so it always gets the default error markup. Neither is a message asking for an action the component doesn't expose, or a write to a reactive variable that isn't writable: - those are rejected with the default error markup whatever handlers you declare.
Handlers can be a block or a method name, exactly as in a controller:
rescue_from ActiveRecord::RecordNotFound, with: :redirect_home