Exception: Lore::Exceptions::Validation_Failure

Inherits:
Exception
  • Object
show all
Defined in:
lib/lore/exceptions/validation_failure.rb

Overview

A validation failure consists of one or more Invalid_Field instances for every model field for which invalid values have been passed, e.g. on Model.create or Model.update.

Usage:

raise Validation_Failure.new(The_Model, { 
    # Generic error. Example:  :user_id => Lore.integer
      :table_foo  => Invalid_Field.new( :the_attribute => :error_type ) 
    # Constraint error. Example:  :email => :format 
      :table bar  => Unmet_Constraints.new( :the_attribute => :error_type )  
    # Type error. Example:  :user_id => Lore.integer or :user_id => :missing
      :table_batz => Invalid_Types.new( :the_attribute => :error_type ) 
})

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass, invalid_params_hash) ⇒ Validation_Failure

Returns a new instance of Validation_Failure.



29
30
31
32
33
34
35
# File 'lib/lore/exceptions/validation_failure.rb', line 29

def initialize(klass, invalid_params_hash)
    # Instances of Exception::Invalid_Field
	@invalid_fields = invalid_params_hash 
	@invalid_klass  = klass
    @message        = "#{self.class.to_s}: #{@invalid_fields.inspect}"
    log()
end

Instance Attribute Details

#invalid_fieldsObject (readonly)

Instances of Exception::Invalid_Field



25
26
27
# File 'lib/lore/exceptions/validation_failure.rb', line 25

def invalid_fields
  @invalid_fields
end

#invalid_klassObject (readonly)

Model klass that failed validation



27
28
29
# File 'lib/lore/exceptions/validation_failure.rb', line 27

def invalid_klass
  @invalid_klass
end

Instance Method Details

#inspectObject

def }}}



60
61
62
63
# File 'lib/lore/exceptions/validation_failure.rb', line 60

def inspect()
	"Model(#{@invalid_klass}) => #{@invalid_fields.serialize} " << 
	"Required: #{@invalid_klass.__attributes__.required.inspect}"
end

#logObject



37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/lore/exceptions/validation_failure.rb', line 37

def log()
  # {{{ 
    Lore.logger.error { "====== VALIDATION FAILURE ===========" }
	Lore.logger.error { "Invalid field values for klass #{@invalid_klass}: " }
	Lore.logger.error { 'Invalid field values are: ' }
    @invalid_fields.each_pair { |table, ip|
      Lore.logger.error { " |- Table: #{table}: #{ip.inspect}" }
    }
	Lore.logger.error { 'Required attributes are: ' } 
    @invalid_klass.__attributes__.required.each_pair { |table, ip|
      Lore.logger.error { " |- Table: #{table}: #{ip.inspect}" } 
    }
end

#serializeObject Also known as: explain

}}}



51
52
53
54
55
56
57
58
# File 'lib/lore/exceptions/validation_failure.rb', line 51

def serialize() 
  # {{{
	serials = {}
	@invalid_fields.each_pair { |table, invalid_param|
		serials[table] = invalid_param.serialize
	}
	return serials
end