From 9355416ad09fb0b66f52c8c9b985754fd8e066d9 Mon Sep 17 00:00:00 2001 From: Ryan Schlesinger Date: Tue, 19 May 2015 17:06:42 -0700 Subject: [PATCH 1/2] Add rescue_from handler to clear state Fixes #917 --- lib/action_controller/serialization.rb | 8 ++++++ test/action_controller/rescue_from_test.rb | 32 ++++++++++++++++++++++ test/fixtures/poro.rb | 6 ++++ 3 files changed, 46 insertions(+) create mode 100644 test/action_controller/rescue_from_test.rb diff --git a/lib/action_controller/serialization.rb b/lib/action_controller/serialization.rb index 11c70fe6d..6fcb5df75 100644 --- a/lib/action_controller/serialization.rb +++ b/lib/action_controller/serialization.rb @@ -53,6 +53,14 @@ def use_adapter? end end + def rescue_with_handler(exception) + @_serializer = nil + @_serializer_opts = nil + @_adapter_opts = nil + + super(exception) + end + module ClassMethods def serialization_scope(scope) self._serialization_scope = scope diff --git a/test/action_controller/rescue_from_test.rb b/test/action_controller/rescue_from_test.rb new file mode 100644 index 000000000..d52ea8e67 --- /dev/null +++ b/test/action_controller/rescue_from_test.rb @@ -0,0 +1,32 @@ +require 'test_helper' + +module ActionController + module Serialization + class RescueFromTest < ActionController::TestCase + class MyController < ActionController::Base + rescue_from Exception, with: :handle_error + + def render_using_raise_error_serializer + @profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' }) + render json: [@profile], serializer: RaiseErrorSerializer + end + + def handle_error(exception) + render json: { errors: ['Internal Server Error'] }, status: :internal_server_error + end + end + + tests MyController + + def test_rescue_from + get :render_using_raise_error_serializer + + expected = { + errors: ['Internal Server Error'] + }.to_json + + assert_equal expected, @response.body + end + end + end +end diff --git a/test/fixtures/poro.rb b/test/fixtures/poro.rb index 69185394f..10b4bd574 100644 --- a/test/fixtures/poro.rb +++ b/test/fixtures/poro.rb @@ -211,3 +211,9 @@ def self.root_name Spam::UnrelatedLinkSerializer = Class.new(ActiveModel::Serializer) do attributes :id end + +RaiseErrorSerializer = Class.new(ActiveModel::Serializer) do + def json_key + raise StandardError, 'OOPS' + end +end From a5db2c52c5394e761e5f17e8c9d3e6576398d355 Mon Sep 17 00:00:00 2001 From: Ryan Schlesinger Date: Wed, 20 May 2015 09:19:32 -0700 Subject: [PATCH 2/2] Clearer exception description --- test/fixtures/poro.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/fixtures/poro.rb b/test/fixtures/poro.rb index 10b4bd574..8fca91d69 100644 --- a/test/fixtures/poro.rb +++ b/test/fixtures/poro.rb @@ -214,6 +214,6 @@ def self.root_name RaiseErrorSerializer = Class.new(ActiveModel::Serializer) do def json_key - raise StandardError, 'OOPS' + raise StandardError, 'Intentional error for rescue_from test' end end