From 6b0148241a2e67d7d6731325780eac96c2c672ae Mon Sep 17 00:00:00 2001 From: dnutiu Date: Sun, 21 Jan 2024 22:59:55 +0200 Subject: [PATCH] improve parser exception handling --- src/dice/parser.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/dice/parser.py b/src/dice/parser.py index 8c901a4..8051038 100644 --- a/src/dice/parser.py +++ b/src/dice/parser.py @@ -35,7 +35,12 @@ class DieParser: """ Parses the die expression and returns the result. """ - clean_expression = "".join(expression.split()) - result = self._parser.parse(clean_expression, semantics=self._semantics) - logging.debug(f"rolling die for {expression} -> result={result}") - return result + try: + clean_expression = "".join(expression.split()) + result = self._parser.parse(clean_expression, semantics=self._semantics) + logging.debug(f"rolling die for {expression} -> result={result}") + return result + except tatsu.exceptions.FailedParse as e: + message = f"Failed to roll {expression}: {e.message}" + self._logger.error(message) + raise ValueError(message) from e