Skip to content

Commit

Permalink
Include compile error location in verbose error message format (rogch…
Browse files Browse the repository at this point in the history
  • Loading branch information
dylanahsmith authored Nov 19, 2021
1 parent abaa636 commit ef77cad
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Support for compiling a context-dependent UnboundScript which can be run in any context of the isolate it was compiled in.
- Support for creating a code cache from an UnboundScript which can be used to create an UnboundScript in other isolates
to run a pre-compiled script in new contexts.
- Included compile error location in `%+v` formatting of JSError

### Changed
- Removed error return value from NewIsolate which never fails
Expand Down
8 changes: 8 additions & 0 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,15 @@ func (e *JSError) Format(s fmt.State, verb rune) {
switch verb {
case 'v':
if s.Flag('+') && e.StackTrace != "" {
// The StackTrace starts with the Message, so only the former needs to be printed
io.WriteString(s, e.StackTrace)

// If it was a compile time error, then there wouldn't be a runtime stack trace,
// but StackTrace will still include the Message, making them equal. In this case,
// we want to include the Location where the compilation failed.
if e.StackTrace == e.Message && e.Location != "" {
fmt.Fprintf(s, " (at %s)", e.Location)
}
return
}
fallthrough
Expand Down
29 changes: 28 additions & 1 deletion errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
v8 "rogchap.com/v8go"
)

func TestErrorFormatting(t *testing.T) {
func TestJSErrorFormat(t *testing.T) {
t.Parallel()
tests := [...]struct {
name string
Expand Down Expand Up @@ -90,3 +90,30 @@ func TestJSErrorOutput(t *testing.T) {
t.Errorf("unexpected error stack trace: %q", e.StackTrace)
}
}

func TestJSErrorFormat_forSyntaxError(t *testing.T) {
t.Parallel()
iso := v8.NewIsolate()
defer iso.Dispose()
ctx := v8.NewContext(iso)
defer ctx.Close()

script := `
let x = 1;
let y = x + ;
let z = x + z;
`
_, err := ctx.RunScript(script, "xyz.js")
jsErr := err.(*v8.JSError)
if jsErr.StackTrace != jsErr.Message {
t.Errorf("unexpected StackTrace %q not equal to Message %q", jsErr.StackTrace, jsErr.Message)
}
if jsErr.Location == "" {
t.Errorf("missing Location")
}

msg := fmt.Sprintf("%+v", err)
if msg != "SyntaxError: Unexpected token ';' (at xyz.js:3:15)" {
t.Errorf("unexpected verbose error message: %q", msg)
}
}

0 comments on commit ef77cad

Please sign in to comment.