Skip to content

Commit ef77cad

Browse files
authored
Include compile error location in verbose error message format (#235)
1 parent abaa636 commit ef77cad

File tree

3 files changed

+37
-1
lines changed

3 files changed

+37
-1
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2020
- Support for compiling a context-dependent UnboundScript which can be run in any context of the isolate it was compiled in.
2121
- Support for creating a code cache from an UnboundScript which can be used to create an UnboundScript in other isolates
2222
to run a pre-compiled script in new contexts.
23+
- Included compile error location in `%+v` formatting of JSError
2324

2425
### Changed
2526
- Removed error return value from NewIsolate which never fails

errors.go

+8
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,15 @@ func (e *JSError) Format(s fmt.State, verb rune) {
4444
switch verb {
4545
case 'v':
4646
if s.Flag('+') && e.StackTrace != "" {
47+
// The StackTrace starts with the Message, so only the former needs to be printed
4748
io.WriteString(s, e.StackTrace)
49+
50+
// If it was a compile time error, then there wouldn't be a runtime stack trace,
51+
// but StackTrace will still include the Message, making them equal. In this case,
52+
// we want to include the Location where the compilation failed.
53+
if e.StackTrace == e.Message && e.Location != "" {
54+
fmt.Fprintf(s, " (at %s)", e.Location)
55+
}
4856
return
4957
}
5058
fallthrough

errors_test.go

+28-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
v8 "rogchap.com/v8go"
1212
)
1313

14-
func TestErrorFormatting(t *testing.T) {
14+
func TestJSErrorFormat(t *testing.T) {
1515
t.Parallel()
1616
tests := [...]struct {
1717
name string
@@ -90,3 +90,30 @@ func TestJSErrorOutput(t *testing.T) {
9090
t.Errorf("unexpected error stack trace: %q", e.StackTrace)
9191
}
9292
}
93+
94+
func TestJSErrorFormat_forSyntaxError(t *testing.T) {
95+
t.Parallel()
96+
iso := v8.NewIsolate()
97+
defer iso.Dispose()
98+
ctx := v8.NewContext(iso)
99+
defer ctx.Close()
100+
101+
script := `
102+
let x = 1;
103+
let y = x + ;
104+
let z = x + z;
105+
`
106+
_, err := ctx.RunScript(script, "xyz.js")
107+
jsErr := err.(*v8.JSError)
108+
if jsErr.StackTrace != jsErr.Message {
109+
t.Errorf("unexpected StackTrace %q not equal to Message %q", jsErr.StackTrace, jsErr.Message)
110+
}
111+
if jsErr.Location == "" {
112+
t.Errorf("missing Location")
113+
}
114+
115+
msg := fmt.Sprintf("%+v", err)
116+
if msg != "SyntaxError: Unexpected token ';' (at xyz.js:3:15)" {
117+
t.Errorf("unexpected verbose error message: %q", msg)
118+
}
119+
}

0 commit comments

Comments
 (0)