From 36774c089b1ec33ecc0909491296da2bd5e2dcc5 Mon Sep 17 00:00:00 2001 From: Benjamin Armintor Date: Fri, 19 Jul 2024 12:51:36 -0400 Subject: [PATCH] fail gracefully when attempting to publish, without data, a pid that doesn't exist in Hyacinth - HYACINTH-1050 --- app/jobs/process_digital_object_import_job.rb | 5 +++-- spec/jobs/process_digital_object_import_job_spec.rb | 12 ++++++++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/app/jobs/process_digital_object_import_job.rb b/app/jobs/process_digital_object_import_job.rb index cc6c308c9..281b02c58 100644 --- a/app/jobs/process_digital_object_import_job.rb +++ b/app/jobs/process_digital_object_import_job.rb @@ -49,7 +49,8 @@ def self.find_or_create_digital_object(digital_object_data, user, digital_object # We're updating data for an existing object existing_object_for_update(digital_object_data, user) else - new_object(digital_object_data['digital_object_type']['string_key'], user, digital_object_import) + digital_object_type = digital_object_data['digital_object_type']['string_key'] if digital_object_data['digital_object_type'] + new_object(digital_object_type, user, digital_object_import) end end @@ -109,7 +110,7 @@ def self.existing_object_for_update(digital_object_data, user) end def self.new_object(digital_object_type, user, digital_object_import) - digital_object = DigitalObjectType.get_model_for_string_key(digital_object_type).new + digital_object = DigitalObjectType.get_model_for_string_key(digital_object_type || :missing).new digital_object.created_by = user digital_object.updated_by = user digital_object diff --git a/spec/jobs/process_digital_object_import_job_spec.rb b/spec/jobs/process_digital_object_import_job_spec.rb index 508a4345c..400fb766a 100644 --- a/spec/jobs/process_digital_object_import_job_spec.rb +++ b/spec/jobs/process_digital_object_import_job_spec.rb @@ -84,6 +84,18 @@ expect(klass).to receive(:new_object) klass.find_or_create_digital_object(digital_object_data, user, digital_object_import) end + context "no digital_object_type given for a new object" do + let(:digital_object) { klass.find_or_create_digital_object(digital_object_data, user, digital_object_import) } + let(:expected_message) { "Invalid DigitalObjectType string key: missing" } + before do + digital_object_data.delete('digital_object_type') + end + + it "fails with a legible error" do + expect(digital_object).to be_nil + expect(digital_object_import.digital_object_errors).to include(expected_message) + end + end end describe ".handle_unexpected_processing_error" do