Skip to content

Commit 27f180a

Browse files
committed
Add the ArithmeticError exception
1 parent 2c03cbd commit 27f180a

File tree

4 files changed

+21
-5
lines changed

4 files changed

+21
-5
lines changed

docs/source/rule_engine/errors.rst

+6
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ Data
1414
Exceptions
1515
----------
1616

17+
.. autoexception:: ArithmeticError
18+
:members:
19+
:show-inheritance:
20+
:special-members: __init__
21+
1722
.. autoexception:: AttributeResolutionError
1823
:members:
1924
:show-inheritance:
@@ -103,6 +108,7 @@ The class hierarchy for Rule Engine exceptions is:
103108
104109
EngineError
105110
+-- EvaluationError
111+
+-- ArithmeticError
106112
+-- AttributeResolutionError
107113
+-- AttributeTypeError
108114
+-- FunctionCallError

lib/rule_engine/ast.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -497,9 +497,9 @@ def __op_arithmetic(self, op, thing):
497497
try:
498498
result = op(left_value, right_value)
499499
except ZeroDivisionError:
500-
raise errors.EvaluationError('arithmetic error: division by zero') from None
500+
raise errors.ArithmeticError('arithmetic error: division by zero') from None
501501
except ArithmeticError:
502-
raise errors.EvaluationError('arithmetic error') from None
502+
raise errors.ArithmeticError('arithmetic error') from None
503503
return result
504504

505505
_op_fdiv = functools.partialmethod(__op_arithmetic, operator.floordiv)

lib/rule_engine/errors.py

+8-2
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ class EvaluationError(EngineError):
6161
An error raised for issues which occur while the rule is being evaluated. This can occur at parse time while AST
6262
nodes are being evaluated during the reduction phase.
6363
"""
64-
pass
6564

6665
class SyntaxError(EngineError):
6766
"""A base error for syntax related issues."""
@@ -171,7 +170,7 @@ def __init__(self, message, token=None):
171170

172171
class AttributeResolutionError(EvaluationError):
173172
"""
174-
An error raised with an attribute can not be resolved to a value.
173+
An error raised when an attribute can not be resolved to a value.
175174
176175
.. versionadded:: 2.0.0
177176
"""
@@ -309,3 +308,10 @@ def __init__(self, message, error=None, function_name=None):
309308
self.error = error
310309
"""The exception from which this error was triggered."""
311310
self.function_name = function_name
311+
312+
class ArithmeticError(EvaluationError):
313+
"""
314+
An error raised when there is an issue performing an arithmetic operation.
315+
316+
.. versionadded:: 5.0.0
317+
"""

tests/ast/expression/left_operator_right.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,11 @@ def test_ast_expression_left_operator_right_arithmetic(self):
9696
self.assertExpressionTests('mul', equals_value=8.0)
9797
self.assertExpressionTests('pow', equals_value=16.0)
9898

99+
def test_ast_expression_left_operator_right_arithmetic_errors(self):
100+
for operation in ('fdiv', 'tdiv'):
101+
with self.assertRaises(errors.ArithmeticError):
102+
self.assertExpressionTests(operation, ast.FloatExpression(context, 2.0), ast.FloatExpression(context, 0.0))
103+
99104
def test_ast_expression_left_operator_right_arithmetic_type_errors(self):
100105
for operation in ('fdiv', 'tdiv', 'mod', 'mul', 'pow'):
101106
with self.assertRaises(errors.EvaluationError):
@@ -106,7 +111,6 @@ def test_ast_expression_left_operator_right_arithmetic_type_errors(self):
106111
self.assertExpressionTests(operation, ast.FloatExpression(context, 2.0), ast.BooleanExpression(context, True))
107112
with self.assertRaises(errors.EvaluationError):
108113
self.assertExpressionTests(operation, ast.BooleanExpression(context, True), ast.FloatExpression(context, 4.0))
109-
110114
class AddExpressionTests(LeftOperatorRightExpresisonTestsBase):
111115
ExpressionClass = ast.AddExpression
112116
false_value = 0.0

0 commit comments

Comments
 (0)