Class: LambdaLoadout::ErrorNotifier

Inherits:
Object
  • Object
show all
Defined in:
lib/lambda_loadout/error_notifier.rb

Overview

ErrorNotifier sends detailed error notifications via SNS when Lambda functions fail

Examples:

Basic usage

notifier = LambdaLoadout::ErrorNotifier.new(
  sns_topic_arn: ENV['ERROR_NOTIFICATION_TOPIC_ARN'],
  logger: logger
)
notifier.notify(error: exception, context: context, event: event)

With custom region

notifier = LambdaLoadout::ErrorNotifier.new(
  sns_topic_arn: 'arn:aws:sns:us-west-2:123456789012:alerts',
  logger: logger,
  region: 'us-west-2'
)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sns_topic_arn:, logger:, region: nil) ⇒ ErrorNotifier

Initialize ErrorNotifier

Parameters:

  • sns_topic_arn (String)

    ARN of SNS topic for error notifications

  • logger (LambdaLoadout::Logger)

    Logger instance for logging notification status

  • region (String, nil) (defaults to: nil)

    AWS region (defaults to AWS_REGION env var or us-east-1)



31
32
33
34
35
36
# File 'lib/lambda_loadout/error_notifier.rb', line 31

def initialize(sns_topic_arn:, logger:, region: nil)
  @sns_topic_arn = sns_topic_arn
  @logger = logger
  @region = region || ENV['AWS_REGION'] || 'us-east-1'
  @sns_client = nil # Lazy initialize
end

Instance Attribute Details

#loggerObject (readonly)

Returns the value of attribute logger.



24
25
26
# File 'lib/lambda_loadout/error_notifier.rb', line 24

def logger
  @logger
end

#regionObject (readonly)

Returns the value of attribute region.



24
25
26
# File 'lib/lambda_loadout/error_notifier.rb', line 24

def region
  @region
end

#sns_topic_arnObject (readonly)

Returns the value of attribute sns_topic_arn.



24
25
26
# File 'lib/lambda_loadout/error_notifier.rb', line 24

def sns_topic_arn
  @sns_topic_arn
end

Instance Method Details

#notify(error:, context:, event:) ⇒ void

This method returns an undefined value.

Send error notification via SNS

Parameters:

  • error (Exception)

    The exception that occurred

  • context (LambdaContext)

    AWS Lambda context object

  • event (Hash)

    Lambda event hash



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
# File 'lib/lambda_loadout/error_notifier.rb', line 44

def notify(error:, context:, event:)
  return if sns_topic_arn.nil? || sns_topic_arn.empty?

  begin
    ensure_sns_client

    subject = build_subject(error, context)
    message = build_message(error, context, event)

    @sns_client.publish(
      topic_arn: sns_topic_arn,
      subject: subject,
      message: message
    )

    logger.info('Error notification sent',
                topic_arn: sns_topic_arn,
                error_class: error.class.name)
  rescue StandardError => e
    # Don't let notification errors prevent the original error from being raised
    logger.warn('Failed to send error notification', e,
                topic_arn: sns_topic_arn,
                original_error: error.class.name)
  end
end