Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
borgerli opened this issue May 18, 2020 · 7 comments
Closed

Comments

@borgerli
Copy link

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
Copy link
Contributor

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

@borgerli
Copy link
Author

Thanks, Michael @MichaelS11

Anko is really cool for Go programmers

@dsikes
Copy link

dsikes commented Sep 2, 2020

@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
Copy link
Contributor

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
Copy link

dsikes commented Sep 2, 2020

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
Copy link
Contributor

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
Copy link

dsikes commented Sep 2, 2020

Ok, Thanks for your help! Much appreciated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants