class Bugsnag::Railtie

Constants

FRAMEWORK_ATTRIBUTES

Public Class Methods

rescue_in_active_record_callbacks?() click to toggle source

Do we need to rescue (& notify) in Active Record callbacks?

On Rails versions < 4.2, Rails did not raise errors in AR callbacks On Rails version 4.2, a config option was added to control this On Rails version 5.0, the config option was removed and errors in callbacks always bubble up

@api private

# File lib/bugsnag/integrations/railtie.rb, line 62
def self.rescue_in_active_record_callbacks?
  # Rails 5+ will re-raise errors in callbacks, so we don't need to rescue them
  return false if ::Rails::VERSION::MAJOR > 4

  # before 4.2, errors were always swallowed, so we need to rescue them
  return true if ::Rails::VERSION::MAJOR < 4

  # a config option was added in 4.2 to control this, but won't exist in 4.0 & 4.1
  return true unless ActiveRecord::Base.respond_to?(:raise_in_transactional_callbacks)

  # if the config option is false, we need to rescue and notify
  ActiveRecord::Base.raise_in_transactional_callbacks == false
end

Public Instance Methods

event_subscription(event) click to toggle source

Subscribes to an ActiveSupport event, leaving a breadcrumb when it triggers

@api private @param event [Hash] details of the event to subscribe to

# File lib/bugsnag/integrations/railtie.rb, line 17
def event_subscription(event)
  ActiveSupport::Notifications.subscribe(event[:id]) do |*, event_id, data|
    filtered_data = data.slice(*event[:allowed_data])
    filtered_data[:event_name] = event[:id]
    filtered_data[:event_id] = event_id

    case event[:id]
    when "sql.active_record"
      if data.key?(:binds)
        binds = data[:binds].each_with_object({}) { |bind, output| output[bind.name] = '?' if defined?(bind.name) }
        filtered_data[:binds] = JSON.dump(binds) unless binds.empty?
      end

      # Rails < 6.1 included connection_id in the event data, but now
      # includes the connection object instead
      if data.key?(:connection) && !data.key?(:connection_id)
        # the connection ID is the object_id of the connection object
        filtered_data[:connection_id] = data[:connection].object_id
      end

    when "start_processing.action_controller"
      filtered_data[:path] = Bugsnag.cleaner.clean_url(data[:path]) if data.key?(:path)

    when "redirect_to.action_controller"
      filtered_data[:location] = Bugsnag.cleaner.clean_url(data[:location]) if data.key?(:location)
    end

    Bugsnag.leave_breadcrumb(
      event[:message],
      filtered_data,
      event[:type],
      :auto
    )
  end
end