I am using go language for a project. I need run a piece of js code in go. I know there is an package which is otto. My problem is that how I get the detail error message for the js code. For example:
src :=`
abc = 2 +
console.log("The value of abc is " + abc)
`
and when I do something let's say compile(src)
.
Then I will get the error that: Miss something at second line and miss ';' at third line.
just like a compiler doing
I already try using the compile of otto before I post the problem, the returned error is nil. Using func (self Otto) Run(src interface{}) (Value, error)
of otto for above code will return error, but if the code became
abc = 9
abc = 2 +
console.log("The value of abc is " + abc)
for both func (self Otto) Run(src interface{}) (Value, error)
and func (self *Otto) Compile(filename string, src interface{}) (*Script, error)
the error are nil
I'm going to ding you for insufficient research. A simple Google of "golang otto" brought up the Github page. With a pretty decent README.markdown for documentation. Read it.
Then you find out that Otto has
func (self *Otto) Compile(filename string, src interface{}) (*Script, error)
That is as good as it gets. Run that, see what you get in the returned error value.
type Error struct {
}
An Error represents a runtime error, e.g. a TypeError, a ReferenceError, etc.
func (err Error) Error() string
Error returns a description of the error
func (err Error) String() string
String returns a description of the error and a trace of where the error occurred.