Skip to content

Commit 2cf5e0f

Browse files
authored
[error] Remove redundant Error wrappers + Add RuntimeError type + Replace DecimoError with specific error types (#198)
This PR refactors Decimo’s error-handling to raise the library’s typed `DecimoError[...]` aliases directly (e.g., `ValueError`, `OverflowError`) instead of wrapping them in `Error(...)`, introduces a `RuntimeError` type, and updates call sites/tests accordingly. **Changes:** - Remove redundant `Error(...)` wrappers across TOML parsing, numeric parsing, and arithmetic modules. - Add `RuntimeError` as a `DecimoError[...]` alias and update runtime-infrastructure failure sites (e.g., MPFR availability/allocation). - Replace uses of the generic `DecimoError` wrapper with more specific error types (and update tests to match).
1 parent 78b0c9c commit 2cf5e0f

25 files changed

+858
-1352
lines changed

docs/plans/gmp_integration.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1321,7 +1321,7 @@ LD_LIBRARY_PATH=./src/decimo/gmp ./myprogram
13211321

13221322
```bash
13231323
bash src/decimo/gmp/build_gmp_wrapper.sh \
1324-
&& mojo build -I src \
1324+
&& pixi run mojo build -I src \
13251325
-Xlinker -L./src/decimo/gmp -Xlinker -ldecimo_gmp_wrapper \
13261326
-o /tmp/myprogram myprogram.mojo \
13271327
&& DYLD_LIBRARY_PATH=./src/decimo/gmp /tmp/myprogram

src/decimo/bigdecimal/arithmetics.mojo

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -320,11 +320,9 @@ def true_divide(
320320
"""
321321
# Check for division by zero
322322
if y.coefficient.is_zero():
323-
raise Error(
324-
ZeroDivisionError(
325-
message="Division by zero.",
326-
function="true_divide()",
327-
)
323+
raise ZeroDivisionError(
324+
message="Division by zero.",
325+
function="true_divide()",
328326
)
329327

330328
# Handle dividend of zero
@@ -698,11 +696,9 @@ def true_divide_inexact(
698696

699697
# Check for division by zero
700698
if x2.coefficient.is_zero():
701-
raise Error(
702-
ZeroDivisionError(
703-
message="Division by zero.",
704-
function="true_divide_inexact()",
705-
)
699+
raise ZeroDivisionError(
700+
message="Division by zero.",
701+
function="true_divide_inexact()",
706702
)
707703

708704
# Handle dividend of zero
@@ -916,11 +912,9 @@ def truncate_divide(x1: BigDecimal, x2: BigDecimal) raises -> BigDecimal:
916912
"""
917913
# Check for division by zero
918914
if x2.coefficient.is_zero():
919-
raise Error(
920-
ZeroDivisionError(
921-
message="Division by zero.",
922-
function="truncate_divide()",
923-
)
915+
raise ZeroDivisionError(
916+
message="Division by zero.",
917+
function="truncate_divide()",
924918
)
925919

926920
# Handle dividend of zero
@@ -961,11 +955,9 @@ def truncate_modulo(
961955
"""
962956
# Check for division by zero
963957
if x2.coefficient.is_zero():
964-
raise Error(
965-
ZeroDivisionError(
966-
message="Division by zero.",
967-
function="truncate_modulo()",
968-
)
958+
raise ZeroDivisionError(
959+
message="Division by zero.",
960+
function="truncate_modulo()",
969961
)
970962

971963
return subtract(

src/decimo/bigdecimal/bigdecimal.mojo

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ from std.memory import UnsafePointer
2727
from std.python import PythonObject
2828
from std import testing
2929

30-
from decimo.errors import DecimoError, ConversionError, ValueError
30+
from decimo.errors import ConversionError, ValueError
3131
from decimo.rounding_mode import RoundingMode
3232
from decimo.bigdecimal.rounding import round_to_precision
3333
from decimo.bigint10.bigint10 import BigInt10
@@ -362,22 +362,18 @@ struct BigDecimal(
362362
return Self(coefficient=BigUInt.zero(), scale=0, sign=False)
363363

364364
if value != value: # Check for NaN
365-
raise Error(
366-
ValueError(
367-
message="Cannot convert NaN to BigDecimal.",
368-
function="BigDecimal.from_scalar()",
369-
)
365+
raise ValueError(
366+
message="Cannot convert NaN to BigDecimal.",
367+
function="BigDecimal.from_scalar()",
370368
)
371369
# Convert to string with full precision
372370
try:
373371
return Self.from_string(String(value))
374372
except e:
375-
raise Error(
376-
ConversionError(
377-
message="Cannot convert scalar to BigDecimal.",
378-
function="BigDecimal.from_scalar()",
379-
previous_error=e^,
380-
)
373+
raise ConversionError(
374+
message="Cannot convert scalar to BigDecimal.",
375+
function="BigDecimal.from_scalar()",
376+
previous_error=e^,
381377
)
382378

383379
@staticmethod
@@ -544,12 +540,10 @@ struct BigDecimal(
544540
return Self(coefficient=coefficient^, scale=scale, sign=sign)
545541

546542
except e:
547-
raise Error(
548-
ConversionError(
549-
message="Failed to convert Python Decimal to BigDecimal.",
550-
function="BigDecimal.from_python_decimal()",
551-
previous_error=e^,
552-
),
543+
raise ConversionError(
544+
message="Failed to convert Python Decimal to BigDecimal.",
545+
function="BigDecimal.from_python_decimal()",
546+
previous_error=e^,
553547
)
554548

555549
# ===------------------------------------------------------------------=== #

src/decimo/bigdecimal/constants.mojo

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -165,10 +165,8 @@ def pi(precision: Int) raises -> BigDecimal:
165165
"""
166166

167167
if precision < 0:
168-
raise Error(
169-
ValueError(
170-
message="Precision must be non-negative", function="pi()"
171-
)
168+
raise ValueError(
169+
message="Precision must be non-negative", function="pi()"
172170
)
173171

174172
# TODO: When global variables are supported,

0 commit comments

Comments
 (0)