Skip to content

Commit

Permalink
Add error handling for Dispose
Browse files Browse the repository at this point in the history
Dispose can panic when a navigation occurs on a page with an iframe.
The specific error that is returned is "Cannot find context with
specified id". The reason for this error is due to the fact that Chrome
has already cleaned up the associated resource before it receives the
CDP command to release it. Therefore it is safe to handle this error,
log a debug log and carry on with the test.
  • Loading branch information
ankur22 committed Apr 29, 2024
1 parent 50a3777 commit 7e42ae9
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions common/js_handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
"strings"

"github.com/grafana/xk6-browser/k6ext"
"github.com/grafana/xk6-browser/log"
Expand Down Expand Up @@ -88,6 +89,14 @@ func (h *BaseJSHandle) Dispose() {
h.logger.Debugf("BaseJSHandle:Dispose", "%v", err)
return
}
// The following error indicates that the object we're trying to release
// cannot be found, which would mean that the object has already been
// removed/deleted. This can occur when a navigation occurs, usually when
// a page contains an iframe.
if strings.Contains(err.Error(), "Cannot find context with specified id") {
h.logger.Debugf("BaseJSHandle:Dispose", "%v", err)
return
}

k6ext.Panic(h.ctx, "dispose: %w", err)
}
Expand Down

0 comments on commit 7e42ae9

Please sign in to comment.