Skip to content

Commit d35f2e5

Browse files
authored
Merge pull request #888 from Petesta/whitelist-all-exceptions
Add configuration option to whitelist all exceptions.
2 parents 3db9990 + 4178643 commit d35f2e5

4 files changed

Lines changed: 29 additions & 2 deletions

File tree

.gitignore

Lines changed: 2 additions & 1 deletion
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

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2099,6 +2099,10 @@ JSONAPI.configure do |config|
20992099
# Subclasses of the whitelisted classes will also be whitelisted.
21002100
config.exception_class_whitelist = []
21012101

2102+
# If enabled, will override configuration option `exception_class_whitelist`
2103+
# and whitelist all exceptions.
2104+
config.whitelist_all_exceptions = false
2105+
21022106
# Resource Linkage
21032107
# Controls the serialization of resource linkage for non compound documents
21042108
# NOTE: always_include_to_many_linkage_data is not currently implemented

lib/jsonapi/configuration.rb

Lines changed: 9 additions & 1 deletion
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,10 @@ def initialize
8182
# the `Pundit::NotAuthorizedError` to the `exception_class_whitelist`.
8283
self.exception_class_whitelist = []
8384

85+
# If enabled, will override configuration option `exception_class_whitelist`
86+
# and whitelist all exceptions.
87+
self.whitelist_all_exceptions = false
88+
8489
# Resource Linkage
8590
# Controls the serialization of resource linkage for non compound documents
8691
# NOTE: always_include_to_many_linkage_data is not currently implemented
@@ -188,7 +193,8 @@ def route_formatter
188193
end
189194

190195
def exception_class_whitelisted?(e)
191-
@exception_class_whitelist.flatten.any? { |k| e.class.ancestors.map(&:to_s).include?(k.to_s) }
196+
@whitelist_all_exceptions ||
197+
@exception_class_whitelist.flatten.any? { |k| e.class.ancestors.map(&:to_s).include?(k.to_s) }
192198
end
193199

194200
def default_processor_klass=(default_processor_klass)
@@ -221,6 +227,8 @@ def default_processor_klass=(default_processor_klass)
221227

222228
attr_writer :exception_class_whitelist
223229

230+
attr_writer :whitelist_all_exceptions
231+
224232
attr_writer :always_include_to_one_linkage_data
225233

226234
attr_writer :always_include_to_many_linkage_data

test/controllers/controller_test.rb

Lines changed: 14 additions & 0 deletions
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)