diff --git a/app/controllers/admin/users_controller.rb b/app/controllers/admin/users_controller.rb index 10922e3c909..b9a24b38fd5 100644 --- a/app/controllers/admin/users_controller.rb +++ b/app/controllers/admin/users_controller.rb @@ -379,7 +379,10 @@ class Admin::UsersController < Admin::AdminController } end rescue UserDestroyer::PostsExistError - raise Discourse::InvalidAccess.new("User #{user.username} has #{user.post_count} posts, so can't be deleted.") + render json: { + deleted: false, + message: "User #{user.username} has #{user.post_count} posts, so they can't be deleted." + } end end end diff --git a/spec/controllers/admin/users_controller_spec.rb b/spec/controllers/admin/users_controller_spec.rb index 703021890fc..9fb6329fc77 100644 --- a/spec/controllers/admin/users_controller_spec.rb +++ b/spec/controllers/admin/users_controller_spec.rb @@ -529,9 +529,11 @@ describe Admin::UsersController do _post = create_post(topic: topic, user: delete_me) end - it "returns an error" do + it "returns an api response that the user can't be deleted because it has posts" do delete :destroy, params: { id: delete_me.id }, format: :json - expect(response).to be_forbidden + expect(response).to be_success + json = ::JSON.parse(response.body) + expect(json['deleted']).to eq(false) end it "doesn't return an error if delete_posts == true" do