Browse Source

Improve Rx exceptions

pull/190/head
Oliverpool 9 years ago
parent
commit
10aece6ada
  1. 7
      patacrep/build.py
  2. 12
      patacrep/errors.py
  3. 11
      patacrep/templates.py
  4. 5
      patacrep/utils.py

7
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

12
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"""

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

5
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)
Loading…
Cancel
Save