File tree Expand file tree Collapse file tree 3 files changed +46
-0
lines changed Expand file tree Collapse file tree 3 files changed +46
-0
lines changed Original file line number Diff line number Diff line change @@ -53,6 +53,14 @@ def use_adapter?
53
53
end
54
54
end
55
55
56
+ def rescue_with_handler ( exception )
57
+ @_serializer = nil
58
+ @_serializer_opts = nil
59
+ @_adapter_opts = nil
60
+
61
+ super ( exception )
62
+ end
63
+
56
64
module ClassMethods
57
65
def serialization_scope ( scope )
58
66
self . _serialization_scope = scope
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change @@ -213,3 +213,9 @@ def self.root_name
213
213
Spam ::UnrelatedLinkSerializer = Class . new ( ActiveModel ::Serializer ) do
214
214
attributes :id
215
215
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
You can’t perform that action at this time.
0 commit comments