From 81f16ce89fa31bebfbba4c8569952a07bbbec5b5 Mon Sep 17 00:00:00 2001 From: Jamie Stephens Date: Fri, 9 Jul 2021 18:43:53 +0000 Subject: [PATCH 1/3] Report broken test instead of panicking; closes #106 --- dsl/js.go | 12 +++++++----- dsl/js_test.go | 14 ++++++++++++++ 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/dsl/js.go b/dsl/js.go index 9f30f07..6073950 100644 --- a/dsl/js.go +++ b/dsl/js.go @@ -39,6 +39,10 @@ func JSExec(ctx *Ctx, src string, env map[string]interface{}) (interface{}, erro return x, nil } +func gojaPanic(vm *goja.Runtime, x interface{}) { + panic(vm.ToValue(x)) +} + func jsExec(ctx *Ctx, src string, env map[string]interface{}) (interface{}, error) { js := goja.New() @@ -64,14 +68,14 @@ func jsExec(ctx *Ctx, src string, env map[string]interface{}) (interface{}, erro js.Set("redactRegexp", func(pat string) { if err := ctx.AddRedaction(pat); err != nil { - panic(err) + gojaPanic(js, Failure(err.Error())) } }) js.Set("redactString", func(pat string) { pat = regexp.QuoteMeta(pat) if err := ctx.AddRedaction(pat); err != nil { - panic(err) + gojaPanic(js, Failure(err.Error())) } }) @@ -81,9 +85,7 @@ func jsExec(ctx *Ctx, src string, env map[string]interface{}) (interface{}, erro } bss, err := match.Match(pat, msg, bs) if err != nil { - // This panic is caught. (That's how we - // return errors.) - panic(js.ToValue(err.Error())) + gojaPanic(js, Failure(err.Error())) } // Strip type (match.Bindings) to enable standard // Javascript access to the maps. diff --git a/dsl/js_test.go b/dsl/js_test.go index c40f169..9c7d344 100644 --- a/dsl/js_test.go +++ b/dsl/js_test.go @@ -92,4 +92,18 @@ bss[0]["?x"] == "queso"; } }) + t.Run("nopanic", func(t *testing.T) { + // ToDo: Iterate over all exposed native Go functions? + + src := `redactRegexp("[}");` + + _, err := JSExec(ctx, src, nil) + if err == nil { + t.Fatal("should have complained (politely)") + } + if _, is := IsBroken(err); !is { + t.Fatalf("should have reported Broken instead of %T", err) + } + }) + } From fb7cac084f7cdc7ce39aa541f1ca039ae2c30196 Mon Sep 17 00:00:00 2001 From: Jamie Stephens Date: Fri, 9 Jul 2021 18:48:57 +0000 Subject: [PATCH 2/3] Document Failure() --- doc/manual.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/doc/manual.md b/doc/manual.md index f651ce2..b48f6ee 100644 --- a/doc/manual.md +++ b/doc/manual.md @@ -605,7 +605,10 @@ single operation. Currently the following steps are supported: 1. `redactString`: a function that compiles and adds a redaction pattern that matches the given string literally. - + + 1. `Failure`: return an object representing a failure with the + argument as the failure message. + 1. `match`: [Sheen](https://github.com/Comcast/sheens)'s [pattern matching](https://github.com/Comcast/sheens#pattern-matching) From 9957618ec8e12350e173638026960c34e8b9e48a Mon Sep 17 00:00:00 2001 From: Jamie Stephens Date: Fri, 9 Jul 2021 19:03:50 +0000 Subject: [PATCH 3/3] Broken, not Failure --- dsl/js.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/dsl/js.go b/dsl/js.go index 6073950..34f0dbd 100644 --- a/dsl/js.go +++ b/dsl/js.go @@ -68,14 +68,14 @@ func jsExec(ctx *Ctx, src string, env map[string]interface{}) (interface{}, erro js.Set("redactRegexp", func(pat string) { if err := ctx.AddRedaction(pat); err != nil { - gojaPanic(js, Failure(err.Error())) + gojaPanic(js, NewBroken(err)) } }) js.Set("redactString", func(pat string) { pat = regexp.QuoteMeta(pat) if err := ctx.AddRedaction(pat); err != nil { - gojaPanic(js, Failure(err.Error())) + gojaPanic(js, NewBroken(err)) } }) @@ -85,7 +85,7 @@ func jsExec(ctx *Ctx, src string, env map[string]interface{}) (interface{}, erro } bss, err := match.Match(pat, msg, bs) if err != nil { - gojaPanic(js, Failure(err.Error())) + gojaPanic(js, NewBroken(err)) } // Strip type (match.Bindings) to enable standard // Javascript access to the maps.