FIX: display validation errors when converting topics (#27064)

When converting a PM to a public topic (and vice versa), if there was a validation error (like a topic already used, or a tag required or not allowed) the error message wasn't bubbled up nor shown to the user.

This fix ensures we properly stop the conversion whenever a validation error happens and bubble up the errors back to the user so they can be informed.

Internal ref - t/128795
This commit is contained in:
Régis Hanol
2024-05-17 16:36:25 +02:00
committed by GitHub
parent 9264479c27
commit e04ac5e2d8
5 changed files with 76 additions and 58 deletions

View File

@ -1146,18 +1146,18 @@ class TopicsController < ApplicationController
def convert_topic
params.require(:id)
params.require(:type)
topic = Topic.find_by(id: params[:id])
guardian.ensure_can_convert_topic!(topic)
if params[:type] == "public"
converted_topic =
topic =
if params[:type] == "public"
topic.convert_to_public_topic(current_user, category_id: params[:category_id])
else
converted_topic = topic.convert_to_private_message(current_user)
end
render_topic_changes(converted_topic)
rescue ActiveRecord::RecordInvalid => ex
render_json_error(ex)
else
topic.convert_to_private_message(current_user)
end
topic.valid? ? render_topic_changes(topic) : render_json_error(topic)
end
def reset_bump_date