Skip to content

Commit 73155f8

Browse files
committed
Add error(Error) and allow subclassing of Error
The type is stored in the "value" field of the Error object.
1 parent ce77388 commit 73155f8

File tree

3 files changed

+24
-5
lines changed

3 files changed

+24
-5
lines changed

M2/Macaulay2/d/actors4.d

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -663,10 +663,20 @@ stringcatfun(e:Expr):Expr := (
663663
setupfun("concatenate",stringcatfun);
664664

665665
errorfun(e:Expr):Expr := (
666-
e = stringcatfun(e);
667666
when e
668667
is s:stringCell do buildErrorPacket(s.v)
669-
else buildErrorPacket("expects a string or sequence of strings as its argument"));
668+
is x:HashTable do (
669+
if ancestor(x.Class, errorClass) then (
670+
y := lookup1(x, toExpr("message"));
671+
if y == notfoundE
672+
then buildErrorPacket("expected a message")
673+
else (
674+
when y
675+
is msg:stringCell
676+
do Expr(Error(dummyPosition, msg.v, x.Class, false, dummyFrame))
677+
else buildErrorPacket("expected message to be a string")))
678+
else buildErrorPacket("expected an error object"))
679+
else buildErrorPacket("expected a string or hash table"));
670680
setupfun("error",errorfun).Protected = false; -- this will be replaced by a toplevel function that calls this one
671681

672682
mingleseq(a:Sequence):Expr := (

M2/Macaulay2/d/evaluate.d

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1672,6 +1672,11 @@ setupop(TestS, testfun);
16721672

16731673
export toExpr(err:Error):Expr := (
16741674
h := newHashTable(errorClass, nothingClass);
1675+
when err.value
1676+
is val:HashTable do (
1677+
if ancestor(val, errorClass) then h.Class = val
1678+
else nothing)
1679+
else nothing;
16751680
h.beingInitialized = true;
16761681
storeInHashTable(h, toExpr("position"), locate(err.position));
16771682
storeInHashTable(h, toExpr("message"), toExpr(err.message));

M2/Macaulay2/m2/debugging.m2

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
needs "nets.m2"
44
needs "methods.m2"
55

6+
new Error from String := (T, msg) -> new T from {"message" => msg}
7+
68
processArgs := args -> concatenate (
79
args = sequence args;
810
apply(args, x ->
@@ -13,9 +15,11 @@ processArgs := args -> concatenate (
1315
apply(args, x -> if class x === Symbol then ("\n", toString locate x, ": here is the first use of '",toString x, "'") else "")
1416
)
1517
olderror := error
16-
error = args -> (
17-
-- this is the body of the "error" function, which prints out error messages
18-
olderror processArgs args)
18+
error = method(Dispatch => Thing)
19+
error String := olderror
20+
error Error := err -> olderror new class err from processArgs err#"message"
21+
error Sequence := olderror @@ processArgs
22+
error Thing := error @@ sequence
1923
protect symbol error
2024

2125
warningMessage0 = (args,deb) -> (

0 commit comments

Comments
 (0)