Class: Errsight::Integrations::RailsErrorReporter

Inherits:
Object
  • Object
show all
Defined in:
lib/errsight/integrations/rails_error_reporter.rb

Overview

Subscriber for Rails.error (ActiveSupport::ErrorReporter), the canonical error-reporting hook in Rails 7+. Catches errors from places our existing middleware + controller-notifications subscriber don’t reach:

- Active Job after retries are exhausted (Rails 7.1+ wraps job
  errors via Rails.error.report on death)
- Active Storage analyzer/identifier errors
- Action Mailer delivery failures
- Action Cable channel errors
- Anywhere app code calls Rails.error.handle / .record / .report

The thread-local seen-set used by CaptureMiddleware and the process_action.action_controller subscriber dedups the overlap on controller errors — Rails internally calls Rails.error.report on controller exceptions too on Rails 7+, so without this dedup a single 500 would create two issues.

Constant Summary collapse

MAX_CONTEXT_KEYS =
20
MAX_CONTEXT_VALUE_BYTES =
1_024

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.install!Object

Idempotent — Rails initializers can fire twice during certain boot paths (engines, some test harnesses) and we don’t want each report to fan out to N copies of ourselves.



27
28
29
30
31
# File 'lib/errsight/integrations/rails_error_reporter.rb', line 27

def install!
  return if @installed
  @installed = true
  ::Rails.error.subscribe(new)
end

.reset!Object

Test-only: lets a test reset state and reinstall against a fresh ActiveSupport::ErrorReporter mock.



35
36
37
# File 'lib/errsight/integrations/rails_error_reporter.rb', line 35

def reset!
  @installed = false
end

Instance Method Details

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

ActiveSupport::ErrorReporter signature has been stable across Rails 7.0–8.x. Trailing **kwargs swallows any additions in future versions so a Rails minor bump can’t crash our subscriber.



43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/errsight/integrations/rails_error_reporter.rb', line 43

def report(error, handled:, severity:, context: {}, source: nil, **)
  return unless error.is_a?(Exception)
  return if duplicate?(error)

  Errsight.capture_exception(
    error,
    metadata: (context, source, handled, severity),
    tags:     build_tags(severity, source, handled)
  )
rescue StandardError
  # Never let our reporter take down the host's request, job, or
  # mail delivery — a missed event is far less bad than a crashed
  # response.
end