diff --git a/crossplane/errors.py b/crossplane/errors.py index bcb7904..cd1d616 100644 --- a/crossplane/errors.py +++ b/crossplane/errors.py @@ -10,9 +10,9 @@ def __init__(self, strerror, filename, lineno): def __str__(self): if self.lineno is not None: - return '%s in %s:%s' % self.args + return '%s in %s:%s' % (self.strerror, self.filename, self.lineno) else: - return '%s in %s' % self.args + return '%s in %s' % (self.strerror, self.filename) class NgxParserSyntaxError(NgxParserBaseException): diff --git a/tests/test_analyze.py b/tests/test_analyze.py index e407ef4..fc0fdea 100644 --- a/tests/test_analyze.py +++ b/tests/test_analyze.py @@ -1,5 +1,6 @@ # -*- coding: utf-8 -*- import crossplane +from crossplane.errors import NgxParserBaseException, NgxParserSyntaxError def test_state_directive(): @@ -57,3 +58,21 @@ def test_flag_directive_args(): raise Exception('bad args for flag directive: ' + repr(args)) except crossplane.errors.NgxParserDirectiveArgumentsError as e: assert e.strerror.endswith('it must be "on" or "off"') + + +def test_exception_str_with_lineno(): + # str() with a line number should include all three fields + e = NgxParserBaseException('some error', 'nginx.conf', 42) + assert str(e) == 'some error in nginx.conf:42' + + +def test_exception_str_without_lineno(): + # str() with lineno=None must not raise TypeError (#93) + e = NgxParserBaseException('some error', 'nginx.conf', None) + assert str(e) == 'some error in nginx.conf' + + +def test_exception_str_subclass_without_lineno(): + # same contract holds for subclasses + e = NgxParserSyntaxError('unexpected "}"', 'nginx.conf', None) + assert str(e) == 'unexpected "}" in nginx.conf'