From 4a02b3734eb4568a9fca7f4ab8c47d95f29c4d47 Mon Sep 17 00:00:00 2001 From: Larry Gebhardt Date: Thu, 19 Nov 2015 12:20:19 -0500 Subject: [PATCH] Translate exceptions arrising in the ActsAsResourceController code into `InternalServerError`s and log them to the Rails error logger. --- lib/jsonapi/acts_as_resource_controller.rb | 8 ++++---- test/controllers/controller_test.rb | 9 +++++++++ test/fixtures/active_record.rb | 20 ++++++++++++++++++++ test/test_helper.rb | 1 + 4 files changed, 34 insertions(+), 4 deletions(-) diff --git a/lib/jsonapi/acts_as_resource_controller.rb b/lib/jsonapi/acts_as_resource_controller.rb index c71b6331b..4ee0a230e 100644 --- a/lib/jsonapi/acts_as_resource_controller.rb +++ b/lib/jsonapi/acts_as_resource_controller.rb @@ -178,10 +178,10 @@ def handle_exceptions(e) case e when JSONAPI::Exceptions::Error render_errors(e.errors) - else # raise all other exceptions - # :nocov: - fail e - # :nocov: + else + internal_server_error = JSONAPI::Exceptions::InternalServerError.new(e) + Rails.logger.error { "Internal Server Error: #{e.message} #{e.backtrace.join("\n")}" } + render_errors(internal_server_error.errors) end end diff --git a/test/controllers/controller_test.rb b/test/controllers/controller_test.rb index 2a9d57da3..11901075b 100644 --- a/test/controllers/controller_test.rb +++ b/test/controllers/controller_test.rb @@ -3270,4 +3270,13 @@ def test_get_namespaced_model_matching_resource assert_response :success assert_equal 'customers', json_response['data'][0]['type'] end +end + +class Api::V7::CategoriesControllerTest < ActionController::TestCase + def test_uncaught_error_in_controller + + get :show, {id: '1'} + assert_response 500 + assert_match /Internal Server Error/, json_response['errors'][0]['detail'] + end end \ No newline at end of file diff --git a/test/fixtures/active_record.rb b/test/fixtures/active_record.rb index e390afc51..6c474eceb 100644 --- a/test/fixtures/active_record.rb +++ b/test/fixtures/active_record.rb @@ -565,6 +565,15 @@ class SubSpecialError < PostsController::SpecialError; end head :forbidden end + def handle_exceptions(e) + case e + when PostsController::SpecialError + raise e + else + super(e) + end + end + #called by test_on_server_error def self.set_callback_message(error) @callback_message = "Sent from method" @@ -758,6 +767,9 @@ class LineItemsController < JSONAPI::ResourceController class OrderFlagsController < JSONAPI::ResourceController end + class CategoriesController < JSONAPI::ResourceController + end + class ClientsController < JSONAPI::ResourceController end end @@ -1406,6 +1418,14 @@ class ClientResource < JSONAPI::Resource has_many :purchase_orders end + class CategoryResource < CategoryResource + attribute :name + + # Raise exception for failure in controller + def name + fail "Something Exceptional Happened" + end + end end module V8 diff --git a/test/test_helper.rb b/test/test_helper.rb index 1812fc49b..01e9f00ed 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -229,6 +229,7 @@ class CatResource < JSONAPI::Resource jsonapi_resources :customers jsonapi_resources :purchase_orders jsonapi_resources :line_items + jsonapi_resources :categories jsonapi_resources :clients end