Module: ActivityNotification::NotificationResilience

Extended by:
ActiveSupport::Concern
Included in:
Mailers::Helpers
Defined in:
lib/activity_notification/notification_resilience.rb

Overview

Provides resilient notification handling across different ORMs Handles missing notification scenarios gracefully without raising exceptions

Constant Summary collapse

ORM_EXCEPTIONS =

Exception classes for different ORMs

{
  active_record: 'ActiveRecord::RecordNotFound',
  mongoid: 'Mongoid::Errors::DocumentNotFound', 
  dynamoid: 'Dynamoid::Errors::RecordNotFound'
}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.current_ormObject

Module-level methods that delegate to class methods



49
50
51
# File 'lib/activity_notification/notification_resilience.rb', line 49

def self.current_orm
  ActivityNotification.config.orm
end

.record_not_found_exception?(exception) ⇒ Boolean

Returns:



64
65
66
67
68
69
70
71
72
# File 'lib/activity_notification/notification_resilience.rb', line 64

def self.record_not_found_exception?(exception)
  ORM_EXCEPTIONS.values.any? do |exception_name|
    begin
      exception.is_a?(exception_name.constantize)
    rescue NameError
      false
    end
  end
end

.record_not_found_exception_classObject



53
54
55
56
57
58
59
60
61
62
# File 'lib/activity_notification/notification_resilience.rb', line 53

def self.record_not_found_exception_class
  exception_name = ORM_EXCEPTIONS[current_orm]
  return nil unless exception_name
  
  begin
    exception_name.constantize
  rescue NameError
    nil
  end
end

Instance Method Details

#with_notification_resilience(notification_id = nil, context = {}) { ... } ⇒ Object?

Executes a block with resilient notification handling Catches ORM-specific “record not found” exceptions and logs them appropriately

Parameters:

  • (defaults to: nil)

    The ID of the notification being processed

  • (defaults to: {})

    Additional context for logging

Yields:

  • Block to execute with resilient handling

Returns:

  • Result of the block, or nil if notification was not found



80
81
82
83
84
85
86
87
88
89
# File 'lib/activity_notification/notification_resilience.rb', line 80

def with_notification_resilience(notification_id = nil, context = {})
  yield
rescue => exception
  if self.class.record_not_found_exception?(exception)
    log_missing_notification(notification_id, exception, context)
    nil
  else
    raise exception
  end
end