human_error 1.7.1 → 1.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9a6785d88a05a06be44b03e87a0ca55447b3a275
4
- data.tar.gz: 07c82c56b9497928d5bdbcb87d7d1af70902c8aa
3
+ metadata.gz: f8ddf319ce51c10a9949de1d62c8fbd357f8dfc4
4
+ data.tar.gz: 93067b5371d13fac93253c0ccb18acffebd8d215
5
5
  SHA512:
6
- metadata.gz: ac0247e593cb8dee6d9bc3b5e3724818ad21c56e45351faab4534157ebedae77b952e7dd1f03e4a2b4a81bd660fe266967e62e793e1b5bcd718bbd1b78100ed4
7
- data.tar.gz: 9c891eebecfa3248f3d3e857291a07c2b505b61af67a18717a28809268cdc8982cddf9747ebf72bab273f1c9bb96c2fe4643250e6e8f5fe7e4c4f95d1944602d
6
+ metadata.gz: 9413d40d415fc3ce3d1c46bf1c7149164eef45cee125c3c3a8cf7338a76fac157317cbfc61395c56dc5766d5b9718e3654d10babd3d1542828b85a1ebf96317c
7
+ data.tar.gz: eb3306baace6da3fdc883b4d933641aac826669737164fc51b29040c5d112d3e97ad4f5de36cf1f04500d5658f2263a159b43dbe250435c560beca9c0ba85c43
@@ -0,0 +1,51 @@
1
+ class HumanError
2
+ module Persistable
3
+ def save!(*args)
4
+ super
5
+ rescue ActiveRecord::InvalidForeignKey => e
6
+ association_info_pattern = %r{DETAIL: Key \((.*)_id\)=\((\d+)\)}
7
+ association_name, association_id = e.message.
8
+ match(association_info_pattern) \
9
+ [1..-1]
10
+
11
+ raise HumanError::Errors::AssociationError.new(
12
+ resource_name: human_error_resource_name,
13
+ association_name: association_name,
14
+ association_id: association_id,
15
+ attributes: attributes)
16
+ rescue ActiveRecord::RecordInvalid, ActiveRecord::RecordNotSaved => e
17
+ raise HumanError::Errors::ResourcePersistenceError.new(
18
+ resource_name: human_error_resource_name,
19
+ attributes: attributes,
20
+ errors: errors.full_messages)
21
+ end
22
+
23
+ def find(*ids)
24
+ super
25
+ rescue ActiveRecord::RecordNotFound => e
26
+ ids = case e.message
27
+ when /\ACouldn't find .* without an ID\z/
28
+ []
29
+ when /\ACouldn't find .* with \'.*\'=(\d+)\z/
30
+ [$1]
31
+ when /\ACouldn't find all .* with \'.*\': ((?:\d+(?:, )?)+)\z/
32
+ $1.split(', ')
33
+ end
34
+
35
+ raise HumanError::Errors::ResourceNotFoundError.new(
36
+ resource_name: human_error_resource_name,
37
+ resource_id: ids)
38
+ end
39
+
40
+ def human_error_resource_name
41
+ last_part_of_class_name = /::(\w+)\z/
42
+
43
+ self.
44
+ class.
45
+ name[last_part_of_class_name, 1].
46
+ underscore.
47
+ humanize.
48
+ downcase
49
+ end
50
+ end
51
+ end
@@ -1,3 +1,3 @@
1
1
  class HumanError
2
- VERSION = '1.7.1'
2
+ VERSION = '1.8.0'
3
3
  end
@@ -32,6 +32,13 @@ describe ResourceNotFoundError do
32
32
  expect(error.developer_details).to eql("black leather trenchcoat_id" => 123)
33
33
  end
34
34
 
35
+ it 'can accept an array of IDs' do
36
+ error = ResourceNotFoundError.new resource_name: 'black leather trenchcoat',
37
+ resource_id: %w{123 456}
38
+
39
+ expect(error.developer_details).to eql("black leather trenchcoat_id" => %w{123 456})
40
+ end
41
+
35
42
  it 'includes the resource name and action in the friendly message' do
36
43
  error = ResourceNotFoundError.new resource_name: 'black leather trenchcoat',
37
44
  action: 'bullet time'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: human_error
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.7.1
4
+ version: 1.8.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - jfelchner
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-05-28 00:00:00.000000000 Z
11
+ date: 2014-06-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec
@@ -76,6 +76,7 @@ files:
76
76
  - lib/human_error/errors/crud_errors/resource_persistence_error.rb
77
77
  - lib/human_error/errors/request_error.rb
78
78
  - lib/human_error/knowledgebase_id_directory.rb
79
+ - lib/human_error/persistable.rb
79
80
  - lib/human_error/utilities/string.rb
80
81
  - lib/human_error/version.rb
81
82
  - spec/lib/human_error/configuration_spec.rb