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

Make common.Throw add stacktraces the same as returning an error #2739

Merged
merged 2 commits into from
Oct 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion js/common/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func Throw(rt *goja.Runtime, err error) {
if e, ok := err.(*goja.Exception); ok { //nolint:errorlint // we don't really want to unwrap here
panic(e)
}
panic(rt.ToValue(err))
panic(rt.NewGoError(err)) // this catches the stack unlike rt.ToValue
}

// GetReader tries to return an io.Reader value from an exported goja value.
Expand Down
17 changes: 8 additions & 9 deletions js/common/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,21 @@ import (

"github.com/dop251/goja"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestThrow(t *testing.T) {
t.Parallel()
rt := goja.New()
fn1, ok := goja.AssertFunction(rt.ToValue(func() { Throw(rt, errors.New("aaaa")) }))
if assert.True(t, ok, "fn1 is invalid") {
_, err := fn1(goja.Undefined())
assert.EqualError(t, err, "aaaa")
require.True(t, ok, "fn1 is invalid")
_, err := fn1(goja.Undefined())
assert.EqualError(t, err, "GoError: aaaa")

fn2, ok := goja.AssertFunction(rt.ToValue(func() { Throw(rt, err) }))
if assert.True(t, ok, "fn1 is invalid") {
_, err := fn2(goja.Undefined())
assert.EqualError(t, err, "aaaa")
}
}
fn2, ok := goja.AssertFunction(rt.ToValue(func() { Throw(rt, err) }))
require.True(t, ok, "fn2 is invalid")
_, err = fn2(goja.Undefined())
assert.EqualError(t, err, "GoError: aaaa")
}

func TestToBytes(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion js/eventloop/eventloop_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,5 +211,5 @@ func TestEventLoopRejectThrow(t *testing.T) {
return err
})
loop.WaitOnRegistered()
require.EqualError(t, err, "Uncaught (in promise) throw error")
require.EqualError(t, err, "Uncaught (in promise) GoError: throw error: GoError\n\tat go.k6.io/k6/js/eventloop_test.TestEventLoopRejectThrow.func1 (native)\n\tat <eval>:1:30(1)\n\tat native\n\n")
}
22 changes: 11 additions & 11 deletions js/modules/k6/http/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"testing"

"github.com/dop251/goja"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -41,23 +40,24 @@ func TestHTTPFile(t *testing.T) {
FileData{Data: input, Filename: "test-ab.bin", ContentType: "application/octet-stream"},
"",
},
{struct{}{}, []string{}, FileData{}, "invalid type struct {}, expected string, []byte or ArrayBuffer"},
{struct{}{}, []string{}, FileData{}, "GoError: invalid type struct {}, expected string, []byte or ArrayBuffer"},
}

for _, tc := range testCases {
tc := tc
t.Run(fmt.Sprintf("%T", tc.input), func(t *testing.T) {
cal, _ := goja.AssertFunction(rt.ToValue(mi.file))
args := make([]goja.Value, 1, len(tc.args)+1)
args[0] = rt.ToValue(tc.input)
for _, arg := range tc.args {
args = append(args, rt.ToValue(arg))
}
_, err := cal(goja.Undefined(), args...)
if tc.expErr != "" {
defer func() {
err := recover()
require.NotNil(t, err)
require.IsType(t, &goja.Object{}, err)
val := err.(*goja.Object).Export()
require.EqualError(t, val.(error), tc.expErr)
}()
require.EqualError(t, err, tc.expErr)
} else {
require.NoError(t, err)
}
out := mi.file(tc.input, tc.args...)
assert.Equal(t, tc.expected, out)
})
}
}
Expand Down