Class: ErrorRadar::ErrorOccurrence

Inherits:
ApplicationRecord show all
Defined in:
app/models/error_radar/error_occurrence.rb

Overview

One row per individual error hit. Linked to an ErrorLog (the deduplicated aggregate). Only recorded when config.track_occurrences is true and the upgrade_v060 migration has been run.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.record_for(log, context: {}, backtrace: nil, http_status: nil, request_url: nil) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'app/models/error_radar/error_occurrence.rb', line 12

def self.record_for(log, context: {}, backtrace: nil, http_status: nil, request_url: nil)
  max = ErrorRadar.config.max_occurrences_per_error

  create!(
    error_log:   log,
    occurred_at: Time.current,
    context:     context.presence,
    backtrace:   Array(backtrace).join("\n").presence,
    http_status: http_status,
    request_url: request_url
  )

  if max&.positive?
    keep_ids = where(error_log_id: log.id).order(occurred_at: :desc).limit(max).pluck(:id)
    where(error_log_id: log.id).where.not(id: keep_ids).delete_all if keep_ids.any?
  end
rescue ActiveRecord::StatementInvalid
  # Table not yet created — run: bin/rails generate error_radar:upgrade_v060 && bin/rails db:migrate
rescue StandardError => e
  ErrorRadar::Tracking.warn_internal("ErrorOccurrence.record_for failed: #{e.class}: #{e.message}")
end

Instance Method Details

#context_prettyObject



34
35
36
37
38
39
# File 'app/models/error_radar/error_occurrence.rb', line 34

def context_pretty
  return '{}' if context.blank?
  JSON.pretty_generate(context.is_a?(Hash) ? context : JSON.parse(context.to_s))
rescue JSON::ParserError
  context.to_s
end