From e406734883452079be055026caf6914ad61dcbfe Mon Sep 17 00:00:00 2001 From: Harriet H-W Date: Mon, 3 Mar 2025 14:50:20 +0000 Subject: [PATCH] Handle incorrect content IDs found in embeds Previously, an uncaught error was raised in a content block's content ID couldn't be found, which prevented any editions being published if they had an incorrect embed code within them, with no feedback for the user on Mainstream when this happened. This is change sends an exception to Sentry to alert developers the event has occurred, and does not add the incorrect ID to the Links on a Document, which will prevent the embed code causing more problems when the editions is published and going through the rendering process via the `ContentEmbedPresenter`. The user should be able to see their changes published, and the embed code will appear as plain text on the frontend, so it should be easy to spot and change. --- .../embedded_content_finder_service.rb | 16 +++++++------- .../embedded_content_finder_service_spec.rb | 22 ++++++++++++++----- 2 files changed, 24 insertions(+), 14 deletions(-) diff --git a/app/services/embedded_content_finder_service.rb b/app/services/embedded_content_finder_service.rb index f3a83a7e7..0c849197e 100644 --- a/app/services/embedded_content_finder_service.rb +++ b/app/services/embedded_content_finder_service.rb @@ -4,8 +4,7 @@ def fetch_linked_content_ids(details, locale) find_content_references(value) }.flatten.compact - check_all_references_exist(content_references.uniq, locale) - content_references.map(&:content_id) + live_content_ids(content_references, locale) end def find_content_references(value) @@ -23,16 +22,17 @@ def find_content_references(value) private - def check_all_references_exist(content_references, locale) - found_editions = live_editions(content_references, locale) + def live_content_ids(content_references, locale) + found_editions = live_editions(content_references.uniq, locale) not_found_content_ids = content_references.map(&:content_id) - found_editions.map(&:content_id) if not_found_content_ids.any? - raise CommandError.new( - code: 422, - message: "Could not find any live editions in locale #{locale} for: #{not_found_content_ids.join(', ')}, ", - ) + Sentry.capture_exception(CommandError.new( + code: 422, + message: "Could not find any live content blocks for content IDs: #{not_found_content_ids.join(', ')}", + )) end + content_references.map(&:content_id) - not_found_content_ids end def live_editions(content_references, locale) diff --git a/spec/services/embedded_content_finder_service_spec.rb b/spec/services/embedded_content_finder_service_spec.rb index bb1dba31c..ffd374dd0 100644 --- a/spec/services/embedded_content_finder_service_spec.rb +++ b/spec/services/embedded_content_finder_service_spec.rb @@ -179,16 +179,20 @@ expect(links).to eq([editions[0].content_id, editions[0].content_id, editions[1].content_id]) end - it "errors when given a content ID that is still draft" do + it "does not return a content ID that is still draft" do details = { field_name => "{{embed:#{document_type}:#{draft_edition.content_id}}}" } - expect { EmbeddedContentFinderService.new.fetch_linked_content_ids(details, Edition::DEFAULT_LOCALE) }.to raise_error(CommandError) + links = EmbeddedContentFinderService.new.fetch_linked_content_ids(details, "foo") + + expect(links).to eq([]) end - it "errors when given a live content ID that is not available in the current locale" do + it "does not return a live content ID that is not available in the current locale" do details = { field_name => "{{embed:#{document_type}:#{editions[0].content_id}}}" } - expect { EmbeddedContentFinderService.new.fetch_linked_content_ids(details, "foo") }.to raise_error(CommandError) + links = EmbeddedContentFinderService.new.fetch_linked_content_ids(details, "foo") + + expect(links).to eq([]) end end end @@ -208,10 +212,16 @@ expect(links).to eq([]) end - it "errors when given a content ID that has no live editions" do + it "alerts Sentry when there is are embeds without live editions" do details = { body: "{{embed:contact:00000000-0000-0000-0000-000000000000}}" } + expect(Sentry).to receive(:capture_exception).with(CommandError.new( + code: 422, + message: "Could not find any live content blocks for content IDs: 00000000-0000-0000-0000-000000000000", + )) - expect { EmbeddedContentFinderService.new.fetch_linked_content_ids(details, Edition::DEFAULT_LOCALE) }.to raise_error(CommandError) + links = EmbeddedContentFinderService.new.fetch_linked_content_ids(details, "foo") + + expect(links).to eq([]) end context "when the field value is an array" do