Skip to content

Commit 17169dc

Browse files
authored
chore: simplify bindingCall error stack and refac some tests (#474)
1 parent 69b51fc commit 17169dc

7 files changed

+114
-90
lines changed

binding_call.go

+23
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
package playwright
22

3+
import (
4+
"fmt"
5+
"strings"
6+
7+
"github.com/go-stack/stack"
8+
)
9+
310
type BindingCall interface {
411
Call(f BindingCallFunction)
512
}
@@ -57,6 +64,22 @@ func (b *bindingCallImpl) Call(f BindingCallFunction) {
5764
}
5865
}
5966

67+
func serializeError(err error) map[string]interface{} {
68+
st := stack.Trace().TrimRuntime()
69+
if len(st) == 0 { // https://github.com/go-stack/stack/issues/27
70+
st = stack.Trace()
71+
}
72+
return map[string]interface{}{
73+
"error": &Error{
74+
Name: "Playwright for Go Error",
75+
Message: err.Error(),
76+
Stack: strings.ReplaceAll(strings.TrimFunc(fmt.Sprintf("%+v", st), func(r rune) bool {
77+
return r == '[' || r == ']'
78+
}), " ", "\n"),
79+
},
80+
}
81+
}
82+
6083
func newBindingCall(parent *channelOwner, objectType string, guid string, initializer map[string]interface{}) *bindingCallImpl {
6184
bt := &bindingCallImpl{}
6285
bt.createChannelOwner(bt, parent, objectType, guid, initializer)

js_handle.go

-13
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ import (
77
"math/big"
88
"net/url"
99
"reflect"
10-
"runtime/debug"
11-
"strings"
1210
"time"
1311
)
1412

@@ -308,17 +306,6 @@ func serializeArgument(arg interface{}) interface{} {
308306
}
309307
}
310308

311-
func serializeError(err error) map[string]interface{} {
312-
stack := strings.Split(string(debug.Stack()), "\n")
313-
return map[string]interface{}{
314-
"error": &Error{
315-
Name: "Playwright for Go Error",
316-
Message: err.Error(),
317-
Stack: strings.Join(stack[:len(stack)-5], "\n"),
318-
},
319-
}
320-
}
321-
322309
func newJSHandle(parent *channelOwner, objectType string, guid string, initializer map[string]interface{}) *jsHandleImpl {
323310
bt := &jsHandleImpl{
324311
preview: initializer["preview"].(string),

tests/binding_test.go

+22-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ func TestBrowserContextExposeBindingPanic(t *testing.T) {
7878
innerError := result.(map[string]interface{})
7979
require.Equal(t, innerError["message"], "WOOF WOOF")
8080
stack := strings.Split(innerError["stack"].(string), "\n")
81-
require.Contains(t, stack[len(stack)-1], "binding_test.go")
81+
require.Contains(t, stack[3], "binding_test.go")
8282
}
8383

8484
func TestBrowserContextExposeBindingHandleShouldWork(t *testing.T) {
@@ -102,3 +102,24 @@ func TestBrowserContextExposeBindingHandleShouldWork(t *testing.T) {
102102
require.NoError(t, err)
103103
require.Equal(t, 42, res)
104104
}
105+
106+
func TestPageExposeBindingPanic(t *testing.T) {
107+
BeforeEach(t)
108+
109+
err := page.ExposeBinding("woof", func(source *playwright.BindingSource, args ...interface{}) interface{} {
110+
panic(errors.New("WOOF WOOF"))
111+
})
112+
require.NoError(t, err)
113+
result, err := page.Evaluate(`async () => {
114+
try {
115+
await window['woof']();
116+
} catch (e) {
117+
return {message: e.message, stack: e.stack};
118+
}
119+
}`)
120+
require.NoError(t, err)
121+
innerError := result.(map[string]interface{})
122+
require.Equal(t, innerError["message"], "WOOF WOOF")
123+
stack := strings.Split(innerError["stack"].(string), "\n")
124+
require.Contains(t, stack[3], "binding_test.go")
125+
}

tests/browser_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func TestBrowserNewContext(t *testing.T) {
3737
}
3838

3939
func TestBrowserNewContextWithExtraHTTPHeaders(t *testing.T) {
40-
context, page = newBrowserContextAndPage(t, playwright.BrowserNewContextOptions{
40+
BeforeEach(t, playwright.BrowserNewContextOptions{
4141
ExtraHttpHeaders: map[string]string{"extra-http": "42"},
4242
})
4343

0 commit comments

Comments
 (0)