Module: Findbug::RailsExt::ControllerMethods

Extended by:
ActiveSupport::Concern
Defined in:
lib/findbug/rails/controller_methods.rb

Overview

ControllerMethods provides helper methods for Rails controllers.

These methods are automatically included in all controllers via the Railtie. They let you add custom context to errors and performance data.

WHY CONTROLLER HELPERS?

When an error occurs, you often want to know:

  • Which user was affected?

  • What were the request params?

  • What was the user’s plan/tier?

  • What A/B experiment variant were they in?

These helpers let you attach this context easily:

class ApplicationController < ActionController::Base
  before_action :set_findbug_context

  def set_findbug_context
    findbug_set_user(current_user)
    findbug_set_context(
      plan: current_user&.plan,
      experiment: session[:ab_variant]
    )
  end
end

Then when an error occurs, all this context is captured automatically.

Instance Method Summary collapse

Instance Method Details

#findbug_breadcrumb(message, category: "default", data: {}) ⇒ Object

Add a breadcrumb (for debugging what happened before the error)

Breadcrumbs help you understand the sequence of events leading to an error. Think of them like a trail of breadcrumbs Hansel & Gretel left.

Examples:

findbug_breadcrumb("User logged in", category: "auth")
findbug_breadcrumb("Loaded products", category: "query", data: { count: 50 })

Parameters:

  • message (String)

    what happened

  • category (String) (defaults to: "default")

    category for grouping

  • data (Hash) (defaults to: {})

    additional data



111
112
113
114
115
116
117
118
# File 'lib/findbug/rails/controller_methods.rb', line 111

def findbug_breadcrumb(message, category: "default", data: {})
  Findbug::Capture::Context.add_breadcrumb(
    message: message,
    category: category,
    data: data,
    timestamp: Time.now.utc.iso8601(3)
  )
end

#findbug_capture(exception, extra = {}) ⇒ Object

Capture an exception manually with current context

Examples:

begin
  external_api.call
rescue ExternalAPIError => e
  findbug_capture(e, api: "payment_gateway")
  # handle gracefully
end

Parameters:

  • exception (Exception)

    the exception to capture

  • extra (Hash) (defaults to: {})

    additional context for this specific error



133
134
135
# File 'lib/findbug/rails/controller_methods.rb', line 133

def findbug_capture(exception, extra = {})
  Findbug.capture_exception(exception, extra)
end

#findbug_set_context(data = {}) ⇒ Object

Set custom context data

Examples:

findbug_set_context(
  organization_id: current_org.id,
  feature_flags: current_flags
)

Parameters:

  • data (Hash) (defaults to: {})

    key-value pairs to attach to errors



78
79
80
# File 'lib/findbug/rails/controller_methods.rb', line 78

def findbug_set_context(data = {})
  Findbug::Capture::Context.merge(data)
end

#findbug_set_user(user) ⇒ Object

Set the current user for error context

WHY A SEPARATE USER METHOD?


Users are special - they’re the most common context and have special handling (we extract id, email, username automatically).

Examples:

findbug_set_user(current_user)

Parameters:

  • user (Object)

    the user object (any object with id, email, etc.)



58
59
60
61
62
63
64
65
66
# File 'lib/findbug/rails/controller_methods.rb', line 58

def findbug_set_user(user)
  return unless user

  Findbug::Capture::Context.set_user(
    id: user.try(:id),
    email: user.try(:email),
    username: user.try(:username) || user.try(:name)
  )
end

#findbug_tag(key, value) ⇒ Object

Add a tag (short key-value pair for filtering)

Tags are optimized for filtering/grouping in the dashboard. Use context for detailed data, tags for filterable attributes.

Examples:

findbug_tag(:environment, "production")
findbug_tag(:region, "us-east-1")

Parameters:

  • key (String, Symbol)

    the tag key

  • value (String)

    the tag value



94
95
96
# File 'lib/findbug/rails/controller_methods.rb', line 94

def findbug_tag(key, value)
  Findbug::Capture::Context.add_tag(key, value)
end