Class: LambdaLoadout::ErrorHandler

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

Overview

Error handler for Lambda functions

Automatically captures exceptions, logs them, and creates error metrics.

Examples:

Basic usage

handler = LambdaLoadout::ErrorHandler.new(
  logger: logger,
  metrics: metrics,
  capture_stack_trace: true
)

def lambda_handler(event:, context:)
  handler.handle(context) do
    # Your code that might raise errors
    process_event(event)
  end
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(logger: nil, metrics: nil, capture_stack_trace: true, error_metric_name: 'LambdaError') ⇒ ErrorHandler

Initialize error handler

Parameters:

  • logger (LambdaLoadout::Logger) (defaults to: nil)

    Logger instance

  • metrics (LambdaLoadout::Metrics) (defaults to: nil)

    Metrics instance

  • capture_stack_trace (Boolean) (defaults to: true)

    Whether to include stack trace in logs

  • error_metric_name (String) (defaults to: 'LambdaError')

    Name of error metric (default: “LambdaError”)



45
46
47
48
49
50
# File 'lib/lambda_loadout/errors.rb', line 45

def initialize(logger: nil, metrics: nil, capture_stack_trace: true, error_metric_name: 'LambdaError')
  @logger = logger
  @metrics = metrics
  @capture_stack_trace = capture_stack_trace
  @error_metric_name = error_metric_name
end

Instance Attribute Details

#loggerObject (readonly)

Returns the value of attribute logger.



37
38
39
# File 'lib/lambda_loadout/errors.rb', line 37

def logger
  @logger
end

#metricsObject (readonly)

Returns the value of attribute metrics.



37
38
39
# File 'lib/lambda_loadout/errors.rb', line 37

def metrics
  @metrics
end

Instance Method Details

#capture_error(error, context = nil, **custom_fields) ⇒ void

This method returns an undefined value.

Capture and log an error

Parameters:

  • error (Exception)

    Exception to capture

  • context (Object) (defaults to: nil)

    Lambda context

  • custom_fields (Hash)

    Additional fields to log



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/lambda_loadout/errors.rb', line 71

def capture_error(error, context = nil, **custom_fields)
  error_details = {
    error_type: error.class.name,
    error_message: error.message
  }

  error_details[:stack_trace] = error.backtrace&.first(20) if @capture_stack_trace

  if context
    error_details[:function_name] = context.function_name
    error_details[:request_id] = context.aws_request_id
  end

  error_details.merge!(custom_fields)

  # Log error
  @logger&.error('Lambda execution error', **error_details)

  # Add error metric
  return unless @metrics

  @metrics.add_metric(name: @error_metric_name, unit: 'Count', value: 1)
  @metrics.add_dimension(name: 'error_type', value: error.class.name)
end

#handle(context = nil) { ... } ⇒ Object

Handle block execution with error capturing

Parameters:

  • context (Object) (defaults to: nil)

    Lambda context

Yields:

  • Block to execute

Returns:

  • (Object)

    Result of block execution

Raises:

  • Re-raises the original exception after logging and metrics



58
59
60
61
62
63
# File 'lib/lambda_loadout/errors.rb', line 58

def handle(context = nil, &block)
  block.call
rescue StandardError => e
  capture_error(e, context)
  raise
end