Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add rescue_from handler to clear state #918

Merged
merged 2 commits into from
May 21, 2015
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions lib/action_controller/serialization.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
32 changes: 32 additions & 0 deletions test/action_controller/rescue_from_test.rb
Original file line number Diff line number Diff line change
@@ -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
6 changes: 6 additions & 0 deletions test/fixtures/poro.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hah, can we write a different error message? 😆

end
end