Class: Aidp::Analyze::ErrorHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/aidp/analyze/error_handler.rb

Overview

Comprehensive error handling system for analyze mode

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(log_file: nil, verbose: false, output: nil) ⇒ ErrorHandler

Returns a new instance of ErrorHandler.



24
25
26
27
28
29
30
# File 'lib/aidp/analyze/error_handler.rb', line 24

def initialize(log_file: nil, verbose: false, output: nil)
  @output = output
  @logger = setup_logger(log_file, verbose)
  @error_counts = Hash.new(0)
  @recovery_strategies = setup_recovery_strategies
  @error_history = []
end

Instance Attribute Details

#error_countsObject (readonly)

Returns the value of attribute error_counts.



22
23
24
# File 'lib/aidp/analyze/error_handler.rb', line 22

def error_counts
  @error_counts
end

#loggerObject (readonly)

Returns the value of attribute logger.



22
23
24
# File 'lib/aidp/analyze/error_handler.rb', line 22

def logger
  @logger
end

#recovery_strategiesObject (readonly)

Returns the value of attribute recovery_strategies.



22
23
24
# File 'lib/aidp/analyze/error_handler.rb', line 22

def recovery_strategies
  @recovery_strategies
end

Instance Method Details

#cleanupObject

Cleanup and resource management



93
94
95
96
97
# File 'lib/aidp/analyze/error_handler.rb', line 93

def cleanup
  logger.info("Cleaning up error handler resources")
  @error_history.clear
  @error_counts.clear
end

#continue_with_partial_data(operation, partial_data_handler) ⇒ Object



78
79
80
# File 'lib/aidp/analyze/error_handler.rb', line 78

def continue_with_partial_data(operation, partial_data_handler)
  operation.call
end

#get_error_summaryObject

Error reporting and statistics



83
84
85
86
87
88
89
90
# File 'lib/aidp/analyze/error_handler.rb', line 83

def get_error_summary
  {
    total_errors: @error_counts.values.sum,
    error_breakdown: @error_counts,
    recent_errors: @error_history.last(10),
    recovery_success_rate: calculate_recovery_success_rate
  }
end

#handle_error(error, context: {}, step: nil, retry_count: 0) ⇒ Object

Handle errors with appropriate recovery strategies



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/aidp/analyze/error_handler.rb', line 33

def handle_error(error, context: {}, step: nil, retry_count: 0)
  error_info = {
    error: error,
    context: context,
    step: step,
    retry_count: retry_count,
    timestamp: Time.now
  }

  log_error(error_info)
  increment_error_count(error.class)
  add_to_history(error_info)

  recovery_strategy = determine_recovery_strategy(error, context)
  apply_recovery_strategy(recovery_strategy, error_info)
end

#retry_with_backoff(operation, max_retries: 3, base_delay: 1) ⇒ Object

Recovery strategies



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/aidp/analyze/error_handler.rb', line 53

def retry_with_backoff(operation, max_retries: 3, base_delay: 1)
  Aidp::Concurrency::Backoff.retry(
    max_attempts: max_retries,
    base: base_delay,
    strategy: :exponential,
    jitter: 0.2,
    on: [StandardError]
  ) do
    operation.call
  end
rescue Aidp::Concurrency::MaxAttemptsError => e
  logger.error("Operation failed after #{max_retries} retries")
  # Re-raise the MaxAttemptsError to preserve error chain and context
  raise e
end

#skip_step_with_warning(step_name, error) ⇒ Object



69
70
71
72
73
74
75
76
# File 'lib/aidp/analyze/error_handler.rb', line 69

def skip_step_with_warning(step_name, error)
  logger.warn("Skipping step '#{step_name}' due to error: #{error.message}")
  {
    status: "skipped",
    reason: error.message,
    timestamp: Time.now
  }
end