Class: PostHog::Rails::ErrorSubscriber Private

Inherits:
Object
  • Object
show all
Includes:
ParameterFilter
Defined in:
lib/posthog/rails/error_subscriber.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Rails 7.0+ error reporter integration. This integrates with Rails.error.handle and Rails.error.record.

Constant Summary

Constants included from ParameterFilter

ParameterFilter::EMPTY_HASH, ParameterFilter::MAX_DEPTH, ParameterFilter::MAX_STRING_LENGTH

Instance Method Summary collapse

Methods included from ParameterFilter

backend, #filter_sensitive_params, #safe_serialize

Instance Method Details

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

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Parameters:

  • error (Exception)

    Error reported by Rails.

  • handled (Boolean)
  • severity (Symbol, String)
  • context (Hash)
  • source (String, nil) (defaults to: nil)


20
21
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
# File 'lib/posthog/rails/error_subscriber.rb', line 20

def report(error, handled:, severity:, context:, source: nil)
  return unless PostHog::Rails.config&.auto_capture_exceptions
  return unless PostHog::Rails.config&.should_capture_exception?(error)
  # Skip if in a web request - CaptureExceptions middleware will handle it
  # with richer context (URL, params, controller, etc.)
  return if PostHog::Rails.in_web_request?

  distinct_id = context[:user_id] || context[:distinct_id]

  properties = {
    '$exception_source' => source || 'rails_error_reporter',
    '$exception_handled' => handled,
    '$exception_severity' => severity.to_s
  }

  # Add context information (safely serialized to avoid circular references)
  if context.present?
    context.each do |key, value|
      next if key.in?(%i[user_id distinct_id])

      properties["$context_#{key}"] = safe_serialize(value)
    end
  end

  PostHog.capture_exception(error, distinct_id, properties)
rescue StandardError => e
  PostHog::Logging.logger.error("Failed to report error via subscriber: #{e.message}")
  PostHog::Logging.logger.error("Backtrace: #{e.backtrace&.first(5)&.join("\n")}")
end