From 10aece6ada965c45b9c26954d7760d9b035beffe Mon Sep 17 00:00:00 2001 From: Oliverpool Date: Tue, 9 Feb 2016 17:53:36 +0100 Subject: [PATCH] Improve Rx exceptions --- patacrep/build.py | 7 ++++++- patacrep/errors.py | 12 ++++++++---- patacrep/templates.py | 11 +++++++++-- patacrep/utils.py | 5 ++--- 4 files changed, 25 insertions(+), 10 deletions(-) diff --git a/patacrep/build.py b/patacrep/build.py index 0e86f74a..f6b176c2 100644 --- a/patacrep/build.py +++ b/patacrep/build.py @@ -44,7 +44,12 @@ class Songbook: def __init__(self, raw_songbook, basename): # Validate config schema = config_model('schema') - utils.validate_yaml_schema(raw_songbook, schema) + + try: + utils.validate_yaml_schema(raw_songbook, schema) + except errors.SchemaError as exception: + exception.message = "The songbook file '{}' is not valid".format(basename) + raise exception self._raw_config = raw_songbook self.basename = basename diff --git a/patacrep/errors.py b/patacrep/errors.py index 08eebc6a..a72fac2f 100644 --- a/patacrep/errors.py +++ b/patacrep/errors.py @@ -7,15 +7,19 @@ class SongbookError(Exception): """ pass -class SBFileError(SongbookError): - """Error during songbook file decoding""" +class SchemaError(SongbookError): + """Error on the songbook schema""" - def __init__(self, message=None): + def __init__(self, message='', rx_exception=None): super().__init__() self.message = message + self.rx_exception = rx_exception def __str__(self): - return self.message + if self.rx_exception: + return self.message + "\n" + str(self.rx_exception) + else: + return self.message class TemplateError(SongbookError): """Error during template generation""" diff --git a/patacrep/templates.py b/patacrep/templates.py index 46a7ca8f..0fbf7115 100644 --- a/patacrep/templates.py +++ b/patacrep/templates.py @@ -163,19 +163,26 @@ class TexBookRenderer(Renderer): def get_all_variables(self, user_config): '''Validate template variables (and set defaults when needed) + + Will raise `SchemaError` if any data does not respect the schema ''' data = self.get_template_variables(self.template) variables = dict() for templatename, param in data.items(): template_config = user_config.get(templatename, {}) - variables[templatename] = self._get_variables(param, template_config) + try: + variables[templatename] = self._get_variables(param, template_config) + except errors.SchemaError as exception: + exception.message = "The songbook file is not valid\n" + exception.message += "'template' > '{}' >".format(templatename) + raise exception return variables @staticmethod def _get_variables(parameter, user_config): '''Get the default value for the parameter, according to the language. - May raise an errors.SBFileError if the data does not respect the schema + Will raise `SchemaError` if the data does not respect the schema ''' data = utils.DictOfDict(parameter.get('default', {})) data.update(user_config) diff --git a/patacrep/utils.py b/patacrep/utils.py index 3949432e..96a8617f 100644 --- a/patacrep/utils.py +++ b/patacrep/utils.py @@ -81,7 +81,7 @@ def yesno(string): def validate_yaml_schema(data, schema): """Check that the data respects the schema - Will raise `SBFileError` if the schema is not respected. + Will raise `SchemaError` if the schema is not respected. """ schema = Rx.make_schema(schema) @@ -91,5 +91,4 @@ def validate_yaml_schema(data, schema): try: schema.validate(data) except Rx.SchemaMismatch as exception: - msg = 'Could not parse songbook file:\n' + str(exception) - raise errors.SBFileError(msg) + raise errors.SchemaError(rx_exception=exception) \ No newline at end of file