renderror 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: bf0849a4098632c1107b831164c2eb590c6bf9e0
4
- data.tar.gz: 43e8eb0da0171280e300820eee7e6597f98247c1
3
+ metadata.gz: 59e93d7cb7ba860191c0bab1f7e8506c595075b8
4
+ data.tar.gz: afccc80bde92e3cbe34352dba0ece5d0cc682562
5
5
  SHA512:
6
- metadata.gz: 2bf62640dc1aab88e8496493693bb0bd65a06fcbd5d8fc2c6d126fcd6dea7d5c93d13d6b994c1a9523ce209aa2bbc643808bf5cb5d647d79c089da28b3b71308
7
- data.tar.gz: ba1ef634c009863693e954f12bed8cc1a369938a4ce8bd8b79fbbb1501db3340702a61a0d98ad0fa9a1dcf012a2f9fc80024197904b0aa0be7864efc0bc45cf1
6
+ metadata.gz: dbac24758fba89d8d767c72c308f956988f189be827299c46577ac7d8d640b3acb7cb05f720eac83a4944067988537855826057fa8d1bacff2e9b2e4baa20190
7
+ data.tar.gz: a9b567bd35851d3c13ad8a9164c3f2c85584a34ad1f57eedac22374e5b472f16f0b1fb1754e352597bcb3d40e5b7b05401b1a51f1539e87959e331f860683015
data/README.md CHANGED
@@ -91,6 +91,51 @@ Ties into the [CanCanCan](https://github.com/CanCanCommunity/cancancan) gem to a
91
91
  }
