From 5c512cb23e5a727333c4a3c77c1b55bf24de213b Mon Sep 17 00:00:00 2001 From: carolyncole <1599081+carolyncole@users.noreply.github.com> Date: Fri, 2 Aug 2024 14:13:00 -0400 Subject: [PATCH] Change unporcessable entities to 200 so the load balancer does not capture them (#1882) --- app/controllers/groups_controller.rb | 5 +++-- app/controllers/users_controller.rb | 5 +++-- app/controllers/works_controller.rb | 6 ++++-- app/controllers/works_wizard_controller.rb | 3 ++- spec/controllers/groups_controller_spec.rb | 8 ++++---- spec/controllers/users_controller_spec.rb | 10 +++++----- spec/controllers/works_controller_spec.rb | 10 +++++----- 7 files changed, 26 insertions(+), 21 deletions(-) diff --git a/app/controllers/groups_controller.rb b/app/controllers/groups_controller.rb index ede19425d..f84f98531 100644 --- a/app/controllers/groups_controller.rb +++ b/app/controllers/groups_controller.rb @@ -26,8 +26,9 @@ def update format.html { redirect_to group_url(@group), notice: "Group was successfully updated." } format.json { render :show, status: :ok, location: @group } else - format.html { render :edit, status: :unprocessable_entity } - format.json { render json: @group.errors, status: :unprocessable_entity } + # return 200 so the loadbalancer doesn't capture the error + format.html { render :edit } + format.json { render json: @group.errors } end end else diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index ab2322cda..165e21d40 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -46,8 +46,9 @@ def update format.html { redirect_to user_url(@user), notice: "User was successfully updated." } format.json { render :show, status: :ok, location: @user } else - format.html { render :edit, status: :unprocessable_entity } - format.json { render json: @user.errors, status: :unprocessable_entity } + # return 200 so the loadbalancer doesn't capture the error + format.html { render :edit } + format.json { render json: @user.errors } end end else diff --git a/app/controllers/works_controller.rb b/app/controllers/works_controller.rb index fac866880..7ce276745 100644 --- a/app/controllers/works_controller.rb +++ b/app/controllers/works_controller.rb @@ -58,7 +58,8 @@ def create else @work_decorator = WorkDecorator.new(@work, current_user) @form_resource_decorator = FormResourceDecorator.new(@work, current_user) - render :new, status: :unprocessable_entity + # return 200 so the loadbalancer doesn't capture the error + render :new end end @@ -389,7 +390,8 @@ def process_updates @uploads = @work.uploads @form_resource_decorator = FormResourceDecorator.new(@work, current_user) - render :edit, status: :unprocessable_entity + # return 200 so the loadbalancer doesn't capture the error + render :edit end end diff --git a/app/controllers/works_wizard_controller.rb b/app/controllers/works_wizard_controller.rb index 05e3ab867..f2ade61fb 100644 --- a/app/controllers/works_wizard_controller.rb +++ b/app/controllers/works_wizard_controller.rb @@ -181,7 +181,8 @@ def edit_helper(view_name, redirect_url) redirect_to redirect_url end else - render view_name, status: :unprocessable_entity + # return 200 so the loadbalancer doesn't capture the error + render view_name end end end diff --git a/spec/controllers/groups_controller_spec.rb b/spec/controllers/groups_controller_spec.rb index 237a5e863..db1f66e8d 100644 --- a/spec/controllers/groups_controller_spec.rb +++ b/spec/controllers/groups_controller_spec.rb @@ -83,7 +83,7 @@ end context "when an update request uses invalid parameters" do - it "renders the Edit View with a 422 response" do + it "renders the Edit View with a 200 response" do params = { "group" => { "title" => nil, @@ -96,12 +96,12 @@ } sign_in admin_user post :update, params: params - expect(response.status).to eq 422 + expect(response.status).to eq 200 expect(response).to render_template("edit") end context "when the request is of the JSON content type" do - it "renders the Edit View with a 422 response" do + it "renders the Edit View with a 200 response" do params = { "group" => { "title" => nil, @@ -114,7 +114,7 @@ } sign_in admin_user post :update, params: params, format: :json - expect(response.status).to eq 422 + expect(response.status).to eq 200 expect(response.content_type).to eq("application/json; charset=utf-8") json_body = JSON.parse(response.body) expect(json_body).to include("base" => ["Title cannot be empty"]) diff --git a/spec/controllers/users_controller_spec.rb b/spec/controllers/users_controller_spec.rb index 0d754e3b1..265fc6dfe 100644 --- a/spec/controllers/users_controller_spec.rb +++ b/spec/controllers/users_controller_spec.rb @@ -77,8 +77,8 @@ patch :update, params: end - it "renders the edit view with a 422 response status code" do - expect(response.code).to eq("422") + it "renders the edit view with a 200 response status code" do + expect(response.code).to eq("200") expect(response).to render_template(:edit) end end @@ -112,8 +112,8 @@ patch :update, params: end - it "renders JSON-serialized error messages with a 422 response status code" do - expect(response.code).to eq("422") + it "renders JSON-serialized error messages with a 200 response status code" do + expect(response.code).to eq("200") end end end @@ -127,7 +127,7 @@ patch :update, params: end - it "renders the edit view with a 422 response status code" do + it "renders the edit view with a 200 response status code" do expect(response).to redirect_to(user_path(user)) expect(Rails.logger).to have_received(:warn).with("Unauthorized to update user #{user.id} (current user: #{user_other.id})") end diff --git a/spec/controllers/works_controller_spec.rb b/spec/controllers/works_controller_spec.rb index a027ff053..edaa75f9a 100644 --- a/spec/controllers/works_controller_spec.rb +++ b/spec/controllers/works_controller_spec.rb @@ -796,7 +796,7 @@ post :approve, params: { id: work.id } end - it "responds with a status code of 422" do + it "responds with a status code of 302" do expect(response.status).to be 302 end end @@ -998,8 +998,8 @@ patch :update, params: end - it "renders the edit view with a 422 response status code" do - expect(response.code).to eq("422") + it "renders the edit view with a 200 response status code" do + expect(response.code).to eq("200") expect(response).to render_template(:edit) end end @@ -1028,8 +1028,8 @@ patch :update, params: end - it "renders JSON-serialized error messages with a 422 response status code" do - expect(response.code).to eq("422") + it "renders JSON-serialized error messages with a 200 response status code" do + expect(response.code).to eq("200") end end end