Add rescue_from handler to clear state

Fixes #917
This commit is contained in:
Ryan Schlesinger 2015-05-19 17:06:42 -07:00
parent a59cc4c779
commit 9355416ad0
3 changed files with 46 additions and 0 deletions

View File

@ -53,6 +53,14 @@ module ActionController
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

View File

@ -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

View File

@ -211,3 +211,9 @@ end
Spam::UnrelatedLinkSerializer = Class.new(ActiveModel::Serializer) do
attributes :id
end
RaiseErrorSerializer = Class.new(ActiveModel::Serializer) do
def json_key
raise StandardError, 'OOPS'
end
end