Skip to content

Commit

Permalink
_js: expose IsIncomplete, don't stringify on throw
Browse files Browse the repository at this point in the history
For example, ParseError contains more information than just the message,
and we want to have that available.

Errors from the JS layer shouldn't be the Go-style error interface. JS
already has an Error type, whith a message string and its own stack
trace. Use that, which is as simple as calling panic in Go.
  • Loading branch information
mvdan committed Feb 26, 2019
1 parent 1c6516f commit 7c8be39
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 5 deletions.
9 changes: 5 additions & 4 deletions _js/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func main() {
}
node, ok := v.(syntax.Node)
if !ok {
throw("NodeType requires a Node argument")
panic("NodeType requires a Node argument")
}
typ := fmt.Sprintf("%T", node)
if i := strings.LastIndexAny(typ, "*.]"); i >= 0 {
Expand All @@ -47,13 +47,14 @@ func main() {
}
return jp
})
stx.Set("IsIncomplete", syntax.IsIncomplete)

stx.Set("KeepComments", func(v interface{}) {
syntax.KeepComments(&v.(*jsParser).Parser)
})
stx.Set("Variant", func(l syntax.LangVariant) func(interface{}) {
if math.IsNaN(float64(l)) {
throw("Variant requires a LangVariant argument")
panic("Variant requires a LangVariant argument")
}
return func(v interface{}) {
syntax.Variant(l)(&v.(*jsParser).Parser)
Expand Down Expand Up @@ -94,8 +95,8 @@ func main() {
})
}

func throw(v interface{}) {
js.Global.Call("$throwRuntimeError", fmt.Sprint(v))
func throw(err error) {
js.Global.Call("$throw", js.MakeFullWrapper(err))
}

// streamReader is an io.Reader wrapper for Node's stream.Readable. See
Expand Down
27 changes: 26 additions & 1 deletion _js/testmain.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,37 @@ const printer = syntax.NewPrinter()
}

{
// parse errors
// fatal parse error
const src = "echo )"
try {
parser.Parse(src, "src")
assert.fail("did not error")
} catch (err) {
assert.equal(err.Filename, "src")
assert.equal(err.Pos.Line(), 1)
assert.equal(syntax.IsIncomplete(err), false)
}
}

{
// incomplete parse error
const src = "echo ${"
try {
parser.Parse(src, "src")
assert.fail("did not error")
} catch (err) {
assert.equal(syntax.IsIncomplete(err), true)
}
}

{
// js error from the wrapper layer
var foo = {}
try {
syntax.NodeType(foo)
assert.fail("did not error")
} catch (err) {
assert.equal(err.message.includes("requires a Node argument"), true)
}
}

Expand Down

0 comments on commit 7c8be39

Please sign in to comment.