Sha256: e6fb673b8740cd5c82f4d87258b63aa317b341251c36d16ce1b07b32f57d42fb

Contents?: true

Size: 931 Bytes

Versions: 3

Compression:

Stored size: 931 Bytes

Contents

require "cascade/helpers/configuration"

module Cascade
  class ErrorHandler
    extend Configuration

    define_setting :raise_parse_errors, false

    HANDLING_EXCEPTIONS = [IndexError]
    DEFAULT_ERROR_STORE = lambda do |row, exception|
      @errors ||= []
      @errors << [row, exception.to_s]
    end

    def initialize(options = {})
      @error_store = options.fetch(:error_store) { DEFAULT_ERROR_STORE }
      @handling_exceptions = options.fetch(:handling_exceptions) do
        HANDLING_EXCEPTIONS
      end
    end

    # Runs passed block with catching throwing errors and storing in ErrorStore
    #
    # @param row [Hash] the object retrieved from CSV to store it in case of
    # problems with processing
    def with_errors_handling(row)
      yield
    rescue *@handling_exceptions => exception
      @error_store.call(row, exception)
      raise exception if self.class.raise_parse_errors
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
cascade-rb-0.2.3 lib/cascade/error_handler.rb
cascade-rb-0.2.2 lib/cascade/error_handler.rb
cascade-rb-0.2.1 lib/cascade/error_handler.rb