Skip to content

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

UX: Improve error message when a topic cannot be moved due to category restrictions (#20900)

Natalie Tay068a36d35488
diff --git a/app/controllers/topics_controller.rb b/app/controllers/topics_controller.rb
index d85a74dbb8b..c8b265f6186 100644
--- a/app/controllers/topics_controller.rb
+++ b/app/controllers/topics_controller.rb
@@ -361,7 +361,14 @@ class TopicsController < ApplicationController
category = Category.find_by(id: params[:category_id])
if category || (params[:category_id].to_i == 0)
- guardian.ensure_can_move_topic_to_category!(category)
+ begin
+ guardian.ensure_can_move_topic_to_category!(category)
+ rescue Discourse::InvalidAccess
+ return(
+ render_json_error I18n.t("category.errors.move_topic_to_category_disallowed"),
+ status: :forbidden
+ )
+ end
else
return render_json_error(I18n.t("category.errors.not_found"))
end
diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml
index 92fab061a4e..17d02c5091a 100644
--- a/config/locales/server.en.yml
+++ b/config/locales/server.en.yml
@@ -705,6 +705,7 @@ en:
disallowed_tags_generic: "This topic has disallowed tags."
slug_contains_non_ascii_chars: "contains non-ascii characters"
is_already_in_use: "is already in use"
+ move_topic_to_category_disallowed: "You cannot move this topic to a category where you do not have permission to create new topics."
cannot_delete:
uncategorized: "This category is special. It is intended as a holding area for topics that have no category; it cannot be deleted."
has_subcategories: "Can't delete this category because it has sub-categories."
diff --git a/spec/requests/topics_controller_spec.rb b/spec/requests/topics_controller_spec.rb
index 20e04a6dc1d..4ad4830079b 100644
--- a/spec/requests/topics_controller_spec.rb
+++ b/spec/requests/topics_controller_spec.rb
@@ -1391,6 +1391,9 @@ RSpec.describe TopicsController do
put "/t/#{topic.id}.json", params: { category_id: category.id }
expect(response.status).to eq(403)
+ expect(response.parsed_body["errors"].first).to eq(
+ I18n.t("category.errors.move_topic_to_category_disallowed"),
+ )
expect(topic.reload.category_id).not_to eq(category.id)
end