Skip to content

Commit e27fea8

Browse files
committed
Add configuration option to whitelist all exceptions.
1 parent 3db9990 commit e27fea8

File tree

3 files changed

+26
-2
lines changed

3 files changed

+26
-2
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
*.gem
22
*.rbc
3+
*.sw*
34
.bundle
45
.config
56
.yardoc
@@ -19,4 +20,4 @@ tmp
1920
coverage
2021
test/log
2122
test_db
22-
test_db-journal
23+
test_db-journal

lib/jsonapi/configuration.rb

+10-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ class Configuration
2424
:allow_transactions,
2525
:include_backtraces_in_errors,
2626
:exception_class_whitelist,
27+
:whitelist_all_exceptions,
2728
:always_include_to_one_linkage_data,
2829
:always_include_to_many_linkage_data,
2930
:cache_formatters,
@@ -81,6 +82,11 @@ def initialize
8182
# the `Pundit::NotAuthorizedError` to the `exception_class_whitelist`.
8283
self.exception_class_whitelist = []
8384

85+
86+
# If enabled, will override configuration option `exception_class_whitelist`
87+
# and whitelist all exceptions.
88+
self.whitelist_all_exceptions = false
89+
8490
# Resource Linkage
8591
# Controls the serialization of resource linkage for non compound documents
8692
# NOTE: always_include_to_many_linkage_data is not currently implemented
@@ -188,7 +194,8 @@ def route_formatter
188194
end
189195

190196
def exception_class_whitelisted?(e)
191-
@exception_class_whitelist.flatten.any? { |k| e.class.ancestors.map(&:to_s).include?(k.to_s) }
197+
@whitelist_all_exceptions ||
198+
@exception_class_whitelist.flatten.any? { |k| e.class.ancestors.map(&:to_s).include?(k.to_s) }
192199
end
193200

194201
def default_processor_klass=(default_processor_klass)
@@ -221,6 +228,8 @@ def default_processor_klass=(default_processor_klass)
221228

222229
attr_writer :exception_class_whitelist
223230

231+
attr_writer :whitelist_all_exceptions
232+
224233
attr_writer :always_include_to_one_linkage_data
225234

226235
attr_writer :always_include_to_many_linkage_data

test/controllers/controller_test.rb

+14
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,20 @@ def test_exception_class_whitelist
9090
JSONAPI.configuration.exception_class_whitelist = original_whitelist
9191
end
9292

93+
def test_whitelist_all_exceptions
94+
original_config = JSONAPI.configuration.whitelist_all_exceptions
95+
$PostProcessorRaisesErrors = true
96+
assert_cacheable_get :index
97+
assert_response 500
98+
99+
JSONAPI.configuration.whitelist_all_exceptions = true
100+
assert_cacheable_get :index
101+
assert_response 403
102+
ensure
103+
$PostProcessorRaisesErrors = false
104+
JSONAPI.configuration.whitelist_all_exceptions = original_config
105+
end
106+
93107
def test_exception_includes_backtrace_when_enabled
94108
original_config = JSONAPI.configuration.include_backtraces_in_errors
95109
$PostProcessorRaisesErrors = true

0 commit comments

Comments
 (0)