Skip to content

fix: NgxParserBaseException.__str__ raises TypeError when lineno is None#119

Open
gaoflow wants to merge 1 commit into
nginxinc:masterfrom
gaoflow:fix/exception-str-lineno-none
Open

fix: NgxParserBaseException.__str__ raises TypeError when lineno is None#119
gaoflow wants to merge 1 commit into
nginxinc:masterfrom
gaoflow:fix/exception-str-lineno-none

Conversation

@gaoflow

@gaoflow gaoflow commented Jun 25, 2026

Copy link
Copy Markdown

Problem

NgxParserBaseException.__str__ raises TypeError: not all arguments converted during string formatting when lineno is None.

The else-branch used self.args (a 3-tuple of (strerror, filename, lineno)) with a 2-placeholder format string:

return '%s in %s' % self.args   # self.args has 3 elements → TypeError

This is triggered whenever an error is caught without a line number, e.g. through crossplane.formatter.format() when the underlying parser error carries lineno=None, or from any code that wraps a non-NgxParser exception and passes lineno=None explicitly.

Fix

Use the named instance attributes directly in both branches, so the tuple passed to % always matches the format string:

return '%s in %s:%s' % (self.strerror, self.filename, self.lineno)
# and
return '%s in %s' % (self.strerror, self.filename)

Fixes #93.

Tests

Three new tests in tests/test_analyze.py cover:

  • str() with a valid line number (regression guard)
  • str() with lineno=None (the previously crashing case)
  • same with a subclass (NgxParserSyntaxError)

All 48 tests pass.


This pull request was prepared with the assistance of AI, under my direction and review.

When lineno is None, __str__ used self.args (a 3-tuple) with a 2-placeholder
format string, causing TypeError: not all arguments converted during string
formatting. Use the named attributes directly so both branches format correctly.

Fixes nginxinc#93.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

NgxParserBaseException.__str__ may raise TypeError

1 participant