Skip to content

Commit

Permalink
Implement a base ParsimoniousError exception class (#221)
Browse files Browse the repository at this point in the history
* Implement a base ParsimoniousError exception class

This commit adds a `ParsimoniousError` class. This replaces the use of `Exception` as a parent class. This is introduced to simplify code by callers who may want to be able to catch all Parsimonious errors using a single except block but not unrelated exceptions.

* Update parsimonious/exceptions.py

Co-authored-by: [object Object] <[email protected]>
  • Loading branch information
kkirsche and lucaswiman authored Sep 13, 2022
1 parent 12c4d77 commit d5636a6
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions parsimonious/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@
from parsimonious.utils import StrAndRepr


class ParseError(StrAndRepr, Exception):
class ParsimoniousError(Exception):
"""A base exception class to allow library users to catch any Parsimonious error."""
pass


class ParseError(StrAndRepr, ParsimoniousError):
"""A call to ``Expression.parse()`` or ``match()`` didn't match."""

def __init__(self, text, pos=-1, expr=None):
Expand Down Expand Up @@ -71,7 +76,7 @@ def __str__(self):
self.column())


class VisitationError(Exception):
class VisitationError(ParsimoniousError):
"""Something went wrong while traversing a parse tree.
This exception exists to augment an underlying exception with information
Expand Down Expand Up @@ -100,7 +105,7 @@ def __init__(self, exc, exc_class, node):
node.prettily(error=node)))


class BadGrammar(StrAndRepr, Exception):
class BadGrammar(StrAndRepr, ParsimoniousError):
"""Something was wrong with the definition of a grammar.
Note that a ParseError might be raised instead if the error is in the
Expand Down

0 comments on commit d5636a6

Please sign in to comment.