Class: Dbwatcher::Services::DiagramErrorHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/dbwatcher/services/diagram_error_handler.rb

Overview

Centralized error handling for diagram generation

Provides consistent error categorization, logging, and response formatting for all diagram generation failures with recovery strategies.

Examples:

handler = DiagramErrorHandler.new
response = handler.handle_generation_error(error, context)
# => { success: false, error_code: 'DIAGRAM_001', message: '...', recoverable: false }

Defined Under Namespace

Classes: DiagramGenerationError

Constant Summary collapse

ERROR_CODES =

Error code mapping for consistent error identification

{
  session_not_found: "DIAGRAM_001",
  invalid_diagram_type: "DIAGRAM_002",
  syntax_validation_failed: "DIAGRAM_003",
  generation_timeout: "DIAGRAM_004",
  insufficient_data: "DIAGRAM_005",
  analyzer_error: "DIAGRAM_006",
  cache_error: "DIAGRAM_007",
  system_error: "DIAGRAM_099"
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(config = {}) ⇒ DiagramErrorHandler

Initialize error handler with configuration

Parameters:

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

    error handler configuration

Options Hash (config):

  • :logger (Logger)

    logger instance

  • :include_backtrace (Boolean)

    include backtrace in logs

  • :backtrace_lines (Integer)

    number of backtrace lines to log



45
46
47
48
# File 'lib/dbwatcher/services/diagram_error_handler.rb', line 45

def initialize(config = {})
  @config = default_config.merge(config)
  @logger = @config[:logger] || default_logger
end

Instance Method Details

#handle_generation_error(error, context = {}) ⇒ Hash

Handle diagram generation error with categorization and logging

Parameters:

  • error (Exception)

    the original error

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

    additional context information

Returns:

  • (Hash)

    formatted error response



55
56
57
58
59
60
61
# File 'lib/dbwatcher/services/diagram_error_handler.rb', line 55

def handle_generation_error(error, context = {})
  error_info = categorize_error(error, context)
  log_error(error_info)

  # Return user-friendly error response
  create_error_response(error_info)
end

#recoverable_error?(error_code) ⇒ Boolean

Check if an error type is recoverable

Parameters:

  • error_code (String)

    error code

Returns:

  • (Boolean)

    true if error is recoverable



67
68
69
70
71
72
73
74
75
# File 'lib/dbwatcher/services/diagram_error_handler.rb', line 67

def recoverable_error?(error_code)
  recoverable_codes = [
    ERROR_CODES[:syntax_validation_failed],
    ERROR_CODES[:generation_timeout],
    ERROR_CODES[:insufficient_data],
    ERROR_CODES[:cache_error]
  ]
  recoverable_codes.include?(error_code)
end