Skip to content

Commit

Permalink
Change unporcessable entities to 200 so the load balancer does not ca…
Browse files Browse the repository at this point in the history
…pture them (#1882)
  • Loading branch information
carolyncole authored Aug 2, 2024
1 parent 7b9678a commit 5c512cb
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 21 deletions.
5 changes: 3 additions & 2 deletions app/controllers/groups_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions app/controllers/works_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down
3 changes: 2 additions & 1 deletion app/controllers/works_wizard_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions spec/controllers/groups_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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"])
Expand Down
10 changes: 5 additions & 5 deletions spec/controllers/users_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
10 changes: 5 additions & 5 deletions spec/controllers/works_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 5c512cb

Please sign in to comment.