Skip to content

Conversation

@ChrisBAshton
Copy link
Contributor

@ChrisBAshton ChrisBAshton commented Dec 3, 2025

What

When trying to create a document that has an already reserved base path, catch the error and render an error with the title field on the new document form.

Further details

  • rework the exception capturing to delete the saved edition if the base path conflict occurs (otherwise the edition would still be created and the user could continue to edit it by accessing through the edition search)
  • display an error on the title field of the form as this is the field that determines the base path of a document

Why

Continues the work of #10912, now presenting an error to the user on the form instead of rendering a server error and removing the contents of the form.

Visual Differences

On attempting to create a new draft with the same base path:

Screenshot 2026-01-06 at 15 22 53

⚠️ This repo is Continuously Deployed: make sure you follow the guidance ⚠️

This application is owned by the Whitehall Experience team. Please let us know in #govuk-whitehall-experience-tech when you raise any PRs.

Follow these steps if you are doing a Rails upgrade.

@patrickpatrickpatrick patrickpatrickpatrick force-pushed the avoid-accidentally-overwriting-base-path branch from 3611a8f to 9de162c Compare December 8, 2025 11:44
Base automatically changed from avoid-accidentally-overwriting-base-path to main December 9, 2025 09:59
@patrickpatrickpatrick patrickpatrickpatrick force-pushed the catch-base-path-exception-and-show-user branch 2 times, most recently from f02d8a6 to 4383c4b Compare December 9, 2025 13:54
@patrickpatrickpatrick patrickpatrickpatrick force-pushed the catch-base-path-exception-and-show-user branch 10 times, most recently from a396a17 to 025310d Compare December 22, 2025 15:46
@patrickpatrickpatrick patrickpatrickpatrick changed the title WIP: Attempt to catch exception and show user error message Attempt to catch exception and show user error message Dec 23, 2025
@patrickpatrickpatrick patrickpatrickpatrick force-pushed the catch-base-path-exception-and-show-user branch 3 times, most recently from 5ddabe7 to 7e59046 Compare January 6, 2026 15:13
@patrickpatrickpatrick patrickpatrickpatrick changed the title Attempt to catch exception and show user error message Render error to user if base path reserved [WHIT-2884] Jan 6, 2026
@patrickpatrickpatrick patrickpatrickpatrick force-pushed the catch-base-path-exception-and-show-user branch 9 times, most recently from 519163d to ae69cf1 Compare January 7, 2026 11:45
@patrickpatrickpatrick patrickpatrickpatrick marked this pull request as ready for review January 7, 2026 11:46
@patrickpatrickpatrick patrickpatrickpatrick force-pushed the catch-base-path-exception-and-show-user branch from ae69cf1 to 8b7de83 Compare January 7, 2026 11:49
Copy link
Contributor Author

@ChrisBAshton ChrisBAshton left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for nudging this towards the finish line, @patrickpatrickpatrick ! Some comments below.

@@ -0,0 +1,9 @@
Feature: News article
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This behaviour applies to StandardEdition documents as well as legacy ones, doesn't it?

If so, please move this test to the standard_edition.feature file (and amend it as necessary). We don't want any concrete references to news articles going forward.

"/government/generic-editions/#{url_slug}"
end

def base_path_without_sequence
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not entirely sure we need this additional method? Reading between the lines, I think it's because you're wanting to catch cases where we send both foo-bar-baz and foo--bar-baz and we want to treat them as the same rather than as distinct slugs. Is that right?

If so, do you have examples of that, or is it a belt and braces thing? I'd suggest we avoid adding this unless absolutely necessary.

If it is necessary, can we add a unit test for this new method?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we don't have this method then base_path will return (e.g.) /government/news/news-title--2 for a news article with the same title within Whitehall. As a result the check against PublishingAPI will return negative for a match if there is published edition with path /government/news/news-title.

An alternative approach could be that we use the similar_slug_exists? method from Document before this check with the API even occurs. We could add it as a validation in Edition and then we could assume that the Edition won't have the --NUMBER suffix on the base path.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I'd misunderstood - that makes sense then, thanks.

Good reminder about the similar_slug_exists? method - which will require some rethinking soon anyway, because it's currently scoped by document_type, and ultimately everything will eventually be a StandardEdition so the method would start raising false positives. Let's revisit that later.

Your current solution is good - perhaps it would benefit from an inline comment where it's used in publishing_api.rb.

@edition.destroy!
build_edition
build_edition_dependencies
render :new
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to be clear - would the submitted values be retained when encountering this error, or would the user have to fill it all out again?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The submitted values would be retained since build_edition and build_edition_dependencies use the parameters that the user has submitted to make a new Edition.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have amended the feature test to confirm this.

test "destroys saved new draft edition if base path conflict with published edition" do
login_as create(:gds_admin)

published_edition = create(:published_news_article, title: "Test title")
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh and this needs to not refer to legacy news article please.

@patrickpatrickpatrick patrickpatrickpatrick force-pushed the catch-base-path-exception-and-show-user branch from 8b7de83 to f754c36 Compare January 8, 2026 14:19
The user attempts to create a document with the same base path as a
published edition then destroy the newly saved edition. This is required
as the edition is saved before the conflict check. If the edition is not
destroyed then the user can still edit and attempt to save and/or force
publish the conflicting new edition.
@patrickpatrickpatrick patrickpatrickpatrick force-pushed the catch-base-path-exception-and-show-user branch from f754c36 to f2f3246 Compare January 8, 2026 14:29
Instead of presenting a server error to the user if they try and create
an edition that has the base path of another published edition, the
error is now presented to the user.
@patrickpatrickpatrick patrickpatrickpatrick force-pushed the catch-base-path-exception-and-show-user branch from f2f3246 to 5088778 Compare January 8, 2026 14:43
@patrickpatrickpatrick patrickpatrickpatrick self-assigned this Jan 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants