Skip to content

Commit

Permalink
Handle incorrect content IDs found in embeds
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Harriethw committed Mar 3, 2025
1 parent 69c9361 commit e406734
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 14 deletions.
16 changes: 8 additions & 8 deletions app/services/embedded_content_finder_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
22 changes: 16 additions & 6 deletions spec/services/embedded_content_finder_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit e406734

Please sign in to comment.