Skip to content

Commit 14ae570

Browse files
committed
console, internal/jsre: colorize JavaScript exceptions too
1 parent ffaf58f commit 14ae570

4 files changed

Lines changed: 37 additions & 12 deletions

File tree

‎console/console.go‎

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,11 @@ func (c *Console) init(preload []string) error {
182182
// Preload any JavaScript files before starting the console
183183
for _, path := range preload {
184184
if err := c.jsre.Exec(path); err != nil {
185-
return fmt.Errorf("%s: %v", path, jsErrorString(err))
185+
failure := err.Error()
186+
if ottoErr, ok := err.(*otto.Error); ok {
187+
failure = ottoErr.String()
188+
}
189+
return fmt.Errorf("%s: %v", path, failure)
186190
}
187191
}
188192
// Configure the console's input prompter for scrollback and tab completion
@@ -269,7 +273,6 @@ func (c *Console) Evaluate(statement string) error {
269273
}
270274
}()
271275
if err := c.jsre.Evaluate(statement, c.printer); err != nil {
272-
fmt.Fprintf(c.printer, "%v\n", jsErrorString(err))
273276
return err
274277
}
275278
return nil
@@ -359,11 +362,3 @@ func (c *Console) Stop(graceful bool) error {
359362
c.jsre.Stop(graceful)
360363
return nil
361364
}
362-
363-
// jsErrorString adds a backtrace to errors generated by otto.
364-
func jsErrorString(err error) string {
365-
if ottoErr, ok := err.(*otto.Error); ok {
366-
return ottoErr.String()
367-
}
368-
return err.Error()
369-
}

‎console/console_test.go‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,3 +281,15 @@ func TestPrettyPrint(t *testing.T) {
281281
t.Fatalf("pretty print mismatch: have %s, want %s", output, want)
282282
}
283283
}
284+
285+
// Tests that the JavaScript exceptions are properly formatted and colored.
286+
func TestPrettyError(t *testing.T) {
287+
tester := newTester(t, nil)
288+
defer tester.Close(t)
289+
tester.console.Evaluate("throw 'hello'")
290+
291+
want := jsre.ErrorColor("hello") + "\n"
292+
if output := string(tester.output.Bytes()); output != want {
293+
t.Fatalf("pretty error mismatch: have %s, want %s", output, want)
294+
}
295+
}

‎internal/jsre/jsre.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -303,11 +303,11 @@ func (self *JSRE) Evaluate(code string, w io.Writer) error {
303303
self.Do(func(vm *otto.Otto) {
304304
val, err := vm.Run(code)
305305
if err != nil {
306-
fail = err
306+
prettyError(vm, err, w)
307307
} else {
308308
prettyPrint(vm, val, w)
309-
fmt.Fprintln(w)
310309
}
310+
fmt.Fprintln(w)
311311
})
312312
return fail
313313
}

‎internal/jsre/pretty.go‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ var (
3737
SpecialColor = color.New(color.Bold).SprintfFunc()
3838
NumberColor = color.New(color.FgRed).SprintfFunc()
3939
StringColor = color.New(color.FgGreen).SprintfFunc()
40+
ErrorColor = color.New(color.FgHiRed).SprintfFunc()
4041
)
4142

4243
// these fields are hidden when printing objects.
@@ -55,6 +56,23 @@ func prettyPrint(vm *otto.Otto, value otto.Value, w io.Writer) {
5556
ppctx{vm: vm, w: w}.printValue(value, 0, false)
5657
}
5758

59+
// prettyError writes err to standard output.
60+
func prettyError(vm *otto.Otto, err error, w io.Writer) {
61+
failure := err.Error()
62+
if ottoErr, ok := err.(*otto.Error); ok {
63+
failure = ottoErr.String()
64+
}
65+
fmt.Fprint(w, ErrorColor("%s", failure))
66+
}
67+
68+
// jsErrorString adds a backtrace to errors generated by otto.
69+
func jsErrorString(err error) string {
70+
if ottoErr, ok := err.(*otto.Error); ok {
71+
return ottoErr.String()
72+
}
73+
return err.Error()
74+
}
75+
5876
func prettyPrintJS(call otto.FunctionCall, w io.Writer) otto.Value {
5977
for _, v := range call.ArgumentList {
6078
prettyPrint(call.Otto, v, w)

0 commit comments

Comments
 (0)