Skip to content

Commit 76fdba9

Browse files
committed
Allow whitelisted errors from controller to raise
1 parent 7b3cdff commit 76fdba9

2 files changed

Lines changed: 30 additions & 4 deletions

File tree

lib/jsonapi/acts_as_resource_controller.rb

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -179,9 +179,13 @@ def handle_exceptions(e)
179179
when JSONAPI::Exceptions::Error
180180
render_errors(e.errors)
181181
else
182-
internal_server_error = JSONAPI::Exceptions::InternalServerError.new(e)
183-
Rails.logger.error { "Internal Server Error: #{e.message} #{e.backtrace.join("\n")}" }
184-
render_errors(internal_server_error.errors)
182+
if JSONAPI.configuration.exception_class_whitelist.any? { |k| e.class.ancestors.include?(k) }
183+
fail e
184+
else
185+
internal_server_error = JSONAPI::Exceptions::InternalServerError.new(e)
186+
Rails.logger.error { "Internal Server Error: #{e.message} #{e.backtrace.join("\n")}" }
187+
render_errors(internal_server_error.errors)
188+
end
185189
end
186190
end
187191

test/controllers/controller_test.rb

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3273,10 +3273,32 @@ def test_get_namespaced_model_matching_resource
32733273
end
32743274

32753275
class Api::V7::CategoriesControllerTest < ActionController::TestCase
3276-
def test_uncaught_error_in_controller
3276+
def test_uncaught_error_in_controller_translated_to_internal_server_error
32773277

32783278
get :show, {id: '1'}
32793279
assert_response 500
32803280
assert_match /Internal Server Error/, json_response['errors'][0]['detail']
32813281
end
3282+
3283+
def test_not_whitelisted_error_in_controller
3284+
original_config = JSONAPI.configuration.dup
3285+
JSONAPI.configuration.operations_processor = :error_raising
3286+
JSONAPI.configuration.exception_class_whitelist = []
3287+
get :show, {id: '1'}
3288+
assert_response 500
3289+
assert_match /Internal Server Error/, json_response['errors'][0]['detail']
3290+
ensure
3291+
JSONAPI.configuration = original_config
3292+
end
3293+
3294+
def test_whitelisted_error_in_controller
3295+
original_config = JSONAPI.configuration.dup
3296+
JSONAPI.configuration.operations_processor = :error_raising
3297+
JSONAPI.configuration.exception_class_whitelist = [PostsController::SubSpecialError]
3298+
assert_raises PostsController::SubSpecialError do
3299+
get :show, {id: '1'}
3300+
end
3301+
ensure
3302+
JSONAPI.configuration = original_config
3303+
end
32823304
end

0 commit comments

Comments
 (0)