mattn / anko

Scriptable interpreter written in golang
http://play-anko.appspot.com/
MIT License
1.45k stars 120 forks source link

Could I throw an error in script for anko to catch and return it to caller #338

Closed borgerli closed 4 years ago

borgerli commented 4 years ago

We use anko as script to process files dynamically in our golang project.
Sometimes the file's format is incorrect, such as missing some necessary fields. Could I throw an error in script? And then anko can catch and return the error to caller

Thanks.

MichaelS11 commented 4 years ago

Sure. Run or RunContext return an error. Or maybe return a variable on what files are incorrect. There are lots of options.

borgerli commented 4 years ago

Thanks, Michael @MichaelS11

Anko is really cool for Go programmers

dsikes commented 3 years ago

@MichaelS11 Thanks for your help on this awesome project. I was wondering if you could provide an example of how to use Run or RunContext to implement a stack trace of sorts?

for example, let's say we had a defined function called read in the environment. Then, in our script we had something like this:

text, err = read("sometext.txt")
if err != nil {
    // we were unable to read the file for some reason...
}

What would be your recommendation for avoiding checking for errors inline (like in the psuedo script above), as a 2nd return value (like go does), and implementing something similar to a Python exception. If the error occurred, and was not caught, then it would exit, with some stack trace. But if the error was caught, then it could be handled, and the script could continue to run.

Again, thanks for the awesome project. Just looking for a little guidance / recommendation on how to implement something like this.

MichaelS11 commented 3 years ago

Most welcome :)

Why do you want a stack trace? What would you use it for? You could also use https://golang.org/pkg/runtime/debug/#Stack without throwing an exception. I have a feeling there is probably a better way. Maybe a return variable or a custom error. Need more information.

I would tend to stick with the blah, err = foo() then check error format unless there is no other way.

dsikes commented 3 years ago

So, in my particular use case I am writing an automation language called Orcascript. I am using anko as the foundation, and providing a suite of libraries that should "just work" out of the box for a lot of common devops/sre automation tasks. I have some other tools that would be used in conjunction with Orcascript once I have it in stable form.

A lot of the potential users of such a tool would be coming from a Python/Ansible background and would be familiar with the workflow that I described above. And when you are dealing with task automation, using custom functions, etc, there are a lot of things that could fail, so seeing the stacktrace of what failed, and where, would be very useful in resolving any scripting related errors.

So, in the psuedo example I gave, a python error may look like this:

Traceback (most recent call last):
  File "test.py", line 2, in <module>
    with open("file.txt") as f:
IOError: [Errno 2] No such file or directory: 'file.txt'

And optionally, I could catch the error, and handle it accordingly.

I would like for my users to (optionally) check for an error in Orcascript (anko) in a similar way, and if the error exist, and isn't handled, provide some form of stacktrace / error message.

Hopefully that helps provide some context for my particular use case. Thanks for the fast response!

MichaelS11 commented 3 years ago

Error message is easy enough. Should be able to work that out with Anko. Plus Anko errors do have location information. They might have some kind of additional stack trace type information hidden in them but have not spend any time seeing if that is the case or not.

Also because you are using layers on top of layers of code, might need to build a stack trace. Maybe this might give you some ideas: https://dave.cheney.net/2016/06/12/stack-traces-and-the-errors-package

Additionally depending on what you are doing, Anko might not have what you need in its current state. There has been talk about updating Anko error handling ("stack trace") but have not seen anyone spending any time on it and I will not have time to spend on Anko coding in the foreseeable future.

Hope that helps guide you in the right direction.

dsikes commented 3 years ago

Ok, Thanks for your help! Much appreciated.