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
-
#findbug_breadcrumb(message, category: "default", data: {}) ⇒ Object
Add a breadcrumb (for debugging what happened before the error).
-
#findbug_capture(exception, extra = {}) ⇒ Object
Capture an exception manually with current context.
-
#findbug_set_context(data = {}) ⇒ Object
Set custom context data.
-
#findbug_set_user(user) ⇒ Object
Set the current user for error context.
-
#findbug_tag(key, value) ⇒ Object
Add a tag (short key-value pair for filtering).
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.
111 112 113 114 115 116 117 118 |
# File 'lib/findbug/rails/controller_methods.rb', line 111 def (, category: "default", data: {}) Findbug::Capture::Context.( message: , category: category, data: data, timestamp: Time.now.utc.iso8601(3) ) end |
#findbug_capture(exception, extra = {}) ⇒ Object
Capture an exception manually with current context
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
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).
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.
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 |