Merge "Use ProjectNotFoundError"

This commit is contained in:
Zuul 2024-03-19 08:22:24 +00:00 committed by Gerrit Code Review
commit fb796cc59a
3 changed files with 27 additions and 12 deletions

View File

@ -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(

View File

@ -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)

View File

@ -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)