Skip to content

Commit 74cd0a9

Browse files
authored
Merge pull request cerebris#880 from rabid/make-backtraces-configurable
Make exception backtraces configurable
2 parents 54cd05d + af93e53 commit 74cd0a9

File tree

3 files changed

+27
-1
lines changed

3 files changed

+27
-1
lines changed

lib/jsonapi/configuration.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ class Configuration
2222
:top_level_meta_include_page_count,
2323
:top_level_meta_page_count_key,
2424
:allow_transactions,
25+
:include_backtraces_in_errors,
2526
:exception_class_whitelist,
2627
:always_include_to_one_linkage_data,
2728
:always_include_to_many_linkage_data,
@@ -68,6 +69,10 @@ def initialize
6869

6970
self.use_text_errors = false
7071

72+
# Whether or not to include exception backtraces in JSONAPI error
73+
# responses. Defaults to `false` in production, and `true` otherwise.
74+
self.include_backtraces_in_errors = !Rails.env.production?
75+
7176
# List of classes that should not be rescued by the operations processor.
7277
# For example, if you use Pundit for authorization, you might
7378
# raise a Pundit::NotAuthorizedError at some point during operations
@@ -212,6 +217,8 @@ def default_processor_klass=(default_processor_klass)
212217

213218
attr_writer :allow_transactions
214219

220+
attr_writer :include_backtraces_in_errors
221+
215222
attr_writer :exception_class_whitelist
216223

217224
attr_writer :always_include_to_one_linkage_data

lib/jsonapi/exceptions.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def initialize(exception)
1616
end
1717

1818
def errors
19-
unless Rails.env.production?
19+
if JSONAPI.configuration.include_backtraces_in_errors
2020
meta = Hash.new
2121
meta[:exception] = exception.message
2222
meta[:backtrace] = exception.backtrace

test/controllers/controller_test.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,25 @@ def test_exception_class_whitelist
9090
JSONAPI.configuration.exception_class_whitelist = original_whitelist
9191
end
9292

93+
def test_exception_includes_backtrace_when_enabled
94+
original_config = JSONAPI.configuration.include_backtraces_in_errors
95+
$PostProcessorRaisesErrors = true
96+
97+
JSONAPI.configuration.include_backtraces_in_errors = true
98+
assert_cacheable_get :index
99+
assert_response 500
100+
assert_includes @response.body, "backtrace", "expected backtrace in error body"
101+
102+
JSONAPI.configuration.include_backtraces_in_errors = false
103+
assert_cacheable_get :index
104+
assert_response 500
105+
refute_includes @response.body, "backtrace", "expected backtrace in error body"
106+
107+
ensure
108+
$PostProcessorRaisesErrors = false
109+
JSONAPI.configuration.include_backtraces_in_errors = original_config
110+
end
111+
93112
def test_on_server_error_block_callback_with_exception
94113
original_config = JSONAPI.configuration.dup
95114
JSONAPI.configuration.exception_class_whitelist = []

0 commit comments

Comments
 (0)