Sha256: 9a567d1bba79a3959f0a7c71f8d2e1e19b64ea14f05839e627b3536eaa9b2c41

Contents?: true

Size: 1.6 KB

Versions: 14

Compression:

Stored size: 1.6 KB

Contents

module Lev

  # A collection of Error objects.
  #
  class Errors < Array

    def initialize(routine_status = nil, raise_fatal_errors = false)
      @routine_status = routine_status || NullStatus.new
      @raise_fatal_errors = raise_fatal_errors
    end

    def add(fail, args={})
      args[:kind] ||= :lev
      error = Error.new(args)

      return if ignored_error_procs.any?{|proc| proc.call(error)}
      self.push(error)

      routine_status.add_error(error)

      if fail
        routine_status.failed!

        if raise_fatal_errors
          # Use special FatalError type so Routine doesn't re-add status errors
          raise Lev::FatalError, args.to_a.map { |i| i.join(' ') }.join(' - ')
        else
          throw :fatal_errors_encountered
        end
      end
    end

    def ignore(arg)
      proc = arg.is_a?(Symbol) ?
               Proc.new{|error| error.code == arg} :
               arg

      raise Lev.configuration.illegal_argument_error if !proc.respond_to?(:call)

      ignored_error_procs.push(proc)
    end

    def [](key)
      self[key]
    end

    # Checks to see if the provided input is associated with one of the errors.
    def has_offending_input?(input)
      self.any? {|error| [error.offending_inputs].flatten.include? input}
    end

    def raise_exception_if_any!(exception_type = StandardError)
      raise exception_type, map(&:message).join('; ') if any?
    end

    def full_messages
      map(&:full_message)
    end

    protected

    attr_reader :routine_status
    attr_reader :raise_fatal_errors

    def ignored_error_procs
      @ignored_error_procs ||= []
    end

  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
lev-12.1.0 lib/lev/errors.rb
lev-12.0.0 lib/lev/errors.rb
lev-11.0.0 lib/lev/errors.rb
lev-10.1.0 lib/lev/errors.rb
lev-10.0.0 lib/lev/errors.rb
lev-9.0.4 lib/lev/errors.rb
lev-9.0.3 lib/lev/errors.rb
lev-9.0.2 lib/lev/errors.rb
lev-9.0.1 lib/lev/errors.rb
lev-9.0.0 lib/lev/errors.rb
lev-8.1.0 lib/lev/errors.rb
lev-8.0.0 lib/lev/errors.rb
lev-7.1.0 lib/lev/errors.rb
lev-7.0.3 lib/lev/errors.rb