Module: Dbwatcher::Storage::Concerns::ErrorHandler

Included in:
BaseStorage
Defined in:
lib/dbwatcher/storage/concerns/error_handler.rb

Overview

Provides standardized error handling capabilities for storage classes

This concern can be included in storage classes to provide consistent error handling patterns, logging, and recovery mechanisms.

Examples:

class MyStorage < BaseStorage
  include Concerns::ErrorHandler

  def risky_operation
    safe_operation("my operation") do
      # potentially failing code
    end
  end
end

Instance Method Summary collapse

Instance Method Details

#safe_operation(operation_name, default_value = nil) { ... } ⇒ Object

Executes a block with error handling and optional default return value

Parameters:

  • operation_name (String)

    description of the operation for logging

  • default_value (Object) (defaults to: nil)

    value to return if operation fails

Yields:

  • the block to execute safely

Returns:

  • (Object)

    the result of the block or default_value on error



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/dbwatcher/storage/concerns/error_handler.rb', line 28

def safe_operation(operation_name, default_value = nil, &block)
  block.call
rescue JSON::ParserError => e
  log_error_with_exception("JSON parsing failed in #{operation_name}", e)
  default_value
rescue Errno::ENOENT => e
  log_error_with_exception("File not found in #{operation_name}", e)
  default_value
rescue Errno::EACCES => e
  log_error_with_exception("Permission denied in #{operation_name}", e)
  raise StorageError, "Permission denied: #{e.message}"
rescue StandardError => e
  log_error_with_exception("#{operation_name} failed", e)
  default_value
end

#with_error_handling(operation) { ... } ⇒ Object

Executes a block with error handling that raises StorageError on failure

Parameters:

  • operation (String)

    description of the operation

Yields:

  • the block to execute

Returns:

  • (Object)

    the result of the block

Raises:



50
51
52
53
54
55
56
# File 'lib/dbwatcher/storage/concerns/error_handler.rb', line 50

def with_error_handling(operation, &block)
  block.call
rescue StandardError => e
  error_message = "Storage #{operation} failed: #{e.message}"
  log_error_with_exception(error_message, e)
  raise StorageError, error_message
end