Skip to content

Commit

Permalink
Refactor reload to return an error
Browse files Browse the repository at this point in the history
  • Loading branch information
ankur22 committed Apr 22, 2024
1 parent ba771ec commit 2f1a141
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
12 changes: 9 additions & 3 deletions browser/mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -692,9 +692,15 @@ func mapPage(vu moduleVU, p *common.Page) mapping {
"pause": p.Pause,
"pdf": p.Pdf,
"press": p.Press,
"reload": func(opts goja.Value) *goja.Object {
r := mapResponse(vu, p.Reload(opts))
return rt.ToValue(r).ToObject(rt)
"reload": func(opts goja.Value) (*goja.Object, error) {
resp, err := p.Reload(opts)
if err != nil {
return nil, err //nolint:wrapcheck
}

r := mapResponse(vu, resp)

return rt.ToValue(r).ToObject(rt), nil
},
"route": p.Route,
"screenshot": func(opts goja.Value) (*goja.ArrayBuffer, error) {
Expand Down
12 changes: 6 additions & 6 deletions common/page.go
Original file line number Diff line number Diff line change
Expand Up @@ -1034,7 +1034,7 @@ func (p *Page) QueryAll(selector string) ([]*ElementHandle, error) {
}

// Reload will reload the current page.
func (p *Page) Reload(opts goja.Value) *Response { //nolint:funlen,cyclop
func (p *Page) Reload(opts goja.Value) (*Response, error) { //nolint:funlen,cyclop
p.logger.Debugf("Page:Reload", "sid:%v", p.sessionID())
_, span := TraceAPICall(p.ctx, p.targetID.String(), "page.reload")
defer span.End()
Expand All @@ -1044,7 +1044,7 @@ func (p *Page) Reload(opts goja.Value) *Response { //nolint:funlen,cyclop
p.timeoutSettings.navigationTimeout(),
)
if err := parsedOpts.Parse(p.ctx, opts); err != nil {
k6ext.Panic(p.ctx, "parsing reload options: %w", err)
return nil, fmt.Errorf("parsing reload options: %w", err)
}

timeoutCtx, timeoutCancelFn := context.WithTimeout(p.ctx, parsedOpts.Timeout)
Expand All @@ -1070,7 +1070,7 @@ func (p *Page) Reload(opts goja.Value) *Response { //nolint:funlen,cyclop

action := cdppage.Reload()
if err := action.Do(cdp.WithExecutor(p.ctx, p.session)); err != nil {
k6ext.Panic(p.ctx, "reloading page: %w", err)
return nil, fmt.Errorf("reloading page: %w", err)
}

wrapTimeoutError := func(err error) error {
Expand All @@ -1090,7 +1090,7 @@ func (p *Page) Reload(opts goja.Value) *Response { //nolint:funlen,cyclop
select {
case <-p.ctx.Done():
case <-timeoutCtx.Done():
k6ext.Panic(p.ctx, "%w", wrapTimeoutError(timeoutCtx.Err()))
return nil, wrapTimeoutError(timeoutCtx.Err())
case data := <-ch:
event = data.(*NavigationEvent)
}
Expand All @@ -1106,12 +1106,12 @@ func (p *Page) Reload(opts goja.Value) *Response { //nolint:funlen,cyclop
select {
case <-lifecycleEvtCh:
case <-timeoutCtx.Done():
k6ext.Panic(p.ctx, "%w", wrapTimeoutError(timeoutCtx.Err()))
return nil, wrapTimeoutError(timeoutCtx.Err())
}

applySlowMo(p.ctx)

return resp
return resp, nil
}

// Route is not implemented.
Expand Down

0 comments on commit 2f1a141

Please sign in to comment.