From 99b3c11ce248400a07b4c21ff0f4b244bf2f1640 Mon Sep 17 00:00:00 2001 From: "James E. Blair" Date: Fri, 15 Mar 2024 10:24:33 -0700 Subject: [PATCH] Use ProjectNotFoundError This error is used in some places, but not all. Correct that to improve config error structured data. Change-Id: Ice4fbee679ff8e7ab05042452bbd4f45ca8f1122 --- tests/unit/test_v3.py | 9 ++++++--- zuul/configloader.py | 5 ++--- zuul/exceptions.py | 25 +++++++++++++++++++------ 3 files changed, 27 insertions(+), 12 deletions(-) diff --git a/tests/unit/test_v3.py b/tests/unit/test_v3.py index ebe96a8cf8..93e0f16f87 100644 --- a/tests/unit/test_v3.py +++ b/tests/unit/test_v3.py @@ -2536,7 +2536,8 @@ class TestInRepoConfig(ZuulTestCase): self.assertEqual(A.data['status'], 'NEW') self.assertEqual(A.reported, 1, "A should report failure") - self.assertIn('Unknown projects: does-not-exist', A.messages[0], + self.assertIn('The project "does-not-exist" was not found', + A.messages[0], "A should have a syntax error reported") def test_required_project_not_found_multiple_error(self): @@ -2561,8 +2562,10 @@ class TestInRepoConfig(ZuulTestCase): self.assertEqual(A.data['status'], 'NEW') self.assertEqual(A.reported, 1, "A should report failure") - self.assertIn('Unknown projects: does-not-exist, also-does-not-exist', - A.messages[0], "A should have a syntax error reported") + self.assertIn('The projects "does-not-exist", ' + '"also-does-not-exist" were not found.', + A.messages[0], + "A should have a syntax error reported") def test_template_not_found_error(self): in_repo_conf = textwrap.dedent( diff --git a/zuul/configloader.py b/zuul/configloader.py index b7c9bab6f5..2242841405 100644 --- a/zuul/configloader.py +++ b/zuul/configloader.py @@ -851,8 +851,7 @@ class JobParser(object): # exception only once to capture all of them in the # error message. if unknown_projects: - names = ", ".join(unknown_projects) - raise Exception("Unknown projects: %s" % (names,)) + raise ProjectNotFoundError(unknown_projects) job.required_projects = new_projects @@ -925,7 +924,7 @@ class JobParser(object): for p in as_list(allowed_projects): (trusted, project) = self.pcontext.tenant.getProject(p) if project is None: - raise Exception("Unknown project %s" % (p,)) + raise ProjectNotFoundError(p) allowed.append(project.name) job.allowed_projects = frozenset(allowed) diff --git a/zuul/exceptions.py b/zuul/exceptions.py index 2ab47ba717..11391ab172 100644 --- a/zuul/exceptions.py +++ b/zuul/exceptions.py @@ -216,12 +216,25 @@ class ProjectNotFoundError(ConfigurationSyntaxError): zuul_error_name = 'Project Not Found' def __init__(self, project): - message = textwrap.dedent("""\ - The project "{project}" was not found. All projects - referenced within a Zuul configuration must first be - added to the main configuration file by the Zuul - administrator.""") - message = textwrap.fill(message.format(project=project)) + projects = None + if isinstance(project, (list, tuple)): + if len(project) > 1: + projects = ', '.join(f'"{p}"' for p in project) + else: + project = project[0] + if projects: + message = textwrap.dedent(f"""\ + The projects {projects} were not found. All projects + referenced within a Zuul configuration must first be + added to the main configuration file by the Zuul + administrator.""") + else: + message = textwrap.dedent(f"""\ + The project "{project}" was not found. All projects + referenced within a Zuul configuration must first be + added to the main configuration file by the Zuul + administrator.""") + message = textwrap.fill(message) super(ProjectNotFoundError, self).__init__(message)