From 7c8be39b438aa713f4d791826b6abfe7949d4d86 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Mart=C3=AD?= Date: Tue, 26 Feb 2019 12:25:25 +0100 Subject: [PATCH] _js: expose IsIncomplete, don't stringify on throw 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. --- _js/main.go | 9 +++++---- _js/testmain.js | 27 ++++++++++++++++++++++++++- 2 files changed, 31 insertions(+), 5 deletions(-) diff --git a/_js/main.go b/_js/main.go index 5ea891d9a..13070361a 100644 --- a/_js/main.go +++ b/_js/main.go @@ -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 { @@ -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) @@ -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 diff --git a/_js/testmain.js b/_js/testmain.js index 73088642d..450f312d6 100644 --- a/_js/testmain.js +++ b/_js/testmain.js @@ -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) } }