Module: Dbwatcher::Storage::ErrorHandler

Defined in:
lib/dbwatcher/storage/errors.rb

Overview

Provides error handling capabilities for storage operations

This module can be included in storage classes to provide standardized error handling and logging capabilities.

Examples:

class MyStorage
  include 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



51
52
53
54
55
56
57
58
59
# File 'lib/dbwatcher/storage/errors.rb', line 51

def safe_operation(operation_name, default_value = nil, &block)
  block.call
rescue JSON::ParserError => e
  log_error("JSON parsing failed in #{operation_name}", e)
  default_value
rescue StandardError => e
  log_error("#{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:



67
68
69
70
71
72
# File 'lib/dbwatcher/storage/errors.rb', line 67

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