console, internal/jsre: colorize JavaScript exceptions too
This commit is contained in:
@ -182,7 +182,11 @@ func (c *Console) init(preload []string) error {
|
||||
// Preload any JavaScript files before starting the console
|
||||
for _, path := range preload {
|
||||
if err := c.jsre.Exec(path); err != nil {
|
||||
return fmt.Errorf("%s: %v", path, jsErrorString(err))
|
||||
failure := err.Error()
|
||||
if ottoErr, ok := err.(*otto.Error); ok {
|
||||
failure = ottoErr.String()
|
||||
}
|
||||
return fmt.Errorf("%s: %v", path, failure)
|
||||
}
|
||||
}
|
||||
// Configure the console's input prompter for scrollback and tab completion
|
||||
@ -269,7 +273,6 @@ func (c *Console) Evaluate(statement string) error {
|
||||
}
|
||||
}()
|
||||
if err := c.jsre.Evaluate(statement, c.printer); err != nil {
|
||||
fmt.Fprintf(c.printer, "%v\n", jsErrorString(err))
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
@ -359,11 +362,3 @@ func (c *Console) Stop(graceful bool) error {
|
||||
c.jsre.Stop(graceful)
|
||||
return nil
|
||||
}
|
||||
|
||||
// jsErrorString adds a backtrace to errors generated by otto.
|
||||
func jsErrorString(err error) string {
|
||||
if ottoErr, ok := err.(*otto.Error); ok {
|
||||
return ottoErr.String()
|
||||
}
|
||||
return err.Error()
|
||||
}
|
||||
|
@ -281,3 +281,15 @@ func TestPrettyPrint(t *testing.T) {
|
||||
t.Fatalf("pretty print mismatch: have %s, want %s", output, want)
|
||||
}
|
||||
}
|
||||
|
||||
// Tests that the JavaScript exceptions are properly formatted and colored.
|
||||
func TestPrettyError(t *testing.T) {
|
||||
tester := newTester(t, nil)
|
||||
defer tester.Close(t)
|
||||
tester.console.Evaluate("throw 'hello'")
|
||||
|
||||
want := jsre.ErrorColor("hello") + "\n"
|
||||
if output := string(tester.output.Bytes()); output != want {
|
||||
t.Fatalf("pretty error mismatch: have %s, want %s", output, want)
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user