Skip to content

Commit

Permalink
Merge pull request #1028 from alphagov/return-422
Browse files Browse the repository at this point in the history
Return 422 when request is unprocessable by Publishing API
  • Loading branch information
brucebolt authored Nov 27, 2024
2 parents 93751ca + 424a738 commit 2d9ebe4
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 6 deletions.
2 changes: 2 additions & 0 deletions app/controllers/manuals_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ def update
end
rescue ActionController::UnknownFormat
render json: { status: "error", errors: "Invalid Accept header" }, status: :not_acceptable
rescue GdsApi::HTTPUnprocessableEntity => e
render json: { status: "error", errors: e }, status: :unprocessable_entity
rescue ValidationError
render json: { status: "error", errors: manual.errors.full_messages }, status: :unprocessable_entity
end
Expand Down
2 changes: 2 additions & 0 deletions app/controllers/sections_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ def update
end
rescue ActionController::UnknownFormat
render json: { status: "error", errors: "Invalid Accept header" }, status: :not_acceptable
rescue GdsApi::HTTPUnprocessableEntity => e
render json: { status: "error", errors: e }, status: :unprocessable_entity
rescue ValidationError
render json: { status: "error", errors: section.errors.full_messages }, status: :unprocessable_entity
end
Expand Down
9 changes: 3 additions & 6 deletions spec/requests/manual_sections_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
describe "manual sections resource" do
include GdsApi::TestHelpers::PublishingApi
include LinksUpdateHelper
include PublishingApiHelper

let(:maximal_section_endpoint) do
"/hmrc-manuals/#{maximal_manual_slug}/sections/#{maximal_section_slug}"
Expand Down Expand Up @@ -65,12 +66,12 @@
expect(response.status).to eq(503)
end

it "handles some other error with the Publishing API" do
it "handles the Publishing API returning an unproccessable entity error" do
publishing_api_validation_error

put_json maximal_section_endpoint, maximal_section

expect(response.status).to eq(500)
expect(response.status).to eq(422)
end

it "returns the status code from the Publishing API response" do
Expand Down Expand Up @@ -103,8 +104,4 @@
def publishing_api_times_out
stub_request(:any, /#{GdsApi::TestHelpers::PublishingApi::PUBLISHING_API_V2_ENDPOINT}\/.*/).to_timeout
end

def publishing_api_validation_error
stub_request(:any, /#{GdsApi::TestHelpers::PublishingApi::PUBLISHING_API_V2_ENDPOINT}\/.*/).to_return(status: 422)
end
end
9 changes: 9 additions & 0 deletions spec/requests/manuals_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
describe "manuals resource" do
include GdsApi::TestHelpers::PublishingApi
include LinksUpdateHelper
include PublishingApiHelper

it "confirms update of the manual" do
stub_any_publishing_api_call
Expand Down Expand Up @@ -37,6 +38,14 @@
expect(response.status).to eq(500)
end

it "handles the Publishing API returning an unproccessable entity error" do
publishing_api_validation_error

put_json "/hmrc-manuals/#{maximal_manual_slug}", maximal_manual

expect(response.status).to eq(422)
end

it "returns the status code from the Publishing API response" do
stub_any_publishing_api_call
stub_any_publishing_api_put_content.to_return(body: { version: nil }.to_json)
Expand Down
5 changes: 5 additions & 0 deletions spec/support/publishing_api_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module PublishingApiHelper
def publishing_api_validation_error
stub_request(:any, /#{GdsApi::TestHelpers::PublishingApi::PUBLISHING_API_V2_ENDPOINT}\/.*/).to_return(status: 422)
end
end

0 comments on commit 2d9ebe4

Please sign in to comment.