From 9afd8f7b61be3a80e0ce37e7f2a82fa6ac44db9c Mon Sep 17 00:00:00 2001 From: Oliverpool Date: Tue, 17 Nov 2015 15:14:16 +0100 Subject: [PATCH] Raise a SongSyntaxError on fatal error --- patacrep/songs/chordpro/syntax.py | 13 ++++++++++++- patacrep/songs/errors.py | 3 ++- test/test_chordpro/test_parser.py | 3 ++- 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/patacrep/songs/chordpro/syntax.py b/patacrep/songs/chordpro/syntax.py index 4f00f0a1..abc79993 100644 --- a/patacrep/songs/chordpro/syntax.py +++ b/patacrep/songs/chordpro/syntax.py @@ -7,6 +7,7 @@ import re from patacrep.songs.syntax import Parser from patacrep.songs.chordpro import ast from patacrep.songs.chordpro.lexer import tokens, ChordProLexer +from patacrep.songs import errors LOGGER = logging.getLogger() @@ -331,5 +332,15 @@ def parse_song(content, filename=None): # TODO: Provide a nicer error (an empty song?) # TODO: Add an error to the song.errors list. # using parser._errors - raise SyntaxError('Fatal error during song parsing: {}'.format(filename)) + + # pylint: disable=protected-access + if parser._errors: + # The line where it started to go wrong + lineno = parser._errors[-1].line + else: + lineno = None + raise errors.SongSyntaxError( + message='Fatal error during song parsing: {}'.format(filename), + line=lineno, + ) return parsed_content diff --git a/patacrep/songs/errors.py b/patacrep/songs/errors.py index ac15c2b8..21052d64 100644 --- a/patacrep/songs/errors.py +++ b/patacrep/songs/errors.py @@ -1,12 +1,13 @@ """Errors in song definition (syntax errors, and so on)""" -class SongError: +class SongError(Exception): """Generic song error""" # pylint: disable=too-few-public-methods type = "generic" def __init__(self, message): + super().__init__() self.message = message def __str__(self): diff --git a/test/test_chordpro/test_parser.py b/test/test_chordpro/test_parser.py index cd38afcd..01967233 100644 --- a/test/test_chordpro/test_parser.py +++ b/test/test_chordpro/test_parser.py @@ -11,6 +11,7 @@ from pkg_resources import resource_filename from patacrep import files from patacrep.build import DEFAULT_CONFIG from patacrep.encoding import open_read +from patacrep.songs import errors from .. import disable_logging from .. import dynamic # pylint: disable=unused-import @@ -119,7 +120,7 @@ class FileTest(unittest.TestCase, metaclass=dynamic.DynamicTest): sourcename = "{}.{}.source".format(base, in_format) with self.chdir('errors'): parser = self.song_plugins[out_format][in_format] - self.assertRaises(SyntaxError, parser, sourcename, self.config) + self.assertRaises(errors.SongSyntaxError, parser, sourcename, self.config) test_parse_failure.__doc__ = ( "Test that '{base}' parsing fails."