Skip to content

Commit

Permalink
Apply suggestions from code review
Browse files Browse the repository at this point in the history
Cleanup `failFunc` to make it easier to read.

Co-authored-by: ka3de <daniel.jimenez@grafana.com>
  • Loading branch information
ankur22 and ka3de committed Mar 6, 2023
1 parent 7c4f55c commit 48ee41a
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions k6ext/panic.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,25 @@ import (
// 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) {
failFunc := func(rt *goja.Runtime, a ...any) {
reason := fmt.Errorf(format, a...).Error()
rt.Interrupt(&errext.InterruptError{Reason: reason})
}, a...)
}
sharedPanic(ctx, failFunc, a...)
}

// 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...)
failFunc := func(rt *goja.Runtime, a ...any) {
k6common.Throw(rt, fmt.Errorf(format, a...))
}
sharedPanic(ctx, failFunc, a...)
}

func sharedPanic(ctx context.Context, fail func(rt *goja.Runtime, a ...any), a ...any) {
func sharedPanic(ctx context.Context, failFunc func(rt *goja.Runtime, a ...any), a ...any) {
rt := Runtime(ctx)
if rt == nil {
// this should never happen unless a programmer error
Expand All @@ -49,7 +53,7 @@ func sharedPanic(ctx context.Context, fail func(rt *goja.Runtime, a ...any), a .
a[len(a)-1] = &UserFriendlyError{Err: err}
}
}
defer fail(rt, a...)
defer failFunc(rt, a...)

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

0 comments on commit 48ee41a

Please sign in to comment.