simple Error Handling
mfleschutz opened this issue · comments
It would be very helpful to have an example that demonstrates in a simple way, how to be able to debug a .mod-file. At the moment I don't know how to set up the error handler right.
Examples dedicated to ErrorHandler's and OutputHandler's are planned but have not been added yet. Here is some information that may help you meanwhile:
- In asyncexample.py there is a simple error handler.
- You can also see the implementation of the default error handler in errorhandler.py.
- The documentation for AMPLException, which is passed as argument to ErrorHandler.error and ErrorHandler.warning is available at http://amplpy.readthedocs.io/en/latest/classes/amplexception.html;
AMPLException.getLineNumber()
may be what you are looking for.
Is this information helpful or did you have something else in mind for debugging mod files?
It works. Is it also possible to get the syntax error messages like this:
syntax error
context: >>> set <<< S default {1..8};
Unfortunately that would require changes in the core API which may take a while to be made. Currently the only way to locate the error is by the line number or the offset. I opened a ticked for the internal API with this feature request.
Dear @fdabrandao,
thanks for all your developments! I have the same interest like @mfleschutz. Here is a example code snippet to demonstrate the current options:
import amplpy
ampl = amplpy.AMPL()
class MyErrorHandler(amplpy.ErrorHandler):
def error(self, exception):
print(f'AMPL Error, line {exception.getLineNumber()}, offset {exception.getOffset()}: {exception.getMessage()}')
ampl.setErrorHandler(MyErrorHandler())
ampl.eval("param b := 4;\nlet a := 3;")
prints AMPL Error, line 2, offset 19: syntax error
and not
file_path, line 2 (offset 19):
syntax error
context: let >>> a <<< := 3;
as AMPL is printing.
Hi @SteffenMeinecke,
Even though I forgot to comment here, context has been included in error messaged in x-ampl
for some time and it has been included in the latest version of AMPL. Which version of AMPL do you have? Upgrading to the latest version of AMPL should fix the issue.
I was using amplpy 0.7.1 and AMPL Version 20221013. Since your message I updated to amplpy 0.8.6.
Could you please show how to extend above minimal example to print the context message in python? (I can now find item __suppress_context__
in the exception object, but that doesn't help because the value is already false.)
Your example was already correct, you just need to upgrade AMPL from 20221013 to 20230124 (the fix was introduced in AMPL itself):
$ cat test.py
import amplpy
ampl = amplpy.AMPL()
class MyErrorHandler(amplpy.ErrorHandler):
def error(self, exception):
print(
f"AMPL Error, line {exception.getLineNumber()}, offset {exception.getOffset()}: {exception.getMessage()}"
)
ampl.setErrorHandler(MyErrorHandler())
ampl.eval("option version;")
ampl.eval("param b := 4;\nlet a := 3;")
$ python test.py
option version 'AMPL Version 20230124 (Darwin-21.6.0, 64-bit)\
Licensed to AMPL Cloud License For Development/Testing.\
Temporary license expires 20231231.\
Using license file "/Users/fdabrandao/bin/ampl.macos64/ampl.lic".\
';
AMPL Error, line 2, offset 18: syntax error
context: let >>> a <<< := 3;
BTW: you can use amplpy modules to simplify the upgrade since AMPL and all solvers are now available as python modules: https://amplpy.readthedocs.io/en/latest/getting-started.html#amplpy-modules
In case you are using a time-limited bundle, you need to generate a new one.
Perfect, it works.
I think, this issue can be closed.
Great! Over the next several months we will working substancially on the Python API so expect to see new features showing up soon and feel free to open issues for anything you think would be important to fix or improve.