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



17
18
19
# File 'lib/activity_notification/notification_resilience.rb', line 17

def current_orm
  ActivityNotification.config.orm
end

.record_not_found_exception?(exception) ⇒ Boolean

Checks if an exception is a “record not found” exception for any supported ORM

Parameters:

  • exception (Exception)

    The exception to check

Returns:

  • (Boolean)

    True if the exception indicates a missing record



37
38
39
40
41
42
43
44
45
# File 'lib/activity_notification/notification_resilience.rb', line 37

def 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_classClass

Returns the exception class for the current ORM

Returns:

  • (Class)

    The exception class for missing records in current ORM



23
24
25
26
27
28
29
30
31
32
# File 'lib/activity_notification/notification_resilience.rb', line 23

def 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:

  • notification_id (String, Integer) (defaults to: nil)

    The ID of the notification being processed

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

    Additional context for logging

Yields:

  • Block to execute with resilient handling

Returns:

  • (Object, nil)

    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