Exception: HumanError::Errors::AssociationError

Inherits:
RuntimeError
  • Object
show all
Includes:
HumanError::Error, CrudError
Defined in:
lib/human_error/errors/crud_errors/association_error.rb

Instance Attribute Summary collapse

Attributes included from CrudError

#action, #resource_id, #resource_name

Attributes included from HumanError::Error

#code, #developer_documentation_url, #external_documentation_url, #id, #message

Class Method Summary collapse

Instance Method Summary collapse

Methods included from CrudError

#initialize

Methods included from HumanError::Error

#as_json, included, #initialize, #to_json, #to_s

Instance Attribute Details

#association_idObject

Returns the value of attribute association_id.



9
10
11
# File 'lib/human_error/errors/crud_errors/association_error.rb', line 9

def association_id
  @association_id
end

#association_nameObject

Returns the value of attribute association_name.



9
10
11
# File 'lib/human_error/errors/crud_errors/association_error.rb', line 9

def association_name
  @association_name
end

#attributesObject

Returns the value of attribute attributes.



9
10
11
# File 'lib/human_error/errors/crud_errors/association_error.rb', line 9

def attributes
  @attributes
end

Class Method Details

.convert(original_error, overrides = {}) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/human_error/errors/crud_errors/association_error.rb', line 13

def self.convert(original_error, overrides = {})
  initialization_parameters = {}

  case original_error.class.name
  when 'ActiveRecord::InvalidForeignKey'
    message_info_pattern = /DETAIL:  Key \((.*)_id\)=\(([a-f0-9\-]+)\)/
    message_info         = original_error.
                             message.
                             match(message_info_pattern)[1..-1]

    initialization_parameters = {
      association_name: message_info[0],
      association_id:   message_info[1],
    }
  end

  new(initialization_parameters.merge(overrides))
end

Instance Method Details

#detailObject



40
41
42
43
# File 'lib/human_error/errors/crud_errors/association_error.rb', line 40

def detail
  "The #{association_name} that you attempted to associate with " \
  "the #{resource_name} was not valid."
end

#http_statusObject



32
33
34
# File 'lib/human_error/errors/crud_errors/association_error.rb', line 32

def http_status
  422
end

#sourceObject



45
46
47
48
49
50
# File 'lib/human_error/errors/crud_errors/association_error.rb', line 45

def source
  {
    resource_name            => attributes,
    "#{association_name} id" => association_id,
  }
end

#titleObject



36
37
38
# File 'lib/human_error/errors/crud_errors/association_error.rb', line 36

def title
  'Association Error'
end