From ca047c7956375b13bb537e3476985492fd6126c7 Mon Sep 17 00:00:00 2001 From: Darko Poljak Date: Thu, 21 Sep 2017 07:36:53 +0200 Subject: [PATCH] Better format error output. --- cdist/__init__.py | 102 ++++++++++++++++++++-------------------------- cdist/config.py | 3 +- 2 files changed, 46 insertions(+), 59 deletions(-) diff --git a/cdist/__init__.py b/cdist/__init__.py index 5a78a1b3..832a0dca 100644 --- a/cdist/__init__.py +++ b/cdist/__init__.py @@ -81,84 +81,72 @@ class CdistBetaRequired(cdist.Error): return err_msg.format(*fmt_args) -class CdistObjectError(Error): - """Something went wrong while working on a specific cdist object""" - def __init__(self, cdist_object, subject=''): - self.cdist_object = cdist_object - self.object_name = cdist_object.name.center(len(cdist_object.name)+2) +class CdistEntityError(Error): + """Something went wrong while executing cdist entity""" + def __init__(self, entity_name, entity_params, stderr_paths, subject=''): + self.entity_name = entity_name + self.entity_params = entity_params + self.stderr_paths = stderr_paths if isinstance(subject, Error): self.original_error = subject else: self.original_error = None self.message = str(subject) - self.line_length = 74 @property def stderr(self): output = [] - for stderr_name in os.listdir(self.cdist_object.stderr_path): - stderr_path = os.path.join(self.cdist_object.stderr_path, - stderr_name) - # label = '---- '+ stderr_name +':stderr ' - label = stderr_name + ':stderr ' + for stderr_name, stderr_path in self.stderr_paths: if os.path.getsize(stderr_path) > 0: - # output.append(label) - # output.append('{0:-^50}'.format(label.center(len(label)+2))) - output.append('{0:-<{1}}'.format(label, self.line_length)) + label_begin = '---- BEGIN ' + stderr_name + ':stderr ----' + label_end = '---- END ' + stderr_name + ':stderr ----' + output.append('\n' + label_begin) with open(stderr_path, 'r') as fd: output.append(fd.read()) + output.append(label_end) return '\n'.join(output) def __str__(self): output = [] output.append(self.message) - output.append('''{label:-<{length}} -name: {o.name} -path: {o.absolute_path} -source: {o.source} -type: {o.cdist_type.absolute_path}'''.format( - label='---- object ', - length=self.line_length, - o=self.cdist_object) - ) - output.append(self.stderr) + header = '\nError in ' + self.entity_name + ' processing' + under_header = '=' * len(header) + output.append(header) + output.append(under_header) + for param_name, param_value in self.entity_params: + output.append(param_name + ': ' + str(param_value)) + output.append(self.stderr + '\n') return '\n'.join(output) -class InitialManifestError(Error): +class CdistObjectError(CdistEntityError): + """Something went wrong while working on a specific cdist object""" + def __init__(self, cdist_object, subject=''): + params = [ + ('name', cdist_object.name, ), + ('path', cdist_object.absolute_path, ), + ('source', " ".join(cdist_object.source), ), + ('type', cdist_object.cdist_type.absolute_path, ), + ] + stderr_paths = [] + for stderr_name in os.listdir(cdist_object.stderr_path): + stderr_path = os.path.join(cdist_object.stderr_path, + stderr_name) + stderr_paths.append((stderr_name, stderr_path, )) + super().__init__('object', params, stderr_paths, subject) + + +class InitialManifestError(CdistEntityError): """Something went wrong while executing initial manifest""" - def __init__(self, initial_manifest, stdout_path, stderr_path, subject=''): - self.initial_manifest = initial_manifest - self.stdout_path = stdout_path - self.stderr_path = stderr_path - if isinstance(subject, Error): - self.original_error = subject - else: - self.original_error = None - self.message = str(subject) - self.line_length = 74 - - @property - def stderr(self): - output = [] - 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()) - return '\n'.join(output) - - def __str__(self): - output = [] - output.append(self.message) - output.append('''{label:-<{length}} -path: {im}'''.format( - label='---- initial manifest ', - length=self.line_length, - im=self.initial_manifest) - ) - output.append(self.stderr) - return '\n'.join(output) + def __init__(self, initial_manifest, stderr_path, subject=''): + params = [ + ('path', initial_manifest, ), + ] + stderr_paths = [] + stderr_paths = [ + ('init', stderr_path, ), + ] + super().__init__('initial manifest', params, stderr_paths, subject) def file_to_list(filename): diff --git a/cdist/config.py b/cdist/config.py index 5c62d07d..dc73830c 100644 --- a/cdist/config.py +++ b/cdist/config.py @@ -407,10 +407,9 @@ class Config(object): 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) + stderr_path, e) self.iterate_until_finished() self.cleanup() self._remove_files_dirs()