Class: PostHog::Rails::CaptureExceptions

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

Overview

Middleware that captures exceptions and sends them to PostHog

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

Constructor Details

#initialize(app) ⇒ CaptureExceptions

Returns a new instance of CaptureExceptions.



11
12
13
# File 'lib/posthog/rails/capture_exceptions.rb', line 11

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/posthog/rails/capture_exceptions.rb', line 15

def call(env)
  # Signal that we're in a web request context
  # ErrorSubscriber will skip capture for web requests to avoid duplicates
  PostHog::Rails.enter_web_request

  response = @app.call(env)

  # Check if there was an exception that Rails handled
  exception = collect_exception(env)

  capture_exception(exception, env) if exception && should_capture?(exception)

  response
rescue StandardError => e
  # Capture unhandled exceptions
  capture_exception(e, env) if should_capture?(e)
  raise
ensure
  PostHog::Rails.exit_web_request
end