erratum 1.0.0 → 1.1.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: e2da98504e0fc6cdcd8b3ce54cba33ceb9fc7858
4
- data.tar.gz: 870ea8ac1b0ec66795a7eed5afa465d3d5eda9c6
3
+ metadata.gz: 982e57d709215c3038efc5bec08ee2c2fe3ec196
4
+ data.tar.gz: 7bb38ea005d8ca877560a156ae4ee28f897b4741
5
5
  SHA512:
6
- metadata.gz: 9eab5d1b08efc6a0ebaaef24900b5f600eba5a55723a87f189d14f597c609c62ffd0f1c2bf7c107b2b7fb6464e41a644c66f9b32f6f78ffdb76f623688a4978a
7
- data.tar.gz: 94254658a01cf4bb7eb62efe55d132e8bdd542e27328c0eaba6f012c1ac655895e801ebdd528361e2e50391f7a5b5f0dfbea5a8668598a585b5af107e0dc4cfd
6
+ metadata.gz: 5251259e5374837ab0d30bdd78b90345c85c30754efc395b70041a0b785b2412084f631eeed8e912d2af0dd7da2f796fd1574ddd84a5ea71bebb09a9f7adba5f
7
+ data.tar.gz: 075e5ce7e21137754139f8c67a5afb691a501ad48186e9af9f348c76be4970d25d26cab979596da5f8fe299970f638b9bc346f97fb939db62c0c2a8242b9d5a6
checksums.yaml.gz.sig CHANGED
Binary file
data/lib/erratum/error.rb CHANGED
@@ -31,20 +31,16 @@ module Error
31
31
 
32
32
  def as_json(_options = {})
33
33
  {
34
- errors: [
35
- {
36
- id: id,
37
- links: {
38
- about: external_documentation_url,
39
- documentation: developer_documentation_url,
40
- },
41
- status: http_status,
42
- code: code,
43
- title: title,
44
- detail: detail,
45
- source: source,
46
- },
47
- ],
34
+ id: id,
35
+ links: {
36
+ about: external_documentation_url,
37
+ documentation: developer_documentation_url,
38
+ },
39
+ status: http_status,
40
+ code: code,
41
+ title: title,
42
+ detail: detail,
43
+ source: source,
48
44
  }
49
45
  end
50
46
 
@@ -1,4 +1,5 @@
1
1
  # frozen_string_literal: true
2
+ require 'erratum/error'
2
3
  require 'erratum/errors/crud_error'
3
4
 
4
5
  class Erratum
@@ -7,23 +8,26 @@ class ResourcePersistenceError < RuntimeError
7
8
  include Error
8
9
  include CrudError
9
10
 
10
- attr_accessor :errors,
11
- :attributes
11
+ attr_accessor :pointer,
12
+ :attribute,
13
+ :value
12
14
 
13
15
  def self.convert(original_error, overrides = {})
14
- initialization_parameters = {}
15
-
16
16
  case original_error.class.name
17
17
  when 'ActiveRecord::RecordInvalid',
18
18
  'ActiveRecord::RecordNotSaved'
19
19
 
20
- initialization_parameters = {
21
- attributes: original_error.record.attributes,
22
- errors: original_error.record.errors.full_messages,
23
- }
24
- end
20
+ original_error.record.errors.each_with_object([]) do |(attribute, error), errors|
21
+ initialization_parameters = {
22
+ attribute: attribute.to_s.camelize(:lower),
23
+ detail: "#{attribute.to_s.humanize} #{error}",
24
+ pointer: "/data/attributes/#{attribute}",
25
+ value: original_error.record.public_send(attribute).to_s,
26
+ }
25
27
 
26
- new(initialization_parameters.merge(overrides))
28
+ errors << new(initialization_parameters.merge(overrides))
29
+ end
30
+ end
27
31
  end
28
32
 
29
33
  def http_status
@@ -35,14 +39,15 @@ class ResourcePersistenceError < RuntimeError
35
39
  end
36
40
 
37
41
  def detail
38
- "One or more of the attributes on the #{resource_name} you attempted " \
39
- "to #{action} is invalid."
42
+ @detail || "One or more of the attributes on the #{resource_name} you attempted " \
43
+ "to #{action} is invalid."
40
44
  end
41
45
 
42
46
  def source
