-
Notifications
You must be signed in to change notification settings - Fork 642
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
Consistent, structured returned error value #46
Labels
Comments
mholt
changed the title
Consistent, structured error type
Consistent, structured returned error value
Sep 4, 2015
I'm glad to hear that. |
yuin
added a commit
that referenced
this issue
Sep 5, 2015
yuin
added a commit
that referenced
this issue
Sep 5, 2015
I've added a
You will want to write utilities like the following: package main
import (
"github.com/yuin/gopher-lua"
"github.com/yuin/gopher-lua/parse"
)
func myDoString(L *lua.LState, source string) error {
fn, err := L.LoadString(source)
if err != nil {
if lerr := err.(*lua.ApiError); lerr != nil {
switch cause := lerr.Cause.(type) {
case *parse.Error:
println("Syntax error:", cause.Error())
println("Line: ", cause.Pos.Line)
println("Column: ", cause.Pos.Column)
case *lua.CompileError:
println("Compile error:", cause.Error())
println("Line: ", cause.Line)
default:
println(lerr.Error())
}
}
return err
}
L.Push(fn)
return L.PCall(0, lua.MultRet, L.NewFunction(func(L *lua.LState) int {
obj := L.Get(1)
dbg, _ := L.GetStack(1)
L.GetInfo("Slunf", dbg, lua.LNil)
println("Runtime error:", obj.String())
println("Line:", dbg.CurrentLine)
return 1 // retuens the original object
}))
}
func main() {
L := lua.NewState()
myDoString(L, `
print(1)
print(2)
pint(3)
print(4)
print(5)
`)
} |
This is great! Thank you! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi. Really loving gopher-lua so far and excited to keep using it! Thanks for making it.
I'm struggling with error handling at the moment. Basically, I'd like to do my own error reporting. The
error
returned from the Do functions could be a concrete type that has structured access to the line and column number and error message.If you're too busy, I'm willing to make the changes but am not sure the best way to go about it. The DoString function could still return
error
type, but the underlying type (after a type assertion) would ideally expose the line, column numbers as well as the error message and maybe error type (syntax, etc.). Right now the error is like this:But I'd like to be able to do this:
What do you think?
The text was updated successfully, but these errors were encountered: