mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-24 14:56:50 +00:00
Merge pull request #918 from aceofsales/rescue_from
Adding rescue_with_handler to clear state
This commit is contained in:
commit
5f05944826
@ -53,6 +53,14 @@ module ActionController
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def rescue_with_handler(exception)
|
||||||
|
@_serializer = nil
|
||||||
|
@_serializer_opts = nil
|
||||||
|
@_adapter_opts = nil
|
||||||
|
|
||||||
|
super(exception)
|
||||||
|
end
|
||||||
|
|
||||||
module ClassMethods
|
module ClassMethods
|
||||||
def serialization_scope(scope)
|
def serialization_scope(scope)
|
||||||
self._serialization_scope = scope
|
self._serialization_scope = scope
|
||||||
|
|||||||
32
test/action_controller/rescue_from_test.rb
Normal file
32
test/action_controller/rescue_from_test.rb
Normal 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
|
||||||
6
test/fixtures/poro.rb
vendored
6
test/fixtures/poro.rb
vendored
@ -213,3 +213,9 @@ end
|
|||||||
Spam::UnrelatedLinkSerializer = Class.new(ActiveModel::Serializer) do
|
Spam::UnrelatedLinkSerializer = Class.new(ActiveModel::Serializer) do
|
||||||
attributes :id
|
attributes :id
|
||||||
end
|
end
|
||||||
|
|
||||||
|
RaiseErrorSerializer = Class.new(ActiveModel::Serializer) do
|
||||||
|
def json_key
|
||||||
|
raise StandardError, 'Intentional error for rescue_from test'
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user