From fdf2f9be65470d46ddd495309b83392e28579430 Mon Sep 17 00:00:00 2001 From: Justin Coyne Date: Mon, 17 Jun 2024 12:15:49 -0500 Subject: [PATCH] Respond with an error if request is invalid --- app/controllers/v1/purls_controller.rb | 16 ++++++++++++---- spec/requests/v1/purls_controller_spec.rb | 18 ++++++++++++++++++ 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/app/controllers/v1/purls_controller.rb b/app/controllers/v1/purls_controller.rb index 13f06a7f..d4d1c8a9 100644 --- a/app/controllers/v1/purls_controller.rb +++ b/app/controllers/v1/purls_controller.rb @@ -20,10 +20,18 @@ def update retry end - PurlCocinaUpdater.update(@purl, cocina_object) - write_public_files - - render json: true, status: :accepted + begin + PurlCocinaUpdater.update(@purl, cocina_object) + write_public_files + + render json: true, status: :accepted + rescue Cocina::Models::ValidationError => e + render json: { + errors: [ + { title: 'bad request', detail: e.message } + ] + }, status: :bad_request + end end def destroy diff --git a/spec/requests/v1/purls_controller_spec.rb b/spec/requests/v1/purls_controller_spec.rb index 1db19b8b..e21a404e 100644 --- a/spec/requests/v1/purls_controller_spec.rb +++ b/spec/requests/v1/purls_controller_spec.rb @@ -75,6 +75,24 @@ end end + context 'with invalid cocina json' do + let(:data) { { invalid: true }.to_json } + + before do + allow(PurlCocinaUpdater).to receive(:update) + end + + context 'with a new item' do + it 'creates a new purl entry' do + expect do + post "/purls/#{druid}", params: data, headers: + end.to change(Purl, :count).by(1) + expect(response).to have_http_status(:bad_request) + expect(PurlCocinaUpdater).not_to have_received(:update) + end + end + end + context 'when no authorization token is provided' do it 'returns 401' do post "/purls/#{druid}", params: data, headers: headers.except('Authorization')