Sha256: 8a15f1e8e2268385b6bc0466a59251a41969f1588f571de68fd3e4ec433bfe98

Contents?: true

Size: 955 Bytes

Versions: 1

Compression:

Stored size: 955 Bytes

Contents

require "cascade/helpers/configuration"

module Cascade
  class ErrorHandler
    extend Configuration

    define_setting :raise_parse_errors, false

    HANDLING_EXCEPTIONS = [IsoCountryCodes::UnknownCodeError, IndexError]
    DEFAULT_ERROR_STORE = ->(row, reason) do
      @errors ||= []
      @errors << [row, reason]
    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.to_s)
      raise exception if self.class.raise_parse_errors
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
cascade-rb-0.1.5 lib/cascade/error_handler.rb