Skip to content

Commit 2b13c5b

Browse files
authored
Merge branch 'main' into bugfix_recvcleanup
2 parents 3c6745f + 6af572f commit 2b13c5b

File tree

3 files changed

+25
-6
lines changed

3 files changed

+25
-6
lines changed

doc/manual.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -605,7 +605,10 @@ single operation. Currently the following steps are supported:
605605
1. `redactString`: a function that compiles and adds a
606606
redaction pattern that matches the given string
607607
literally.
608-
608+
609+
1. `Failure`: return an object representing a failure with the
610+
argument as the failure message.
611+
609612
1. `match`: [Sheen](https://github.com/Comcast/sheens)'s
610613
[pattern
611614
matching](https://github.com/Comcast/sheens#pattern-matching)

dsl/js.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ func JSExec(ctx *Ctx, src string, env map[string]interface{}) (interface{}, erro
3939
return x, nil
4040
}
4141

42+
func gojaPanic(vm *goja.Runtime, x interface{}) {
43+
panic(vm.ToValue(x))
44+
}
45+
4246
func jsExec(ctx *Ctx, src string, env map[string]interface{}) (interface{}, error) {
4347

4448
js := goja.New()
@@ -64,14 +68,14 @@ func jsExec(ctx *Ctx, src string, env map[string]interface{}) (interface{}, erro
6468

6569
js.Set("redactRegexp", func(pat string) {
6670
if err := ctx.AddRedaction(pat); err != nil {
67-
panic(err)
71+
gojaPanic(js, NewBroken(err))
6872
}
6973
})
7074

7175
js.Set("redactString", func(pat string) {
7276
pat = regexp.QuoteMeta(pat)
7377
if err := ctx.AddRedaction(pat); err != nil {
74-
panic(err)
78+
gojaPanic(js, NewBroken(err))
7579
}
7680
})
7781

@@ -81,9 +85,7 @@ func jsExec(ctx *Ctx, src string, env map[string]interface{}) (interface{}, erro
8185
}
8286
bss, err := match.Match(pat, msg, bs)
8387
if err != nil {
84-
// This panic is caught. (That's how we
85-
// return errors.)
86-
panic(js.ToValue(err.Error()))
88+
gojaPanic(js, NewBroken(err))
8789
}
8890
// Strip type (match.Bindings) to enable standard
8991
// Javascript access to the maps.

dsl/js_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,4 +92,18 @@ bss[0]["?x"] == "queso";
9292
}
9393
})
9494

95+
t.Run("nopanic", func(t *testing.T) {
96+
// ToDo: Iterate over all exposed native Go functions?
97+
98+
src := `redactRegexp("[}");`
99+
100+
_, err := JSExec(ctx, src, nil)
101+
if err == nil {
102+
t.Fatal("should have complained (politely)")
103+
}
104+
if _, is := IsBroken(err); !is {
105+
t.Fatalf("should have reported Broken instead of %T", err)
106+
}
107+
})
108+
95109
}

0 commit comments

Comments
 (0)