Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: simplify bindingCall error stack and refac some tests #474

Merged
merged 2 commits into from
Jul 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions binding_call.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
package playwright

import (
"fmt"
"strings"

"github.com/go-stack/stack"
)

type BindingCall interface {
Call(f BindingCallFunction)
}
Expand Down Expand Up @@ -57,6 +64,22 @@ func (b *bindingCallImpl) Call(f BindingCallFunction) {
}
}

func serializeError(err error) map[string]interface{} {
st := stack.Trace().TrimRuntime()
if len(st) == 0 { // https://github.com/go-stack/stack/issues/27
st = stack.Trace()
}
return map[string]interface{}{
"error": &Error{
Name: "Playwright for Go Error",
Message: err.Error(),
Stack: strings.ReplaceAll(strings.TrimFunc(fmt.Sprintf("%+v", st), func(r rune) bool {
return r == '[' || r == ']'
}), " ", "\n"),
},
}
}

func newBindingCall(parent *channelOwner, objectType string, guid string, initializer map[string]interface{}) *bindingCallImpl {
bt := &bindingCallImpl{}
bt.createChannelOwner(bt, parent, objectType, guid, initializer)
Expand Down
13 changes: 0 additions & 13 deletions js_handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import (
"math/big"
"net/url"
"reflect"
"runtime/debug"
"strings"
"time"
)

Expand Down Expand Up @@ -308,17 +306,6 @@ func serializeArgument(arg interface{}) interface{} {
}
}

func serializeError(err error) map[string]interface{} {
stack := strings.Split(string(debug.Stack()), "\n")
return map[string]interface{}{
"error": &Error{
Name: "Playwright for Go Error",
Message: err.Error(),
Stack: strings.Join(stack[:len(stack)-5], "\n"),
},
}
}

func newJSHandle(parent *channelOwner, objectType string, guid string, initializer map[string]interface{}) *jsHandleImpl {
bt := &jsHandleImpl{
preview: initializer["preview"].(string),
Expand Down
23 changes: 22 additions & 1 deletion tests/binding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func TestBrowserContextExposeBindingPanic(t *testing.T) {
innerError := result.(map[string]interface{})
require.Equal(t, innerError["message"], "WOOF WOOF")
stack := strings.Split(innerError["stack"].(string), "\n")
require.Contains(t, stack[len(stack)-1], "binding_test.go")
require.Contains(t, stack[3], "binding_test.go")
}

func TestBrowserContextExposeBindingHandleShouldWork(t *testing.T) {
Expand All @@ -102,3 +102,24 @@ func TestBrowserContextExposeBindingHandleShouldWork(t *testing.T) {
require.NoError(t, err)
require.Equal(t, 42, res)
}

func TestPageExposeBindingPanic(t *testing.T) {
BeforeEach(t)

err := page.ExposeBinding("woof", func(source *playwright.BindingSource, args ...interface{}) interface{} {
panic(errors.New("WOOF WOOF"))
})
require.NoError(t, err)
result, err := page.Evaluate(`async () => {
try {
await window['woof']();
} catch (e) {
return {message: e.message, stack: e.stack};
}
}`)
require.NoError(t, err)
innerError := result.(map[string]interface{})
require.Equal(t, innerError["message"], "WOOF WOOF")
stack := strings.Split(innerError["stack"].(string), "\n")
require.Contains(t, stack[3], "binding_test.go")
}
2 changes: 1 addition & 1 deletion tests/browser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func TestBrowserNewContext(t *testing.T) {
}

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

Expand Down
Loading
Loading