Skip to content

Commit 19b6df0

Browse files
DavidMikeSimonlgebhardt
authored andcommitted
Correctly handle errors from process_operations callbacks
1 parent 1d1cc4b commit 19b6df0

File tree

1 file changed

+26
-48
lines changed

1 file changed

+26
-48
lines changed

lib/jsonapi/acts_as_resource_controller.rb

Lines changed: 26 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@ def self.included(base)
1010
base.include Callbacks
1111
base.cattr_reader :server_error_callbacks
1212
base.define_jsonapi_resources_callbacks :process_operations,
13-
:transaction,
14-
:rollback
13+
:transaction
1514
end
1615

1716
attr_reader :response_document
@@ -76,57 +75,46 @@ def process_request
7675

7776
transactional = request_parser.transactional?
7877

79-
force_rollback = false
80-
run_in_transaction(transactional) do
81-
begin
78+
begin
79+
run_in_transaction(transactional) do
8280
run_callbacks :process_operations do
83-
begin
84-
request_parser.each(response_document) do |op|
85-
op.options[:serializer] = resource_serializer_klass.new(
86-
op.resource_klass,
87-
include_directives: op.options[:include_directives],
88-
fields: op.options[:fields],
89-
base_url: base_url,
90-
key_formatter: key_formatter,
91-
route_formatter: route_formatter,
92-
serialization_options: serialization_options
93-
)
94-
op.options[:cache_serializer_output] = !JSONAPI.configuration.resource_cache.nil?
95-
96-
process_operation(op)
97-
end
98-
rescue => e
99-
handle_exceptions(e)
81+
request_parser.each(response_document) do |op|
82+
op.options[:serializer] = resource_serializer_klass.new(
83+
op.resource_klass,
84+
include_directives: op.options[:include_directives],
85+
fields: op.options[:fields],
86+
base_url: base_url,
87+
key_formatter: key_formatter,
88+
route_formatter: route_formatter,
89+
serialization_options: serialization_options
90+
)
91+
op.options[:cache_serializer_output] = !JSONAPI.configuration.resource_cache.nil?
92+
93+
process_operation(op)
10094
end
10195
end
102-
rescue => e
103-
force_rollback = true
104-
raise e
105-
ensure
106-
if response_document.has_errors? || force_rollback
107-
rollback_transaction(transactional)
96+
if response_document.has_errors?
97+
raise ActiveRecord::Rollback
10898
end
10999
end
100+
rescue => e
101+
handle_exceptions(e)
110102
end
111103
render_response_document
112104
end
113105

114106
def run_in_transaction(transactional)
115107
if transactional
116108
run_callbacks :transaction do
117-
transaction do
109+
ActiveRecord::Base.transaction do
118110
yield
119111
end
120112
end
121113
else
122-
yield
123-
end
124-
end
125-
126-
def rollback_transaction(transactional)
127-
if transactional
128-
run_callbacks :rollback do
129-
rollback
114+
begin
115+
yield
116+
rescue ActiveRecord::Rollback
117+
# Can't rollback without transaction, so just ignore it
130118
end
131119
end
132120
end
@@ -136,16 +124,6 @@ def process_operation(operation)
136124
response_document.add_result(result, operation)
137125
end
138126

139-
def transaction
140-
ActiveRecord::Base.transaction do
141-
yield
142-
end
143-
end
144-
145-
def rollback
146-
fail ActiveRecord::Rollback
147-
end
148-
149127
private
150128

151129
def resource_klass
@@ -276,7 +254,7 @@ def handle_exceptions(e)
276254
errors = JSONAPI::Exceptions::ParameterMissing.new(e.param).errors
277255
else
278256
if JSONAPI.configuration.exception_class_whitelisted?(e)
279-
fail e
257+
raise e
280258
else
281259
if self.class.server_error_callbacks
282260
self.class.server_error_callbacks.each { |callback|

0 commit comments

Comments
 (0)