Repository: SFDO-Tooling/CumulusCI
Pull request #3659 (2023-09-21 05:39:58)

Improve message description handling for scratch org cli `not_found` error

lakshmi2506: Hi @davidmreed 

Added a descriptive message for the not_found error while creating a scratch org. It is due to not hitting the correct endpoint or doesn't have access to the API version you are using.

Please review the code 
Thank you

Patch:
diff --git a/cumulusci/core/config/scratch_org_config.py b/cumulusci/core/config/scratch_org_config.py
index 4f2b5766f..06e81198d 100644
--- a/cumulusci/core/config/scratch_org_config.py
+++ b/cumulusci/core/config/scratch_org_config.py
@@ -81,6 +81,18 @@ class ScratchOrgConfig(SfdxOrgConfig):
 
         def raise_error() -> NoReturn:
             message = f"{FAILED_TO_CREATE_SCRATCH_ORG}: \n{stdout}\n{stderr}"
+            try:
+                output = json.loads(stdout)
+                if (
+                    output.get("message") == "The requested resource does not exist"
+                    and output.get("name") == "NOT_FOUND"
+                ):
+                    raise ScratchOrgException(
+                        "The Salesforce CLI was unable to create a scratch org. Ensure you are connected using a valid API version on an active Dev Hub."
+                    )
+            except json.decoder.JSONDecodeError:
+                raise ScratchOrgException(message)
+
             raise ScratchOrgException(message)
 
         result = {}  # for type checker.
@@ -88,6 +100,7 @@ class ScratchOrgConfig(SfdxOrgConfig):
             raise_error()
         try:
             result = json.loads(stdout)
+
         except json.decoder.JSONDecodeError:
             raise_error()
 


Test patch:
diff --git a/cumulusci/core/config/tests/test_config_expensive.py b/cumulusci/core/config/tests/test_config_expensive.py
index c1ea451b0..13485a7b7 100644
--- a/cumulusci/core/config/tests/test_config_expensive.py
+++ b/cumulusci/core/config/tests/test_config_expensive.py
@@ -606,6 +606,29 @@ class TestScratchOrgConfig:
         assert config.config["created"]
         assert config.scratch_org_type == "workspace"
 
+    def test_check_apiversion_error(self, Command):
+        out = b"""{
+            "context": "Create",
+            "commandName": "Create",
+            "message": "The requested resource does not exist",
+            "name": "NOT_FOUND"
+            }"""
+
+        Command.return_value = mock.Mock(
+            stdout=io.BytesIO(out), stderr=io.BytesIO(b""), returncode=1
+        )
+        config = ScratchOrgConfig(
+            {"config_file": "tmp.json", "email_address": "test@example.com"}, "test"
+        )
+        with temporary_dir():
+            with open("tmp.json", "w") as f:
+                f.write("{}")
+            with pytest.raises(
+                SfdxOrgException,
+                match="The Salesforce CLI was unable to create a scratch org. Ensure you are connected using a valid API version on an active Dev Hub.",
+            ):
+                config.create_org()
+
     def test_create_org_no_config_file(self, Command):
         config = ScratchOrgConfig({}, "test")
         with pytest.raises(ScratchOrgException, match="missing a config_file"):