Class: BugReportsClient::ErrorReporter

Inherits:
Object
  • Object
show all
Defined in:
lib/bug_reports_client/error_reporter.rb

Overview

Subscribes to the Rails error reporter and turns unhandled exceptions (real 500s) into error reports on the central API, which files one GitHub issue per distinct error. Sentry-style basics without the Sentry:

- Only unhandled errors are captured, and only ones Rails would answer
with a 5xx (RecordNotFound and friends map to 4xx and are skipped).
- Errors are fingerprinted by exception class + the top application
backtrace frame (line numbers stripped, so deploys that shift code
around keep the same fingerprint).
- A cache-based throttle stops an error storm flooding the API; the
API deduplicates authoritatively by fingerprint regardless.

Enable per host with config.error_reporting_enabled = true.

Constant Summary collapse

BACKTRACE_LINES =
20

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.fingerprint_for(error) ⇒ Object

Stable identity for "the same error": class + top application frame with the line number stripped, so unrelated code changes moving lines around do not spawn new issues.



48
49
50
51
52
# File 'lib/bug_reports_client/error_reporter.rb', line 48

def self.fingerprint_for(error)
  frame = Rails.backtrace_cleaner.clean(error.backtrace || []).first.to_s
  frame = frame.sub(/:\d+:in /, ":in ")
  Digest::SHA256.hexdigest("#{error.class.name}|#{frame}")[0, 16]
end

Instance Method Details

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

Called by Rails.error for every reported error. Must never raise - error reporting failing inside a failing request would mask the original problem.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/bug_reports_client/error_reporter.rb', line 23

def report(error, handled:, severity: nil, context: {}, source: nil)
  return if handled
  return unless BugReportsClient.config.error_reporting_enabled
  return if ignored?(error)

  fingerprint = self.class.fingerprint_for(error)
  record_user_event(error, fingerprint, context)

  return unless claim!(fingerprint)

  ReportErrorJob.perform_later(
    "fingerprint" => fingerprint,
    "exception_class" => error.class.name,
    "message" => error.message.to_s.truncate(2_000),
    "backtrace" => cleaned_backtrace(error),
    "occurred_at" => Time.current.iso8601
  )
rescue StandardError => e
  Rails.logger.error "BugReportsClient: error reporter failed: #{e.class}: #{e.message}"
  nil
end