Skip to content

Commit 96d7108

Browse files
committed
Surface subscription validation errors instead of a generic message
On a failed create/update, show the actual validation messages (e.g. the doc trust-gate reason, or an invalid email-limit) via errors.full_messages, falling back to "Something went wrong" only when no message is present. Previously a user blocked by the <7-day doc gate through the controller saw a generic error and never the carefully-worded reason.
1 parent 2b9e106 commit 96d7108

2 files changed

Lines changed: 13 additions & 3 deletions

File tree

‎app/controllers/repo_subscriptions_controller.rb‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ def create
99
SendSingleTriageEmailJob.perform_later(@repo_subscription.id)
1010
redirect_to @repo_subscription.repo, notice: I18n.t("repo_subscriptions.subscribed")
1111
else
12-
flash[:error] = "Something went wrong"
12+
flash[:error] = @repo_subscription.errors.full_messages.to_sentence.presence || "Something went wrong"
1313
redirect_to repo_path(@repo_subscription.try(:repo) || Repo.find(repo_subscription_params[:repo_id]))
1414
end
1515
end
@@ -26,7 +26,7 @@ def update
2626
if @repo_sub.save
2727
flash[:success] = "Preferences updated!"
2828
else
29-
flash[:error] = "Something went wrong"
29+
flash[:error] = @repo_sub.errors.full_messages.to_sentence.presence || "Something went wrong"
3030
end
3131
redirect_to repo_path(@repo_sub.repo)
3232
end

‎test/functional/repo_subscriptions_controller_test.rb‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,16 @@ class RepoSubscriptionsControllerTest < ActionController::TestCase
2222
assert_redirected_to repo_path(repo)
2323
end
2424

25+
test "blocked doc subscription surfaces the gate reason instead of a generic error" do
26+
user = users(:mockstar)
27+
user.update_column(:created_at, Time.current)
28+
sign_in user
29+
repo = repos(:no_subscribers)
30+
post :create, params: {repo_subscription: {repo_id: repo.id, read: true, write: true, read_limit: 3, write_limit: 3}}
31+
assert_includes flash[:error], "You can turn on docs once your account is 7 days old"
32+
assert_redirected_to repo_path(repo)
33+
end
34+
2535
test "not update schneems' subscription when signed in as mockstar" do
2636
sign_in users(:mockstar)
2737
assert_raise ActiveRecord::RecordNotFound do
@@ -61,7 +71,7 @@ class RepoSubscriptionsControllerTest < ActionController::TestCase
6171
sign_in users(:schneems)
6272
patch :update, params: {id: repo_subscription.id,
6373
repo_subscription: {email_limit: -1}}
64-
assert_equal flash[:error], "Something went wrong"
74+
assert_includes flash[:error], "must be greater than or equal to 0"
6575
assert_redirected_to repo_path(repo_subscription.repo)
6676
end
6777

0 commit comments

Comments
 (0)