43
47
  {
44
- 'errors' => errors,
45
- 'attributes' => attributes,
48
+ 'pointer' => pointer,
49
+ 'parameter' => attribute,
50
+ 'value' => value,
46
51
  }
47
52
  end
48
53
  end
@@ -15,14 +15,17 @@ module RescuableResource
15
15
  erratum = Erratum.convert(exception,
16
16
  resource_name: self.class.singular_resource_name,
17
17
  action: action_name)
18
+ erratum = Array(erratum)
18
19
 
19
- render json: erratum,
20
- status: erratum.http_status
20
+ render json: Hash['errors' => erratum.as_json],
21
+ status: erratum.first.http_status
21
22
  end
22
23
 
23
24
  base.rescue_from 'Erratum::Error' do |exception|
24
- render json: exception,
25
- status: exception.http_status
25
+ exception = Array(exception)
26
+
27
+ render json: Hash['errors' => exception.as_json],
28
+ status: exception.first.http_status
26
29
  end
27
30
  end
28
31
  end
@@ -1,4 +1,4 @@
1
1
  # frozen_string_literal: true
2
2
  class Erratum
3
- VERSION = '1.0.0'
3
+ VERSION = '1.1.0'
4
4
  end
@@ -57,20 +57,16 @@ RSpec.describe Error do
57
57
  )
58
58
 
59
59
  expect(custom_error.as_json).to eql(
60
- errors: [
61
- {
62
- id: 'identifier',
63
- links: {
64
- about: 'jinkies/87654321',
65
- documentation: 'asimof/jibbit?version=janky',
66
- },
67
- status: 'flibbity',
68
- code: 'jibbit',
69
- title: 'roll dem bones and stones',
70
- detail: 'I cannot receive any satisfaction',
71
- source: 'But perhaps if I attempt it one more time, I can',
72
- },
73
- ],
60
+ id: 'identifier',
61
+ links: {
62
+ about: 'jinkies/87654321',
63
+ documentation: 'asimof/jibbit?version=janky',
64
+ },
65
+ status: 'flibbity',
66
+ code: 'jibbit',
67
+ title: 'roll dem bones and stones',
68
+ detail: 'I cannot receive any satisfaction',
69
+ source: 'But perhaps if I attempt it one more time, I can',
74
70
  )
75
71
  end
76
72
 
@@ -98,20 +94,16 @@ RSpec.describe Error do
98
94
  )
99
95
 
100
96
  expect(custom_error.as_json).to eql(
101
- errors: [
102
- {
103
- id: 'identifier',
104
- links: {
105
- about: 'http://example.com/edu',
106
- documentation: 'http://example.com/ddu',
107
- },
108
- status: 'flibbity',
109
- code: 'jibbit',
110
- title: 'roll dem bones and stones',
111
- detail: 'I cannot receive any satisfaction',
112
- source: 'But perhaps if I attempt it one more time, I can',
113
- },
114
- ],
97
+ id: 'identifier',
98
+ links: {
99
+ about: 'http://example.com/edu',
100
+ documentation: 'http://example.com/ddu',
101
+ },
102
+ status: 'flibbity',
103
+ code: 'jibbit',
104
+ title: 'roll dem bones and stones',
105
+ detail: 'I cannot receive any satisfaction',
106
+ source: 'But perhaps if I attempt it one more time, I can',
115
107
  )
116
108
  end
117
109
 
@@ -141,20 +133,16 @@ RSpec.describe Error do
141
133
  )
142
134
 
143
135
  expect(custom_error.as_json).to eql(
144
- errors: [
145
- {
146
- id: 'identifier',
147
- links: {
148
- about: 'hinkies/87654321',
149
- documentation: 'hasimof/jibbit?version=hanky',
150
- },
151
- status: 'flibbity',
152
- code: 'jibbit',
153
- title: 'roll dem bones and stones',
154
- detail: 'I cannot receive any satisfaction',
155
- source: 'But perhaps if I attempt it one more time, I can',
156
- },
157
- ],
136
+ id: 'identifier',
137
+ links: {
138
+ about: 'hinkies/87654321',
139
+ documentation: 'hasimof/jibbit?version=hanky',
140
+ },
141
+ status: 'flibbity',
142
+ code: 'jibbit',
143
+ title: 'roll dem bones and stones',
144
+ detail: 'I cannot receive any satisfaction',
145
+ source: 'But perhaps if I attempt it one more time, I can',
158
146
  )
