Skip to content

Commit

Permalink
Refactor addCookies to catch ErrInteral
Browse files Browse the repository at this point in the history
This refactoring will catch ErrInternal errors and panic if found.
  • Loading branch information
ankur22 committed Feb 27, 2023
1 parent 2954828 commit ee45317
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 10 deletions.
2 changes: 1 addition & 1 deletion api/browser_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type BrowserContext interface {
Route(url goja.Value, handler goja.Callable)
SetDefaultNavigationTimeout(timeout int64)
SetDefaultTimeout(timeout int64)
SetExtraHTTPHeaders(headers map[string]string)
SetExtraHTTPHeaders(headers map[string]string) error
SetGeolocation(geolocation goja.Value)
// SetHTTPCredentials sets username/password credentials to use for HTTP authentication.
//
Expand Down
30 changes: 23 additions & 7 deletions browser/mapping.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package browser

import (
"context"
"errors"
"fmt"

"github.com/dop251/goja"

"github.com/grafana/xk6-browser/api"
"github.com/grafana/xk6-browser/chromium"
"github.com/grafana/xk6-browser/k6error"
"github.com/grafana/xk6-browser/k6ext"

k6common "go.k6.io/k6/js/common"
Expand Down Expand Up @@ -589,13 +592,20 @@ func mapBrowserContext(vu moduleVU, bc api.BrowserContext) mapping {
"route": bc.Route,
"setDefaultNavigationTimeout": bc.SetDefaultNavigationTimeout,
"setDefaultTimeout": bc.SetDefaultTimeout,
"setExtraHTTPHeaders": bc.SetExtraHTTPHeaders,
"setGeolocation": bc.SetGeolocation,
"setHTTPCredentials": bc.SetHTTPCredentials, //nolint:staticcheck
"setOffline": bc.SetOffline,
"storageState": bc.StorageState,
"unroute": bc.Unroute,
"waitForEvent": bc.WaitForEvent,
"setExtraHTTPHeaders": func(headers map[string]string) *goja.Promise {
ctx := vu.Context()
return k6ext.Promise(ctx, func() (result any, reason error) {
err := bc.SetExtraHTTPHeaders(headers)
panicIfInternalError(ctx, err)
return nil, err //nolint:wrapcheck
})
},
"setGeolocation": bc.SetGeolocation,
"setHTTPCredentials": bc.SetHTTPCredentials, //nolint:staticcheck
"setOffline": bc.SetOffline,
"storageState": bc.StorageState,
"unroute": bc.Unroute,
"waitForEvent": bc.WaitForEvent,
"pages": func() *goja.Object {
var (
mpages []mapping
Expand Down Expand Up @@ -663,3 +673,9 @@ func mapBrowserType(vu moduleVU, bt api.BrowserType) mapping {
},
}
}

func panicIfInternalError(ctx context.Context, err error) {
if errors.Is(err, k6error.ErrInternal) {
k6ext.Panic(ctx, err.Error())
}
}
5 changes: 3 additions & 2 deletions common/browser_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"time"

"github.com/grafana/xk6-browser/api"
"github.com/grafana/xk6-browser/k6error"
"github.com/grafana/xk6-browser/k6ext"
"github.com/grafana/xk6-browser/log"

Expand Down Expand Up @@ -270,8 +271,8 @@ func (b *BrowserContext) SetDefaultTimeout(timeout int64) {
}

// SetExtraHTTPHeaders is not implemented.
func (b *BrowserContext) SetExtraHTTPHeaders(headers map[string]string) {
k6ext.Panic(b.ctx, "BrowserContext.setExtraHTTPHeaders(headers) has not been implemented yet")
func (b *BrowserContext) SetExtraHTTPHeaders(headers map[string]string) error {
return fmt.Errorf("BrowserContext.setExtraHTTPHeaders(headers) has not been implemented yet: %w", k6error.ErrInternal)
}

// SetGeolocation overrides the geo location of the user.
Expand Down

0 comments on commit ee45317

Please sign in to comment.