scicloj / wolframite

An interface between Clojure and Wolfram Language (the language of Mathematica)
https://scicloj.github.io/wolframite/
Mozilla Public License 2.0
56 stars 2 forks source link

Improve failed evaluation detection, reporting #123

Closed holyjak closed 1 month ago

holyjak commented 2 months ago

Issue: When we ask Wolfram to do something impossible, it typically returns the input form or $Failed, so it is hard to see what was wrong. However, when evaluated in a notebook, it does print an explanation. This is however ignored by the recommended .waitForAnswer.

Fix: Follow the docs and add a packet listener, which captures the messages we care about in these cases (text, message) between we start eval and read the result. To make this easier, move the impl. of evaluation into a new protocol method evaluate! + add extracting captured messages from the listener + throw an error if the result seems to indicate a problem.

Bonus: Upgrade Clojure to stable.

Demo

What does this look like?

(wl/eval "Subtract[1]")
Execution error (ExceptionInfo) at wolframite.impl.jlink_proto_impl.JLinkImpl/evaluate_BANG_ (jlink_proto_impl.clj:151).
Evaluation seems to have failed. Result: Subtract[1] Details: Subtract::argr: Subtract called with 1 argument; 2 arguments are expected.

(wl/eval "Get[\"/no-such-file\"]")
Execution error (ExceptionInfo) at wolframite.impl.jlink_proto_impl.JLinkImpl/evaluate_BANG_ (jlink_proto_impl.clj:151).
Evaluation seems to have failed. Result: $Failed Details: Get::noopen: Cannot open /no-such-file.

(wl/eval "Print[\"hello!\"]") ; => nil
[nREPL-session-101a2c04-a45a-4559-9cb5-319d8574a740] INFO wolframite.impl.jlink-proto-impl - Messages retrieved during evaluation: [hello!
]
light-matters commented 1 month ago

Capturing errors is a great improvement. Should be mentioned in our upcoming talks.