92
92
  ```
93
93
 
94
+ #### `:conflict`
95
+
96
+ This ties into `renderror_validate` mentioned below, and allows auto rescuing from `Renderror::Conflict` errors, responding with a `Conflict` error
97
+
98
+ ```json
99
+ {
100
+ "errors": [
101
+ {
102
+ "title": "Conflict",
103
+ "status": "409",
104
+ "detail": "Incorrect type specified"
105
+ }
106
+ ]
107
+ }
108
+ ```
109
+
110
+ ## Renderror Validate
111
+
112
+ As well as providing an auto rescue functionality, Renderror can also validate request params to ensure they have the correct type and id [as specified by JSON:Api](http://jsonapi.org/format/#crud-creating-responses-409).
113
+
114
+ You can enable this in a similar way to auto rescue:
115
+
116
+ ```ruby
117
+ class ApiController < ApplicationController
118
+ renderror_validate :jsonapi_type, :jsonapi_id
119
+ end
120
+ ```
121
+
122
+ This will enable automatic type validate for all `create` and `update` actions, as well as validating that the `id` matches the query param id for all `update` requests. If this validation fails a `Renderror::Conflict` will be raised, which you can auto rescue from (as mentioned above).
123
+
124
+ ### Validating Custom Actions
125
+
126
+ Sometimes you may want to validate `jsonapi_type` or `jsonapi_id` for actions other than just create and update. If this is the case then you can simply pass them in as a before action and they will be handled the same way:
127
+
128
+ ```ruby
129
+ class UsersController < ApiController
130
+ before_action :validate_jsonapi_type, only: :change_status
131
+ before_action :validate_jsonapi_id, only: :change_status
132
+
133
+ def change_status
134
+ # Your code here
135
+ end
136
+ end
137
+ ```
138
+
94
139
  ## Additional Methods
95
140
 
96
141
  There is also a number of situations where Renderror can be triggered manually, such as when wanting to render model errors or invalid authentication errors. You can do this using the following methods.
@@ -20,3 +20,6 @@ en:
20
20
  unprocessable_entity:
21
21
  title: Unprocessable Entity
22
22
  detail: Unprocessable Entity
23
+ conflict:
24
+ title: Conflict
25
+ detail: Incorrect type specified
data/lib/renderror.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  require 'active_support'
2
2
  require 'renderror/configuration'
3
3
  require 'renderror/auto_rescue'
4
+ require 'renderror/validate'
4
5
  require 'renderror/renderer'
5
6
 
6
7
  require 'renderror/base_error'
@@ -9,6 +10,7 @@ require 'renderror/unauthorized'
9
10
  require 'renderror/forbidden'
10
11
  require 'renderror/not_found'
11
12
  require 'renderror/unprocessable_entity'
13
+ require 'renderror/conflict'
12
14
 
13
15
  module Renderror
14
16
  extend Configuration
@@ -19,6 +21,7 @@ end
19
21
  module ActionController
20
22
  class Base
21
23
  include Renderror::AutoRescue
24
+ include Renderror::Validate
22
25
  include Renderror::Renderer
23
26
  end
24
27
  end
@@ -3,12 +3,9 @@ module Renderror
3
3
  extend ActiveSupport::Concern
4
4
 
5
5
  PERMITTED_EXCEPTIONS = %i[
6
- bad_request not_found invalid_document cancan
6
+ bad_request not_found invalid_document cancan conflict
7
7
  ].freeze
8
8
 
9
- included do
10
- end
11
-
12
9
  module ClassMethods
13
10
  def renderror_auto_rescue(*exceptions)
14
11
  sanitized_exceptions(exceptions).each do |e|
@@ -52,6 +49,12 @@ module Renderror
52
49
  )
53
50
  end
54
51
  end
52
+
53
+ def rescue_conflict
54
+ rescue_from Renderror::Conflict do |exception|
55
+ render_errors(exception)
56
+ end
57
+ end
55
58
  end
56
59
  end
57
60
  end
@@ -7,12 +7,12 @@ module Renderror
7
7
  end
8
8
 
9
9
  def to_json
10
- {
11
- 'status' => status,
12
- 'title' => title,
13
- 'detail' => detail,
14
- 'pointer' => pointer
15
- }
10
+ Hash.new.tap do |hash|
11
+ hash['status'] = status
12
+ hash['title'] = title
13
+ hash['detail'] = detail
14
+ hash['source'] = { 'pointer' => pointer } if pointer
15
+ end
16
16
  end
17
17
 
18
18
  def status
@@ -0,0 +1,15 @@
1
+ module Renderror
2
+ class Conflict < Renderror::BaseError
3
+ def status
4
+ '409'
5
+ end
6
+
7
+ def default_title
8
+ I18n.t(:"renderror.conflict.title")
9
+ end
10
+
11
+ def default_detail
12
+ I18n.t(:"renderror.conflict.detail")
13
+ end
14
+ end
15
+ end
@@ -13,10 +13,15 @@ module Renderror
13
13
  resource.errors.map do |field, error|
14
14
  Renderror::UnprocessableEntity.new(
15
15
  detail: resource.errors.full_message(field, error),
16
- pointer: "/data/attributes/#{field.to_s.dasherize}"
16
+ pointer: pointer_for(field)
17
17
  )
18
18
  end
19
19
  end
20
+
21
+ def pointer_for(field)
22
+ return if field == :base
23
+ "/data/attributes/#{field.to_s.dasherize}"
24
+ end
20
25
  end
21
26
  end
22
27
  end
@@ -12,7 +12,8 @@ module Renderror
12
12
  include RenderInvalidAuthentication
13
13
 
14
14
  def render_errors(errors)
15
- render json: { errors: errors.map(&:to_json) }, status: errors[0].status
15
+ render json: { errors: Array(errors).map(&:to_json) },
16
+ status: Array(errors)[0].status
16
17
  end
17
18
  end
18
19
  end
@@ -0,0 +1,58 @@
1
+ module Renderror
2
+ module Validate
3
+ extend ActiveSupport::Concern
4
+
5
+ PERMITTED_VALIDATIONS = %i[
6
+ jsonapi_type jsonapi_id
7
+ ].freeze
8
+
9
+ included do
10
+ def validate_jsonapi_type
11
+ return unless params['data'].present?
12
+
13
+ raise Renderror::Conflict unless type_matches?
14
+ end
15
+
16
+ def type_matches?
17
+ params['data'].try(:[], 'type') == jsonapi_type
18
+ end
19
+
20
+ # This can be overwritten on a per-controller basis if the type name
21
+ # doesn't match the controller name
22
+ def jsonapi_type
23
+ controller_name.dasherize
24
+ end
25
+
26
+ def validate_jsonapi_id
27
+ return unless params['data'].present?
28
+ return if id_matches?
29
+
30
+ raise Renderror::Conflict.new(detail: 'Incorrect resource ID')
31
+ end
32
+
33
+ def id_matches?
34
+ params.dig(:data, :id) == params[:id]
35
+ end
36
+ end
37
+
38
+ module ClassMethods
39
+ def renderror_validate(*validations)
40
+ sanitized_validations(validations).each do |validation|
41
+ send("validate_#{validation}")
42
+ end
43
+ end
44
+
45
+ def sanitized_validations(validation_list)
46
+ validation_list.select { |e| PERMITTED_VALIDATIONS.include? e }
47
+ end
48
+
49
+ def validate_jsonapi_type
50
+ before_action :validate_jsonapi_type, only: %i[create update]
51
+ end
52
+
53
+ def validate_jsonapi_id
54
+ before_action :validate_jsonapi_id, only: :update
55
+ end
56
+ end
57
+ end
58
+ end
@@ -1,3 +1,3 @@
1
1
  module Renderror
2
- VERSION = '0.1.1'.freeze
2
+ VERSION = '0.1.2'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: renderror
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Isaac Norman
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-08-09 00:00:00.000000000 Z
11
+ date: 2017-10-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -111,6 +111,7 @@ files:
111
111
  - lib/renderror/bad_request.rb
112
112
  - lib/renderror/base_error.rb
113
113
  - lib/renderror/configuration.rb
114
+ - lib/renderror/conflict.rb
114
115
  - lib/renderror/forbidden.rb
115
116
  - lib/renderror/not_found.rb
116
117
  - lib/renderror/reform_unprocessable.rb
@@ -119,6 +120,7 @@ files:
119
120
  - lib/renderror/renderer.rb
120
121
  - lib/renderror/unauthorized.rb
121
122
  - lib/renderror/unprocessable_entity.rb
123
+ - lib/renderror/validate.rb
122
124
  - lib/renderror/version.rb
123
125
  - renderror.gemspec
124
126
  homepage: https://github.com/Papercloud/renderror
@@ -142,7 +144,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
142
144
  version: '0'
143
145
  requirements: []
144
146
  rubyforge_project:
145
- rubygems_version: 2.6.12
147
+ rubygems_version: 2.6.13
146
148
  signing_key:
147
149
  specification_version: 4
148
150
  summary: Render Errors Easily