Class: Findbug::Capture::ExceptionSubscriber

Inherits:
Object
  • Object
show all
Defined in:
lib/findbug/capture/exception_subscriber.rb

Overview

ExceptionSubscriber integrates with Rails 7’s ErrorReporter.

RAILS ERROR REPORTER (Rails 7+)

Rails 7 introduced a centralized error reporting API:

Rails.error.handle { risky_operation }  # Swallows error, reports it
Rails.error.record { risky_operation }  # Re-raises, but reports first

Third-party gems can subscribe to receive ALL reported errors:

Rails.error.subscribe(MySubscriber.new)

This is better than just middleware because it catches:

  • Errors handled gracefully with Rails.error.handle

  • Background job errors

  • Errors in non-request contexts

HOW IT WORKS

  1. Rails catches an exception

  2. Rails calls Rails.error.report(exception, …)

  3. Rails calls our subscriber’s #report method

  4. We capture the exception asynchronously

Instance Method Summary collapse

Instance Method Details

#report(error, handled:, severity:, context:, source: nil) ⇒ Object

Called by Rails when an error is reported

Parameters:

  • error (Exception)

    the exception that occurred

  • handled (Boolean)

    whether the error was handled

  • severity (Symbol)

    :error, :warning, or :info

  • context (Hash)

    additional context from Rails

  • source (String) (defaults to: nil)

    where the error came from



41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/findbug/capture/exception_subscriber.rb', line 41

def report(error, handled:, severity:, context:, source: nil)
  return unless Findbug.enabled?
  return unless should_capture?(error)

  # Build event data
  event_data = build_event_data(error, handled, severity, context, source)

  # Push to Redis buffer (async, non-blocking)
  Storage::RedisBuffer.push_error(event_data)
rescue StandardError => e
  # CRITICAL: Never let Findbug crash your app
  Findbug.logger.error("[Findbug] ExceptionSubscriber failed: #{e.message}")
end