restful_json 4.4.0 → 4.5.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 +4 -4
- data/README.md +26 -21
- data/lib/restful_json/config.rb +7 -7
- data/lib/restful_json/controller.rb +12 -12
- data/lib/restful_json/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fc95c0c7d75c8d006f6bd7b03cdc10fecb5b6bfe
|
4
|
+
data.tar.gz: cd6dc9927dbb99b9fa44b033e5bd41c0a50654fe
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cfe9ef00e8b242f50a38356e09af036896e82f759896afc3c4bbd990b287e831a70b0d544672143126110c831bd7e7bed4f3b7f14caf6a00b01e137176495966
|
7
|
+
data.tar.gz: 7b26424e5d2b3cf61f0453fee47b227fae3426577cad8336d801396436748b7ff117c26e78ca39d6970b29fc081c285ae6050ae9ee4c655c7b6a4ddaa03ca9d7
|
data/README.md
CHANGED
@@ -84,7 +84,7 @@ You only define what you need to provide and it can easily integrate with common
|
|
84
84
|
In your Rails app's `Gemfile`:
|
85
85
|
|
86
86
|
```ruby
|
87
|
-
gem 'restful_json', '~> 4.
|
87
|
+
gem 'restful_json', '~> 4.5.0'
|
88
88
|
```
|
89
89
|
|
90
90
|
Then:
|
@@ -296,37 +296,40 @@ RestfulJson.configure do
|
|
296
296
|
# if not using permitters, will check respond_to?("(singular_model_name)_params".to_sym) and if true will __send__(method)
|
297
297
|
self.actions_that_permit = [:create, :update]
|
298
298
|
|
299
|
-
# will call .includes(...) for including and/or
|
299
|
+
# will call .includes(...) for including and/or including_for_action when action was generated by query_for
|
300
300
|
self.apply_includes_to_custom_queries = true
|
301
301
|
|
302
302
|
# in error JSON, break out the exception info into fields for debugging
|
303
303
|
self.return_error_data = true
|
304
304
|
|
305
305
|
# the class that is rescued in each action method, but if nil will always reraise and not handle
|
306
|
-
self.
|
306
|
+
self.rj_action_rescue_class = StandardError
|
307
307
|
|
308
308
|
# will define order of errors handled and what status and/or i18n message key to use
|
309
|
-
self.
|
309
|
+
self.rj_action_rescue_handlers = []
|
310
|
+
|
311
|
+
# default to checking for the StrongParameters default method (singular model name)_params and using it if haven't tried
|
312
|
+
self.actions_supporting_params_methods = [:create, :update]
|
310
313
|
|
311
|
-
# rescue_handlers are an ordered array of handlers to handle rescue of self.
|
314
|
+
# rescue_handlers are an ordered array of handlers to handle rescue of self.rj_action_rescue_class or sub types.
|
312
315
|
# can use optional i18n_key for message, but will default to e.message if i18n_key not found.
|
313
316
|
|
314
317
|
# support 404 error for ActiveRecord::RecordNotFound if using ActiveRecord.
|
315
318
|
begin
|
316
319
|
require 'active_record/errors'
|
317
|
-
self.
|
320
|
+
self.rj_action_rescue_handlers << {exception_classes: [ActiveRecord::RecordNotFound], status: :not_found, i18n_key: 'api.not_found'.freeze}
|
318
321
|
rescue LoadError, NameError
|
319
322
|
end
|
320
323
|
|
321
324
|
# support 403 error for CanCan::AccessDenied if using CanCan
|
322
325
|
begin
|
323
326
|
require 'cancan/exceptions'
|
324
|
-
self.
|
327
|
+
self.rj_action_rescue_handlers << {exception_classes: [CanCan::AccessDenied], status: :forbidden, i18n_key: 'api.not_found'.freeze}
|
325
328
|
rescue LoadError, NameError
|
326
329
|
end
|
327
330
|
|
328
|
-
# support 500 error for everything else that is a self.
|
329
|
-
self.
|
331
|
+
# support 500 error for everything else that is a self.rj_action_rescue_class (in action)
|
332
|
+
self.rj_action_rescue_handlers << {status: :internal_server_error, i18n_key: 'api.internal_server_error'.freeze}
|
330
333
|
|
331
334
|
end
|
332
335
|
```
|
@@ -349,7 +352,7 @@ All of the app-level configuration parameters are configurable at the controller
|
|
349
352
|
self.use_permitters = true
|
350
353
|
self.avoid_respond_with = true
|
351
354
|
self.return_error_data = true
|
352
|
-
self.
|
355
|
+
self.rj_action_rescue_class = StandardError
|
353
356
|
self.action_to_permitter = {create: nil, update: nil}
|
354
357
|
self.actions_that_authorize = [:create, :update]
|
355
358
|
self.allow_action_specific_params_methods = true
|
@@ -357,7 +360,7 @@ All of the app-level configuration parameters are configurable at the controller
|
|
357
360
|
|
358
361
|
require 'active_record/errors'
|
359
362
|
require 'cancan/exceptions'
|
360
|
-
self.
|
363
|
+
self.rj_action_rescue_handlers [
|
361
364
|
{exception_classes: [ActiveRecord::RecordNotFound], status: :not_found, i18n_key: 'api.not_found'.freeze},
|
362
365
|
{exception_classes: [CanCan::AccessDenied], status: :forbidden, i18n_key: 'api.not_found'.freeze},
|
363
366
|
{status: :internal_server_error, i18n_key: 'api.internal_server_error'.freeze}
|
@@ -929,7 +932,7 @@ To use the Rails 4 default Rack error handling, you need to remove all of the de
|
|
929
932
|
|
930
933
|
```ruby
|
931
934
|
RestfulJson.configure do
|
932
|
-
self.
|
935
|
+
self.rj_action_rescue_handlers = []
|
933
936
|
end
|
934
937
|
```
|
935
938
|
|
@@ -941,7 +944,7 @@ First, ensure that you've unset restful_json's rescue_handlers to use the Rails
|
|
941
944
|
|
942
945
|
```ruby
|
943
946
|
RestfulJson.configure do
|
944
|
-
self.
|
947
|
+
self.rj_action_rescue_handlers = []
|
945
948
|
end
|
946
949
|
```
|
947
950
|
|
@@ -988,14 +991,14 @@ The standard configuration will rescue StandardError in each action method and w
|
|
988
991
|
|
989
992
|
There are a few options to customize the rescue and error rendering behavior.
|
990
993
|
|
991
|
-
The `
|
994
|
+
The `rj_action_rescue_class` config option specifies what to rescue. Set to StandardError to behave like a normal rescue. Set to nil to just reraise everything rescued (to disable handling).
|
992
995
|
|
993
|
-
The `
|
996
|
+
The `rj_action_rescue_handlers` config option is like a minimalist set of rescue blocks that apply to every action method. For example, the following would effectively `rescue => e` (rescuing `StandardError`) and then for `ActiveRecord::RecordNotFound`, it would uses response status `:not_found` (HTTP 404). Otherwise it uses status `:internal_server_error` (HTTP 500). In both cases the error message is `e.message`:
|
994
997
|
|
995
998
|
```ruby
|
996
999
|
RestfulJson.configure do
|
997
|
-
self.
|
998
|
-
self.
|
1000
|
+
self.rj_action_rescue_class = StandardError
|
1001
|
+
self.rj_action_rescue_handlers = [
|
999
1002
|
{exception_classes: [ActiveRecord::RecordNotFound], status: :not_found},
|
1000
1003
|
{status: :internal_server_error}
|
1001
1004
|
]
|
@@ -1006,8 +1009,8 @@ In a slightly more complicated case, this configuration would catch all exceptio
|
|
1006
1009
|
|
1007
1010
|
```ruby
|
1008
1011
|
RestfulJson.configure do
|
1009
|
-
self.
|
1010
|
-
self.
|
1012
|
+
self.rj_action_rescue_class = Exception
|
1013
|
+
self.rj_action_rescue_handlers = [
|
1011
1014
|
{exception_ancestor_classes: [ActiveRecord::RecordNotFound], status: :not_found, i18n_key: 'api.not_found'.freeze},
|
1012
1015
|
{i18n_key: 'api.internal_server_error'.freeze}
|
1013
1016
|
]
|
@@ -1020,8 +1023,8 @@ You can turn off backtrace cleaning in `error_data` by setting `clean_backtrace`
|
|
1020
1023
|
|
1021
1024
|
```ruby
|
1022
1025
|
RestfulJson.configure do
|
1023
|
-
self.
|
1024
|
-
self.
|
1026
|
+
self.rj_action_rescue_class = Exception
|
1027
|
+
self.rj_action_rescue_handlers = [
|
1025
1028
|
{exception_ancestor_classes: [ActiveRecord::RecordNotFound], status: :not_found, i18n_key: 'api.not_found'.freeze},
|
1026
1029
|
{i18n_key: 'api.internal_server_error'.freeze, clean_backtrace: false}
|
1027
1030
|
]
|
@@ -1034,6 +1037,8 @@ See the [changelog][changelog] for basically what happened when, and git log for
|
|
1034
1037
|
|
1035
1038
|
### Upgrading
|
1036
1039
|
|
1040
|
+
To avoid an internal conflict with Rails' `rj_action_rescue_from` using the same variable, if you had set `rj_action_rescue_handlers` in your config or controller for restful_json use, please change the variable name to `rj_action_rescue_handlers`. Also similarly change `rj_action_rescue_class` to `rj_action_rescue_class`.
|
1041
|
+
|
1037
1042
|
The class method:
|
1038
1043
|
|
1039
1044
|
```ruby
|
data/lib/restful_json/config.rb
CHANGED
@@ -16,8 +16,8 @@ module RestfulJson
|
|
16
16
|
:actions_supporting_params_methods,
|
17
17
|
:avoid_respond_with,
|
18
18
|
:return_error_data,
|
19
|
-
:
|
20
|
-
:
|
19
|
+
:rj_action_rescue_class,
|
20
|
+
:rj_action_rescue_handlers,
|
21
21
|
:apply_includes_to_custom_queries
|
22
22
|
]
|
23
23
|
|
@@ -79,10 +79,10 @@ RestfulJson.configure do
|
|
79
79
|
self.return_error_data = true
|
80
80
|
|
81
81
|
# the class that is rescued in each action method, but if nil will always reraise and not handle
|
82
|
-
self.
|
82
|
+
self.rj_action_rescue_class = StandardError
|
83
83
|
|
84
84
|
# will define order of errors handled and what status and/or i18n message key to use
|
85
|
-
self.
|
85
|
+
self.rj_action_rescue_handlers = []
|
86
86
|
|
87
87
|
# default to checking for the StrongParameters default method (singular model name)_params and using it if haven't tried
|
88
88
|
self.actions_supporting_params_methods = [:create, :update]
|
@@ -93,18 +93,18 @@ RestfulJson.configure do
|
|
93
93
|
# support 404 error for ActiveRecord::RecordNotFound if using ActiveRecord.
|
94
94
|
begin
|
95
95
|
require 'active_record/errors'
|
96
|
-
self.
|
96
|
+
self.rj_action_rescue_handlers << {exception_classes: [ActiveRecord::RecordNotFound], status: :not_found, i18n_key: 'api.not_found'.freeze}
|
97
97
|
rescue LoadError, NameError
|
98
98
|
end
|
99
99
|
|
100
100
|
# support 403 error for CanCan::AccessDenied if using CanCan
|
101
101
|
begin
|
102
102
|
require 'cancan/exceptions'
|
103
|
-
self.
|
103
|
+
self.rj_action_rescue_handlers << {exception_classes: [CanCan::AccessDenied], status: :forbidden, i18n_key: 'api.not_found'.freeze}
|
104
104
|
rescue LoadError, NameError
|
105
105
|
end
|
106
106
|
|
107
107
|
# support 500 error for everything else that is a self.rescue_class (in action)
|
108
|
-
self.
|
108
|
+
self.rj_action_rescue_handlers << {status: :internal_server_error, i18n_key: 'api.internal_server_error'.freeze}
|
109
109
|
|
110
110
|
end
|
@@ -242,15 +242,15 @@ module RestfulJson
|
|
242
242
|
self.return_error_data
|
243
243
|
end
|
244
244
|
|
245
|
-
# Searches through self.
|
246
|
-
# self.
|
245
|
+
# Searches through self.rj_action_rescue_handlers for appropriate handler.
|
246
|
+
# self.rj_action_rescue_handlers is an array of hashes where there is key :exception_classes and/or :exception_ancestor_classes
|
247
247
|
# along with :i18n_key and :status keys.
|
248
248
|
# :exception_classes contains an array of classes to exactly match the exception.
|
249
249
|
# :exception_ancestor_classes contains an array of classes that can match an ancestor of the exception.
|
250
250
|
# If exception handled, returns hash, hopefully containing keys :i18n_key and :status.
|
251
251
|
# Otherwise, returns nil which indicates that this exception should not be handled.
|
252
252
|
def exception_handling_data(e)
|
253
|
-
self.
|
253
|
+
self.rj_action_rescue_handlers.each do |handler|
|
254
254
|
return handler if (handler.key?(:exception_classes) && handler[:exception_classes].include?(e.class))
|
255
255
|
if handler.key?(:exception_ancestor_classes)
|
256
256
|
handler[:exception_ancestor_classes].each do |ancestor|
|
@@ -264,7 +264,7 @@ module RestfulJson
|
|
264
264
|
end
|
265
265
|
|
266
266
|
def handle_or_raise(e)
|
267
|
-
raise e if self.
|
267
|
+
raise e if self.rj_action_rescue_class.nil?
|
268
268
|
handling_data = exception_handling_data(e)
|
269
269
|
raise e unless handling_data
|
270
270
|
# this is something we intended to rescue, so log it
|
@@ -342,7 +342,7 @@ module RestfulJson
|
|
342
342
|
params
|
343
343
|
end
|
344
344
|
|
345
|
-
# Renders error using handling data options (where options are probably hash from self.
|
345
|
+
# Renders error using handling data options (where options are probably hash from self.rj_action_rescue_handlers that was matched).
|
346
346
|
#
|
347
347
|
# If include_error_data? is true, it returns something like the following (with the appropriate HTTP status code via setting appropriate status in respond_do:
|
348
348
|
# {"status": "not_found",
|
@@ -539,7 +539,7 @@ module RestfulJson
|
|
539
539
|
@value = value
|
540
540
|
instance_variable_set(@model_at_plural_name_sym, @value)
|
541
541
|
render_or_respond(true)
|
542
|
-
rescue self.
|
542
|
+
rescue self.rj_action_rescue_class => e
|
543
543
|
handle_or_raise(e)
|
544
544
|
end
|
545
545
|
|
@@ -552,7 +552,7 @@ module RestfulJson
|
|
552
552
|
allowed_params
|
553
553
|
instance_variable_set(@model_at_singular_name_sym, @value)
|
554
554
|
render_or_respond(true, @value.nil? ? :not_found : :ok)
|
555
|
-
rescue self.
|
555
|
+
rescue self.rj_action_rescue_class => e
|
556
556
|
handle_or_raise(e)
|
557
557
|
end
|
558
558
|
|
@@ -565,7 +565,7 @@ module RestfulJson
|
|
565
565
|
@value = @model_class.new
|
566
566
|
instance_variable_set(@model_at_singular_name_sym, @value)
|
567
567
|
render_or_respond(true)
|
568
|
-
rescue self.
|
568
|
+
rescue self.rj_action_rescue_class => e
|
569
569
|
handle_or_raise(e)
|
570
570
|
end
|
571
571
|
|
@@ -578,7 +578,7 @@ module RestfulJson
|
|
578
578
|
allowed_params
|
579
579
|
instance_variable_set(@model_at_singular_name_sym, @value)
|
580
580
|
@value
|
581
|
-
rescue self.
|
581
|
+
rescue self.rj_action_rescue_class => e
|
582
582
|
handle_or_raise(e)
|
583
583
|
end
|
584
584
|
|
@@ -589,7 +589,7 @@ module RestfulJson
|
|
589
589
|
@value.save
|
590
590
|
instance_variable_set(@model_at_singular_name_sym, @value)
|
591
591
|
render_or_respond(false, :created)
|
592
|
-
rescue self.
|
592
|
+
rescue self.rj_action_rescue_class => e
|
593
593
|
handle_or_raise(e)
|
594
594
|
end
|
595
595
|
|
@@ -603,7 +603,7 @@ module RestfulJson
|
|
603
603
|
@value.update_attributes(p_params) unless @value.nil?
|
604
604
|
instance_variable_set(@model_at_singular_name_sym, @value)
|
605
605
|
render_or_respond(true, @value.nil? ? :not_found : :ok)
|
606
|
-
rescue self.
|
606
|
+
rescue self.rj_action_rescue_class => e
|
607
607
|
handle_or_raise(e)
|
608
608
|
end
|
609
609
|
|
@@ -625,7 +625,7 @@ module RestfulJson
|
|
625
625
|
else
|
626
626
|
render_or_respond(false)
|
627
627
|
end
|
628
|
-
rescue self.
|
628
|
+
rescue self.rj_action_rescue_class => e
|
629
629
|
handle_or_raise(e)
|
630
630
|
end
|
631
631
|
end
|
data/lib/restful_json/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: restful_json
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 4.
|
4
|
+
version: 4.5.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Gary S. Weaver
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-08-
|
12
|
+
date: 2013-08-30 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activesupport
|