diff options
author | Felix Lange <fjl@twurst.com> | 2016-04-21 05:17:00 +0800 |
---|---|---|
committer | Felix Lange <fjl@twurst.com> | 2016-04-21 05:33:43 +0800 |
commit | 87ae0df476cf6b413795ee54207e8ec86e178dbc (patch) | |
tree | 0a3a42c72845507ce40ff8e6767188d9809fa361 /cmd/geth/js.go | |
parent | a6ca8fd26884807c80b649bd2a0e780aa93ced22 (diff) | |
download | dexon-87ae0df476cf6b413795ee54207e8ec86e178dbc.tar dexon-87ae0df476cf6b413795ee54207e8ec86e178dbc.tar.gz dexon-87ae0df476cf6b413795ee54207e8ec86e178dbc.tar.bz2 dexon-87ae0df476cf6b413795ee54207e8ec86e178dbc.tar.lz dexon-87ae0df476cf6b413795ee54207e8ec86e178dbc.tar.xz dexon-87ae0df476cf6b413795ee54207e8ec86e178dbc.tar.zst dexon-87ae0df476cf6b413795ee54207e8ec86e178dbc.zip |
cmd/geth, jsre: improve the js command
geth js stopped the JS runtime after running the first input file
and blocked for pending callbacks. This commit makes it process
all files and enables quitting with Ctrl-C regardless of callbacks.
Error reporting is also improved. If a script fails to load, the error
is printed and includes the backtrace. package jsre now ensures that
otto is aware of the filename, the backtrace will contain them.
Before:
$ geth js bad.js; echo "exit $?"
... log messages ...
exit 0
After:
$ geth js bad.js; echo "exit $?"
... log messages ...
Fatal: JavaScript Error: Invalid number of input parameters
at web3.js:3109:20
at web3.js:4917:15
at web3.js:4960:5
at web3.js:4984:23
at checkWork (bad.js:11:9)
at bad.js:19:1
exit 1
Diffstat (limited to 'cmd/geth/js.go')
-rw-r--r-- | cmd/geth/js.go | 16 |
1 files changed, 7 insertions, 9 deletions
diff --git a/cmd/geth/js.go b/cmd/geth/js.go index 767b513c1..2b64303b2 100644 --- a/cmd/geth/js.go +++ b/cmd/geth/js.go @@ -123,7 +123,7 @@ func (self *jsre) batch(statement string) { err := self.re.EvalAndPrettyPrint(statement) if err != nil { - fmt.Printf("error: %v", err) + fmt.Printf("%v", jsErrorString(err)) } if self.atexit != nil { @@ -301,21 +301,19 @@ func (self *jsre) preloadJSFiles(ctx *cli.Context) error { for _, file := range jsFiles { filename := common.AbsolutePath(assetPath, strings.TrimSpace(file)) if err := self.re.Exec(filename); err != nil { - return fmt.Errorf("%s: %v", file, err) + return fmt.Errorf("%s: %v", file, jsErrorString(err)) } } } return nil } -// exec executes the JS file with the given filename and stops the JSRE -func (self *jsre) exec(filename string) error { - if err := self.re.Exec(filename); err != nil { - self.re.Stop(false) - return fmt.Errorf("Javascript Error: %v", err) +// jsErrorString adds a backtrace to errors generated by otto. +func jsErrorString(err error) string { + if ottoErr, ok := err.(*otto.Error); ok { + return ottoErr.String() } - self.re.Stop(true) - return nil + return err.Error() } func (self *jsre) interactive() { |