Skip to content

Commit 5f05944

Browse files
committed
Merge pull request #918 from aceofsales/rescue_from
Adding rescue_with_handler to clear state
2 parents f7fb4db + a5db2c5 commit 5f05944

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
@@ -213,3 +213,9 @@ def self.root_name
213213
Spam::UnrelatedLinkSerializer = Class.new(ActiveModel::Serializer) do
214214
attributes :id
215215
end
216+
217+
RaiseErrorSerializer = Class.new(ActiveModel::Serializer) do
218+
def json_key
219+
raise StandardError, 'Intentional error for rescue_from test'
220+
end
221+
end

0 commit comments

Comments
 (0)