Skip to content

Commit 9355416

Browse files
committed
Add rescue_from handler to clear state
Fixes #917
1 parent a59cc4c commit 9355416

File tree

3 files changed

+46
-0
lines changed

3 files changed

+46
-0
lines changed

lib/action_controller/serialization.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,14 @@ def use_adapter?
5353
end
5454
end
5555

56+
def rescue_with_handler(exception)
57+
@_serializer = nil
58+
@_serializer_opts = nil
59+
@_adapter_opts = nil
60+
61+
super(exception)
62+
end
63+
5664
module ClassMethods
5765
def serialization_scope(scope)
5866
self._serialization_scope = scope
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
require 'test_helper'
2+
3+
module ActionController
4+
module Serialization
5+
class RescueFromTest < ActionController::TestCase
6+
class MyController < ActionController::Base
7+
rescue_from Exception, with: :handle_error
8+
9+
def render_using_raise_error_serializer
10+
@profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
11+
render json: [@profile], serializer: RaiseErrorSerializer
12+
end
13+
14+
def handle_error(exception)
15+
render json: { errors: ['Internal Server Error'] }, status: :internal_server_error
16+
end
17+
end
18+
19+
tests MyController
20+
21+
def test_rescue_from
22+
get :render_using_raise_error_serializer
23+
24+
expected = {
25+
errors: ['Internal Server Error']
26+
}.to_json
27+
28+
assert_equal expected, @response.body
29+
end
30+
end
31+
end
32+
end

test/fixtures/poro.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,3 +211,9 @@ def self.root_name
211211
Spam::UnrelatedLinkSerializer = Class.new(ActiveModel::Serializer) do
212212
attributes :id
213213
end
214+
215+
RaiseErrorSerializer = Class.new(ActiveModel::Serializer) do
216+
def json_key
217+
raise StandardError, 'OOPS'
218+
end
219+
end

0 commit comments

Comments
 (0)