Repository: wordpress-mobile/WordPress-Android (language: kotlin)

Issue:
IllegalStateException: Site already exists - seems like an issue with domain suggestions endpoint
Sentry Issue: [JETPACK-ANDROID-DZ0](https://a8c.sentry.io/issues/4584356763/?referrer=github_integration)

```
IllegalStateException: Site already exists - seems like an issue with domain suggestions endpoint
    at org.wordpress.android.ui.sitecreation.services.SiteCreationServiceManager$createSite$1.invokeSuspend(SiteCreationServiceManager.kt:139)
    at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
    at kotlinx.coroutines.CoroutineContextKt.withContinuationContext
    at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:90)
    at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:108)
...
(4 additional frame(s) were not displayed)
```


Discussion hints:
Thanks for reporting! 👍 <!-- generated_by_dangermattic -->

Sentry Issue: [JETPACK-ANDROID-1RW](https://a8c.sentry.io/issues/3857462344/?referrer=github_integration)
Checking the sentry logs all of them contain the following error:
```
e/WordPress-SITE_CREATION: Site already exists - seems like an issue with domain suggestions endpoint

StackTrace: com.android.volley.ClientError
	at com.android.volley.toolbox.NetworkUtility.shouldRetryException(NetworkUtility.java:193)
	at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:145)
	at com.android.volley.NetworkDispatcher.processRequest(NetworkDispatcher.java:132)
	at com.android.volley.NetworkDispatcher.processRequest(NetworkDispatcher.java:111)
	at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:90)
Volley error on https://public-api.wordpress.com/rest/v1.1/sites/new/?locale=en_GB Status Code: 400 - exception: null
```
[Checking the Volley code](https://github.com/google/volley/blob/master/core/src/main/java/com/android/volley/toolbox/NetworkUtility.java#L191) the above should indicate that no retry is possible.

Checking the stats ~2 users per day experience this crash

![Screenshot 2024-03-15 at 1 07 54 PM](https://github.com/wordpress-mobile/WordPress-Android/assets/304044/ff71f77b-ac3f-4d89-937f-eb8625f90d5c)
![Screenshot 2024-03-15 at 1 18 55 PM](https://github.com/wordpress-mobile/WordPress-Android/assets/304044/6bb14303-d964-4103-9012-520475cd494d)


Pull request: Redirects to the site creation failure flow when the endpoint fails
Fixes #20479

## Description
Redirects to the site creation failure flow when the endpoint fails instead of an unhandled crash

-----

## To Test:
Since I was not able to reproduce this issue I would recommend a sanity check of the site creation flow

-----

## Regression Notes

1. Potential unintended areas of impact

    - Site Creation

2. What I did to test those areas of impact (or what existing automated tests I relied on)

    - Manual testing

3. What automated tests I added (or what prevented me from doing so)

    - Added a test case

-----

## PR Submission Checklist:

- [x] I have completed the Regression Notes.
- [x] I have considered adding accessibility improvements for my changes.
- [x] I have considered if this change warrants user-facing release notes and have added them to `RELEASE-NOTES.txt` if necessary.

-----

## Testing Checklist (strike-out the not-applying and unnecessary ones):

- [ ] WordPress.com sites and self-hosted Jetpack sites.
- [ ] Portrait and landscape orientations.
- [ ] Light and dark modes.
- [ ] Fonts: Larger, smaller and bold text.
- [ ] High contrast.
- [ ] Talkback.
- [ ] Languages with large words or with letters/accents not frequently used in English.
- [ ] Right-to-left languages. (Even if translation isn’t complete, formatting should still respect the right-to-left layout)
- [ ] Large and small screen sizes. (Tablet and smaller phones)
- [ ] Multi-tasking: Split screen and Pop-up view. (Android 10 or higher)


Test command: ./gradlew :WordPress:testWordpressVanillaDebugUnitTest --tests "org.wordpress.android.ui.sitecreation.services.SiteCreationServiceManagerTest"

Fix patch:
diff --git a/WordPress/src/main/java/org/wordpress/android/ui/sitecreation/services/SiteCreationServiceManager.kt b/WordPress/src/main/java/org/wordpress/android/ui/sitecreation/services/SiteCreationServiceManager.kt
index 86a62720f401..b4dc3824e639 100644
--- a/WordPress/src/main/java/org/wordpress/android/ui/sitecreation/services/SiteCreationServiceManager.kt
+++ b/WordPress/src/main/java/org/wordpress/android/ui/sitecreation/services/SiteCreationServiceManager.kt
@@ -136,7 +136,7 @@ class SiteCreationServiceManager @Inject constructor(
                          */
                         val errorMsg = "Site already exists - seems like an issue with domain suggestions endpoint"
                         AppLog.e(T.SITE_CREATION, errorMsg)
-                        throw IllegalStateException(errorMsg)
+                        executePhase(FAILURE)
                     }
                 } else {
                     executePhase(FAILURE)


Test patch:
diff --git a/WordPress/src/test/java/org/wordpress/android/ui/sitecreation/services/SiteCreationServiceManagerTest.kt b/WordPress/src/test/java/org/wordpress/android/ui/sitecreation/services/SiteCreationServiceManagerTest.kt
index a70333b10838..ed8ecd68a3a2 100644
--- a/WordPress/src/test/java/org/wordpress/android/ui/sitecreation/services/SiteCreationServiceManagerTest.kt
+++ b/WordPress/src/test/java/org/wordpress/android/ui/sitecreation/services/SiteCreationServiceManagerTest.kt
@@ -134,6 +134,22 @@ class SiteCreationServiceManagerTest : BaseUnitTest() {
         }
     }
 
+    @Test
+    fun verifyFailureFlowWhenTheSiteExistsAndIsNotARetry() = test {
+        setSiteExistsErrorResponses()
+        val stateBeforeFailure = CREATE_SITE_STATE
+        whenever(serviceListener.getCurrentState()).thenReturn(stateBeforeFailure)
+
+        startFlow()
+        
+        argumentCaptor<SiteCreationServiceState>().apply {
+            verify(serviceListener, times(3)).updateState(capture())
+            assertThat(allValues[0]).isEqualTo(IDLE_STATE)
+            assertThat(allValues[1]).isEqualTo(CREATE_SITE_STATE)
+            assertThat(allValues[2]).isEqualTo(FAILURE_STATE.copy(payload = stateBeforeFailure))
+        }
+    }
+
     @Test
     fun verifyRetryWorksWhenCreateSiteRequestFailed() = test {
         setGenericErrorResponses()