From 7ebd69dcace7746d6f67ee1db0cb9c4e3373263b Mon Sep 17 00:00:00 2001 From: Darko Poljak Date: Mon, 18 Sep 2017 23:14:09 +0200 Subject: [PATCH] Refactor init manifest error handling. --- cdist/__init__.py | 4 ++-- cdist/config.py | 9 ++++++++- cdist/core/manifest.py | 14 +++++--------- 3 files changed, 15 insertions(+), 12 deletions(-) diff --git a/cdist/__init__.py b/cdist/__init__.py index cc9c8875..5a78a1b3 100644 --- a/cdist/__init__.py +++ b/cdist/__init__.py @@ -141,8 +141,8 @@ class InitialManifestError(Error): @property def stderr(self): output = [] - label = "init" + ':stderr ' if os.path.getsize(self.stderr_path) > 0: + label = "init" + ':stderr ' output.append('{0:-<{1}}'.format(label, self.line_length)) with open(self.stderr_path, 'r') as fd: output.append(fd.read()) @@ -152,7 +152,7 @@ class InitialManifestError(Error): output = [] output.append(self.message) output.append('''{label:-<{length}} -initial manifest: {im}'''.format( +path: {im}'''.format( label='---- initial manifest ', length=self.line_length, im=self.initial_manifest) diff --git a/cdist/config.py b/cdist/config.py index 65cb97d4..5c62d07d 100644 --- a/cdist/config.py +++ b/cdist/config.py @@ -403,7 +403,14 @@ class Config(object): self._init_files_dirs() self.explorer.run_global_explorers(self.local.global_explorer_out_path) - self.manifest.run_initial_manifest(self.local.initial_manifest) + try: + self.manifest.run_initial_manifest(self.local.initial_manifest) + except cdist.Error as e: + which = "init" + stdout_path = os.path.join(self.local.stdout_base_path, which) + stderr_path = os.path.join(self.local.stderr_base_path, which) + raise cdist.InitialManifestError(self.local.initial_manifest, + stdout_path, stderr_path, e) self.iterate_until_finished() self.cleanup() self._remove_files_dirs() diff --git a/cdist/core/manifest.py b/cdist/core/manifest.py index a4277aa3..12b5b005 100644 --- a/cdist/core/manifest.py +++ b/cdist/core/manifest.py @@ -158,15 +158,11 @@ class Manifest(object): stdout_path = os.path.join(self.local.stdout_base_path, which) with open(stderr_path, 'ba+') as stderr, \ open(stdout_path, 'ba+') as stdout: - try: - self.local.run_script( - initial_manifest, - env=self.env_initial_manifest(initial_manifest), - message_prefix=message_prefix, - stdout=stdout, stderr=stderr) - except cdist.Error as e: - raise cdist.InitialManifestError(initial_manifest, stdout_path, - stderr_path, e) + self.local.run_script( + initial_manifest, + env=self.env_initial_manifest(initial_manifest), + message_prefix=message_prefix, + stdout=stdout, stderr=stderr) def env_type_manifest(self, cdist_object): type_manifest = os.path.join(self.local.type_path,