Module: LiveCable::Connection::ErrorHandling

Extended by:
ActiveSupport::Concern
Included in:
LiveCable::Connection
Defined in:
lib/live_cable/connection/error_handling.rb

Instance Method Summary collapse

Instance Method Details

#handle_error(component, error) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/live_cable/connection/error_handling.rb', line 8

def handle_error(component, error)
  Rails.error.report(error)

  if LiveCable.configuration.verbose_errors
    summary = "#{component.class.name} - #{error.class.name}: #{ERB::Util.html_escape(error.message)}"
    backtrace_html = <<~HTML
      <small>
        <ol>
          #{error.backtrace&.map { |line| "<li>#{ERB::Util.html_escape(line)}</li>" }&.join("\n")}
        </ol>
      </small>
    HTML
  else
    summary = 'An error occurred'
  end

  html = <<~HTML
    <details>
      <summary style="color: #f00; cursor: pointer">#{summary}</summary>
      #{backtrace_html}
    </details>
  HTML

  # Destroy children first so their _status:destroy messages arrive before _error
  component.rendered_children.each(&:destroy)

  # Broadcast the error - JS replaces the DOM and calls unsubscribe(),
  # which triggers LiveChannel#unsubscribed -> component.disconnect for server cleanup
  component.broadcast(_error: html)
end