Class: RailsErrorDashboard::Middleware::ErrorCatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_error_dashboard/middleware/error_catcher.rb

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ ErrorCatcher

Returns a new instance of ErrorCatcher.



18
19
20
# File 'lib/rails_error_dashboard/middleware/error_catcher.rb', line 18

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/rails_error_dashboard/middleware/error_catcher.rb', line 22

def call(env)
  # Record request start time for duration calculation
  env["rails_error_dashboard.request_start"] = Time.now.to_f

  # Initialize breadcrumb buffer for this request
  if RailsErrorDashboard.configuration.enable_breadcrumbs
    RailsErrorDashboard::Services::BreadcrumbCollector.init_buffer
  end

  @app.call(env)
rescue => exception
  # Report to Rails.error (will be logged by our ErrorReporter)
  # CRITICAL: Wrap in rescue to ensure gem failures don't break the app
  begin
    Rails.error.report(exception,
      handled: false,
      severity: :error,
      context: {
        request: ActionDispatch::Request.new(env),
        middleware: true
      },
      source: "rack.middleware"
    )
  rescue => e
    # If error reporting fails, log it but DON'T break the app
    RailsErrorDashboard::Logger.error("[RailsErrorDashboard] Middleware error reporting failed: #{e.class} - #{e.message}")
    RailsErrorDashboard::Logger.error(e.backtrace&.first(5)&.join("\n")) if e.backtrace
  end

  # Re-raise original exception to let Rails handle the response
  raise exception
ensure
  # CRITICAL: Always clean up thread-local storage (Puma reuses threads)
  RailsErrorDashboard::Services::BreadcrumbCollector.clear_buffer
end