Skip to content

A platform for community discussion. Free, open, simple.

FIX: Add missing error handling to merge_topic hijack block (#37542)

Samd9c414e64557
diff --git a/app/controllers/topics_controller.rb b/app/controllers/topics_controller.rb
index 0ba284be3ae..edfe02e721f 100644
--- a/app/controllers/topics_controller.rb
+++ b/app/controllers/topics_controller.rb
@@ -904,6 +904,8 @@ class TopicsController < ApplicationController
hijack(info: "merging topic #{topic_id.inspect} into #{destination_topic_id.inspect}") do
destination_topic = topic.move_posts(acting_user, topic.posts.pluck(:id), args)
render_topic_changes(destination_topic)
+ rescue ActiveRecord::RecordInvalid, ActiveRecord::RecordNotSaved => ex
+ render_json_error(ex)
end
end
diff --git a/spec/requests/topics_controller_spec.rb b/spec/requests/topics_controller_spec.rb
index 3fd5a82dd1f..f5f9295f48e 100644
--- a/spec/requests/topics_controller_spec.rb
+++ b/spec/requests/topics_controller_spec.rb
@@ -976,6 +976,29 @@ RSpec.describe TopicsController do
end
end
end
+
+ describe "error handling" do
+ fab!(:p1) { Fabricate(:post, user: user) }
+ fab!(:topic) { p1.topic }
+
+ it "returns a JSON error when move_posts raises RecordInvalid" do
+ sign_in(moderator)
+
+ Topic
+ .any_instance
+ .stubs(:move_posts)
+ .raises(
+ ActiveRecord::RecordInvalid.new(
+ Post.new.tap { |p| p.errors.add(:base, "Something went wrong") },
+ ),
+ )
+
+ post "/t/#{topic.id}/merge-topic.json", params: { destination_topic_id: dest_topic.id }
+
+ expect(response.status).to eq(422)
+ expect(response.parsed_body["errors"]).to be_present
+ end
+ end
end
describe "#change_post_owners" do