Class: Lev::ErrorTransferer

Inherits:
Object show all
Defined in:
lib/lev/error_transferer.rb

Class Method Summary collapse

Class Method Details

.transfer(source, target_routine, input_mapper, fail_if_errors = false) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/lev/error_transferer.rb', line 5

def self.transfer(source, target_routine, input_mapper, fail_if_errors=false)
  case source
  when ActiveRecord::Base, Lev::Paramifier
    source.errors.each do |error|
      target_routine.nonfatal_error(
        code: error.type,
        data: {
          model: source,
          attribute: error.attribute
        },
        kind: :activerecord,
        message: error.message,
        offending_inputs: input_mapper.map(error.attribute)
      )
    end
  when Lev::Errors
    source.each do |error|
      target_routine.nonfatal_error(
        code: error.code,
        data: error.data,
        kind: error.kind,
        message: error.message,
        offending_inputs: input_mapper.map(error.offending_inputs)
      )
    end
  else
    raise Exception
  end

  # We add nonfatal errors above and then have this call here so that all
  # errors can be transferred before we freak out.
  throw :fatal_errors_encountered if target_routine.errors? && fail_if_errors
end