Class: LambdaLoadout::ErrorHandler
- Inherits:
-
Object
- Object
- LambdaLoadout::ErrorHandler
- Defined in:
- lib/lambda_loadout/errors.rb
Overview
Error handler for Lambda functions
Automatically captures exceptions, logs them, and creates error metrics.
Instance Attribute Summary collapse
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
-
#metrics ⇒ Object
readonly
Returns the value of attribute metrics.
Instance Method Summary collapse
-
#capture_error(error, context = nil, **custom_fields) ⇒ void
Capture and log an error.
-
#handle(context = nil) { ... } ⇒ Object
Handle block execution with error capturing.
-
#initialize(logger: nil, metrics: nil, capture_stack_trace: true, error_metric_name: 'LambdaError') ⇒ ErrorHandler
constructor
Initialize error handler.
Constructor Details
#initialize(logger: nil, metrics: nil, capture_stack_trace: true, error_metric_name: 'LambdaError') ⇒ ErrorHandler
Initialize error handler
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
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
37 38 39 |
# File 'lib/lambda_loadout/errors.rb', line 37 def logger @logger end |
#metrics ⇒ Object (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
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. } 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
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 |