Better format error output.

This commit is contained in:
Darko Poljak 2017-09-21 07:36:53 +02:00 committed by Darko Poljak
parent 7ebd69dcac
commit ca047c7956
2 changed files with 46 additions and 59 deletions

View file

@ -81,84 +81,72 @@ class CdistBetaRequired(cdist.Error):
return err_msg.format(*fmt_args) return err_msg.format(*fmt_args)
class CdistObjectError(Error): class CdistEntityError(Error):
"""Something went wrong while working on a specific cdist object""" """Something went wrong while executing cdist entity"""
def __init__(self, cdist_object, subject=''): def __init__(self, entity_name, entity_params, stderr_paths, subject=''):
self.cdist_object = cdist_object self.entity_name = entity_name
self.object_name = cdist_object.name.center(len(cdist_object.name)+2) self.entity_params = entity_params
self.stderr_paths = stderr_paths
if isinstance(subject, Error): if isinstance(subject, Error):
self.original_error = subject self.original_error = subject
else: else:
self.original_error = None self.original_error = None
self.message = str(subject) self.message = str(subject)
self.line_length = 74
@property @property
def stderr(self): def stderr(self):
output = [] output = []
for stderr_name in os.listdir(self.cdist_object.stderr_path): for stderr_name, stderr_path in self.stderr_paths:
stderr_path = os.path.join(self.cdist_object.stderr_path,
stderr_name)
# label = '---- '+ stderr_name +':stderr '
label = stderr_name + ':stderr '
if os.path.getsize(stderr_path) > 0: if os.path.getsize(stderr_path) > 0:
# output.append(label) label_begin = '---- BEGIN ' + stderr_name + ':stderr ----'
# output.append('{0:-^50}'.format(label.center(len(label)+2))) label_end = '---- END ' + stderr_name + ':stderr ----'
output.append('{0:-<{1}}'.format(label, self.line_length)) output.append('\n' + label_begin)
with open(stderr_path, 'r') as fd: with open(stderr_path, 'r') as fd:
output.append(fd.read()) output.append(fd.read())
output.append(label_end)
return '\n'.join(output) return '\n'.join(output)
def __str__(self): def __str__(self):
output = [] output = []
output.append(self.message) output.append(self.message)
output.append('''{label:-<{length}} header = '\nError in ' + self.entity_name + ' processing'
name: {o.name} under_header = '=' * len(header)
path: {o.absolute_path} output.append(header)
source: {o.source} output.append(under_header)
type: {o.cdist_type.absolute_path}'''.format( for param_name, param_value in self.entity_params:
label='---- object ', output.append(param_name + ': ' + str(param_value))
length=self.line_length, output.append(self.stderr + '\n')
o=self.cdist_object)
)
output.append(self.stderr)
return '\n'.join(output) 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""" """Something went wrong while executing initial manifest"""
def __init__(self, initial_manifest, stdout_path, stderr_path, subject=''): def __init__(self, initial_manifest, stderr_path, subject=''):
self.initial_manifest = initial_manifest params = [
self.stdout_path = stdout_path ('path', initial_manifest, ),
self.stderr_path = stderr_path ]
if isinstance(subject, Error): stderr_paths = []
self.original_error = subject stderr_paths = [
else: ('init', stderr_path, ),
self.original_error = None ]
self.message = str(subject) super().__init__('initial manifest', params, stderr_paths, 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 file_to_list(filename): def file_to_list(filename):

View file

@ -407,10 +407,9 @@ class Config(object):
self.manifest.run_initial_manifest(self.local.initial_manifest) self.manifest.run_initial_manifest(self.local.initial_manifest)
except cdist.Error as e: except cdist.Error as e:
which = "init" which = "init"
stdout_path = os.path.join(self.local.stdout_base_path, which)
stderr_path = os.path.join(self.local.stderr_base_path, which) stderr_path = os.path.join(self.local.stderr_base_path, which)
raise cdist.InitialManifestError(self.local.initial_manifest, raise cdist.InitialManifestError(self.local.initial_manifest,
stdout_path, stderr_path, e) stderr_path, e)
self.iterate_until_finished() self.iterate_until_finished()
self.cleanup() self.cleanup()
self._remove_files_dirs() self._remove_files_dirs()