159
147
  end
160
148
 
@@ -169,20 +157,16 @@ RSpec.describe Error do
169
157
  )
170
158
 
171
159
  expect(custom_error.as_json).to eql(
172
- errors: [
173
- {
174
- id: 'identifier',
175
- links: {
176
- about: nil,
177
- documentation: nil,
178
- },
179
- status: 'flibbity',
180
- code: 'jibbit',
181
- title: 'roll dem bones and stones',
182
- detail: 'I cannot receive any satisfaction',
183
- source: 'But perhaps if I attempt it one more time, I can',
184
- },
185
- ],
160
+ id: 'identifier',
161
+ links: {
162
+ about: nil,
163
+ documentation: nil,
164
+ },
165
+ status: 'flibbity',
166
+ code: 'jibbit',
167
+ title: 'roll dem bones and stones',
168
+ detail: 'I cannot receive any satisfaction',
169
+ source: 'But perhaps if I attempt it one more time, I can',
186
170
  )
187
171
  end
188
172
  end
@@ -51,33 +51,41 @@ RSpec.describe ResourcePersistenceError do
51
51
  end
52
52
 
53
53
  it 'includes the resource name and action in the source' do
54
- error = ResourcePersistenceError.new errors: 'lots of errors',
55
- attributes: 'winter is coming'
54
+ error = ResourcePersistenceError.new pointer: '/data/pointer/stuff',
55
+ attribute: 'my_attribute',
56
+ value: 'my_value'
56
57
 
57
- expect(error.source).to eql('errors' => 'lots of errors',
58
- 'attributes' => 'winter is coming')
58
+ expect(error.source).to eql('pointer' => '/data/pointer/stuff',
59
+ 'parameter' => 'my_attribute',
60
+ 'value' => 'my_value')
59
61
  end
60
62
 
61
63
  it 'can convert an "ActiveRecord::RecordNotSaved"' do
62
64
  record = ErratumTestModel.new
63
65
  record.valid?
64
66
  resource_persistence_error = ActiveRecord::RecordNotSaved.new('message', record)
65
- error = ResourcePersistenceError.
67
+ errors = ResourcePersistenceError.
66
68
  convert(resource_persistence_error)
69
+ error = errors.first
67
70
 
68
- expect(error.attributes).to eql('some_attribute' => nil)
69
- expect(error.errors).to eql ["Some attribute can't be blank"]
71
+ expect(error.pointer).to eql '/data/attributes/some_attribute'
72
+ expect(error.attribute).to eql 'someAttribute'
73
+ expect(error.value).to eql ''
74
+ expect(error.detail).to eql "Some attribute can't be blank"
70
75
  end
71
76
 
72
77
  it 'can convert an "ActiveRecord::RecordInvalid"' do
73
78
  record = ErratumTestModel.new
74
79
  record.valid?
75
80
  resource_persistence_error = ActiveRecord::RecordInvalid.new(record)
76
- error = ResourcePersistenceError.
81
+ errors = ResourcePersistenceError.
77
82
  convert(resource_persistence_error)
83
+ error = errors.first
78
84
 
79
- expect(error.attributes).to eql('some_attribute' => nil)
80
- expect(error.errors).to eql ["Some attribute can't be blank"]
85
+ expect(error.pointer).to eql '/data/attributes/some_attribute'
86
+ expect(error.attribute).to eql 'someAttribute'
87
+ expect(error.value).to eql ''
88
+ expect(error.detail).to eql "Some attribute can't be blank"
81
89
  end
82
90
  end
83
91
  end
data.tar.gz.sig CHANGED
Binary file
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: erratum
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - thegranddesign
@@ -31,7 +31,7 @@ cert_chain:
31
31
  zRIv8lqQM8QFT76rzP5SBCERwN+ltKAFbQ5/FwmZNGWYnmCP3RZMQiRnbh+9H9lh
32
32
  mlbwaYZTjgsXq6cy8N38EecewgBbZYS1IYJraE/M
33
33
  -----END CERTIFICATE-----
34
- date: 2016-05-12 00:00:00.000000000 Z
34
+ date: 2016-05-26 00:00:00.000000000 Z
35
35
  dependencies:
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: rspec
metadata.gz.sig CHANGED
Binary file