Class: RailsErrorDashboard::Commands::LogError
- Inherits:
-
Object
- Object
- RailsErrorDashboard::Commands::LogError
- Defined in:
- lib/rails_error_dashboard/commands/log_error.rb
Overview
Command: Log an error to the database This is a write operation that creates an ErrorLog record
Class Method Summary collapse
- .call(exception, context = {}) ⇒ Object
-
.call_async(exception, context = {}) ⇒ Object
Queue error logging as a background job.
Instance Method Summary collapse
- #call ⇒ Object
-
#initialize(exception, context = {}) ⇒ LogError
constructor
A new instance of LogError.
Constructor Details
#initialize(exception, context = {}) ⇒ LogError
Returns a new instance of LogError.
34 35 36 37 |
# File 'lib/rails_error_dashboard/commands/log_error.rb', line 34 def initialize(exception, context = {}) @exception = exception @context = context end |
Class Method Details
.call(exception, context = {}) ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/rails_error_dashboard/commands/log_error.rb', line 8 def self.call(exception, context = {}) # Check if async logging is enabled if RailsErrorDashboard.configuration.async_logging # For async logging, just enqueue the job # All filtering happens when the job runs call_async(exception, context) else # For sync logging, execute immediately new(exception, context).call end end |
.call_async(exception, context = {}) ⇒ Object
Queue error logging as a background job
21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/rails_error_dashboard/commands/log_error.rb', line 21 def self.call_async(exception, context = {}) # Serialize exception data for the job exception_data = { class_name: exception.class.name, message: exception., backtrace: exception.backtrace } # Enqueue the async job using ActiveJob # The queue adapter (:sidekiq, :solid_queue, :async) is configured separately AsyncErrorLoggingJob.perform_later(exception_data, context) end |
Instance Method Details
#call ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/rails_error_dashboard/commands/log_error.rb', line 39 def call # Check if this exception should be ignored return nil if should_ignore_exception?(@exception) # Check sampling rate for non-critical errors # Critical errors are ALWAYS logged regardless of sampling return nil if should_skip_due_to_sampling?(@exception) error_context = ValueObjects::ErrorContext.new(@context, @context[:source]) # Find or create application (cached lookup) application = find_or_create_application # Build error attributes truncated_backtrace = truncate_backtrace(@exception.backtrace) attributes = { application_id: application.id, error_type: @exception.class.name, message: @exception., backtrace: truncated_backtrace, user_id: error_context.user_id, request_url: error_context.request_url, request_params: error_context.request_params, user_agent: error_context.user_agent, ip_address: error_context.ip_address, platform: error_context.platform, controller_name: error_context.controller_name, action_name: error_context.action_name, occurred_at: Time.current } # Generate error hash for deduplication (including controller/action context and application) error_hash = generate_error_hash(@exception, error_context.controller_name, error_context.action_name, application.id) # Calculate backtrace signature for fuzzy matching (if column exists) if ErrorLog.column_names.include?("backtrace_signature") attributes[:backtrace_signature] = calculate_backtrace_signature_from_backtrace(truncated_backtrace) end # Add git/release info if columns exist if ErrorLog.column_names.include?("git_sha") attributes[:git_sha] = RailsErrorDashboard.configuration.git_sha || ENV["GIT_SHA"] || ENV["HEROKU_SLUG_COMMIT"] || ENV["RENDER_GIT_COMMIT"] || detect_git_sha_from_command end if ErrorLog.column_names.include?("app_version") attributes[:app_version] = RailsErrorDashboard.configuration.app_version || ENV["APP_VERSION"] || detect_version_from_file end # Find existing error or create new one # This ensures accurate occurrence tracking error_log = ErrorLog.find_or_increment_by_hash(error_hash, attributes.merge(error_hash: error_hash)) # Track individual error occurrence for co-occurrence analysis (if table exists) if defined?(ErrorOccurrence) && ErrorOccurrence.table_exists? begin ErrorOccurrence.create( error_log: error_log, occurred_at: attributes[:occurred_at], user_id: attributes[:user_id], request_id: error_context.request_id, session_id: error_context.session_id ) rescue => e RailsErrorDashboard::Logger.error("Failed to create error occurrence: #{e.}") end end # Send notifications only for new errors (not increments) # Check if this is first occurrence or error was just created if error_log.occurrence_count == 1 send_notifications(error_log) # Dispatch plugin event for new error PluginRegistry.dispatch(:on_error_logged, error_log) # Trigger notification callbacks trigger_callbacks(error_log) # Emit ActiveSupport::Notifications instrumentation events emit_instrumentation_events(error_log) else # Dispatch plugin event for error recurrence PluginRegistry.dispatch(:on_error_recurred, error_log) end # Check for baseline anomalies check_baseline_anomaly(error_log) error_log rescue => e # Don't let error logging cause more errors - fail silently # CRITICAL: Log but never propagate exception RailsErrorDashboard::Logger.error("[RailsErrorDashboard] LogError command failed: #{e.class} - #{e.}") RailsErrorDashboard::Logger.error("Original exception: #{@exception.class} - #{@exception.}") if @exception RailsErrorDashboard::Logger.error("Context: #{@context.inspect}") if @context RailsErrorDashboard::Logger.error(e.backtrace&.first(5)&.join("\n")) if e.backtrace nil # Explicitly return nil, never raise end |