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

Rename ErrInternal to ErrFatal and use abort #810

Merged
merged 7 commits into from
Mar 6, 2023
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion api/browser_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ type BrowserContext interface {
ClearCookies()
ClearPermissions()
Close()
Cookies() []any // TODO: make it []Cookie later on
Cookies() ([]any, error) // TODO: make it []Cookie later on
ExposeBinding(name string, callback goja.Callable, opts goja.Value)
ExposeFunction(name string, callback goja.Callable)
GrantPermissions(permissions []string, opts goja.Value)
Expand Down
27 changes: 16 additions & 11 deletions browser/mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -578,13 +578,18 @@ func mapWorker(vu moduleVU, w api.Worker) mapping {
func mapBrowserContext(vu moduleVU, bc api.BrowserContext) mapping {
rt := vu.Runtime()
return mapping{
"addCookies": bc.AddCookies,
"addInitScript": bc.AddInitScript,
"browser": bc.Browser,
"clearCookies": bc.ClearCookies,
"clearPermissions": bc.ClearPermissions,
"close": bc.Close,
"cookies": bc.Cookies,
"addCookies": bc.AddCookies,
"addInitScript": bc.AddInitScript,
"browser": bc.Browser,
"clearCookies": bc.ClearCookies,
"clearPermissions": bc.ClearPermissions,
"close": bc.Close,
"cookies": func() ([]any, error) {
cc, err := bc.Cookies()
ctx := vu.Context()
panicIfFatalError(ctx, err)
return cc, err //nolint:wrapcheck
},
"exposeBinding": bc.ExposeBinding,
"exposeFunction": bc.ExposeFunction,
"grantPermissions": bc.GrantPermissions,
Expand All @@ -596,7 +601,7 @@ func mapBrowserContext(vu moduleVU, bc api.BrowserContext) mapping {
ctx := vu.Context()
return k6ext.Promise(ctx, func() (result any, reason error) {
err := bc.SetExtraHTTPHeaders(headers)
panicIfInternalError(ctx, err)
panicIfFatalError(ctx, err)
return nil, err //nolint:wrapcheck
})
},
Expand Down Expand Up @@ -678,8 +683,8 @@ 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())
func panicIfFatalError(ctx context.Context, err error) {
if errors.Is(err, k6error.ErrFatal) {
k6ext.Abort(ctx, err.Error())
}
}
7 changes: 3 additions & 4 deletions common/browser_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,8 @@ func (b *BrowserContext) Close() {
}

// Cookies is not implemented.
func (b *BrowserContext) Cookies() []any {
k6ext.Panic(b.ctx, "BrowserContext.cookies() has not been implemented yet")
return nil
func (b *BrowserContext) Cookies() ([]any, error) {
return nil, fmt.Errorf("BrowserContext.cookies() has not been implemented yet: %w", k6error.ErrFatal)
}

// ExposeBinding is not implemented.
Expand Down Expand Up @@ -272,7 +271,7 @@ func (b *BrowserContext) SetDefaultTimeout(timeout int64) {

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

// SetGeolocation overrides the geo location of the user.
Expand Down
12 changes: 8 additions & 4 deletions k6error/internal.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
// Package k6error contains ErrInternal.
// Package k6error contains ErrFatal.
package k6error

import (
"errors"
)

// ErrInternal should be wrapped into an error
// ErrFatal should be wrapped into an error
// to signal to the mapping layer that the error
// is an internal error and we should abort the test run.
var ErrInternal = errors.New("internal error")
// is a fatal error and we should abort the whole
// test run, not just the current iteration. It
// should be used in cases where if the iteration
// ran again then there's a 100% chance that it
// will end up running into the same error.
var ErrFatal = errors.New("fatal error")
24 changes: 21 additions & 3 deletions k6ext/panic.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,32 @@ import (
"strings"
"time"

"github.com/dop251/goja"

"go.k6.io/k6/errext"
k6common "go.k6.io/k6/js/common"
)

// Panic will cause a panic with the given error which will shut
// the application down. Before panicking, it will find the
// Abort will shutdown the whole test run. This should
// only be used from the goja mapping layer. It is only
// to be used when an error will occur in all iterations,
// so it's permanent.
func Abort(ctx context.Context, format string, a ...any) {
sharedPanic(ctx, func(rt *goja.Runtime, a ...any) {
reason := fmt.Errorf(format, a...).Error()
rt.Interrupt(&errext.InterruptError{Reason: reason})
}, a...)
ankur22 marked this conversation as resolved.
Show resolved Hide resolved
}

// Panic will cause a panic with the given error which will stop
// the current iteration. Before panicking, it will find the
// browser process from the context and kill it if it still exists.
// TODO: test.
func Panic(ctx context.Context, format string, a ...any) {
sharedPanic(ctx, func(rt *goja.Runtime, a ...any) { k6common.Throw(rt, fmt.Errorf(format, a...)) }, a...)
ankur22 marked this conversation as resolved.
Show resolved Hide resolved
}

func sharedPanic(ctx context.Context, fail func(rt *goja.Runtime, a ...any), a ...any) {
ankur22 marked this conversation as resolved.
Show resolved Hide resolved
rt := Runtime(ctx)
if rt == nil {
// this should never happen unless a programmer error
Expand All @@ -31,7 +49,7 @@ func Panic(ctx context.Context, format string, a ...any) {
a[len(a)-1] = &UserFriendlyError{Err: err}
}
}
defer k6common.Throw(rt, fmt.Errorf(format, a...))
defer fail(rt, a...)
ankur22 marked this conversation as resolved.
Show resolved Hide resolved

// TODO: Remove this after moving k6ext.Panic into the mapping layer.
pidder, ok := GetVU(ctx).(interface {
Expand Down