Skip to content
This repository was archived by the owner on Jan 30, 2025. It is now read-only.

Commit 1b28e42

Browse files
author
Ivan Mirić
committed
Use custom error for object overflow warning
Resolves #130 (comment)
1 parent 2d32025 commit 1b28e42

File tree

3 files changed

+24
-10
lines changed

3 files changed

+24
-10
lines changed

common/frame_session.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ package common
2323
import (
2424
"context"
2525
"encoding/json"
26+
"errors"
2627
"fmt"
2728
"strings"
2829
"sync"
@@ -479,8 +480,13 @@ func (fs *FrameSession) onConsoleAPICalled(event *runtime.EventConsoleAPICalled)
479480
for _, robj := range event.Args {
480481
i, err := parseRemoteObject(robj, l)
481482
if err != nil {
482-
// If this throws it's a bug :)
483-
k6Throw(fs.ctx, "unable to parse remote object value: %w", err)
483+
var oe *objectOverflowError
484+
if errors.As(err, &oe) {
485+
l.Warn(err)
486+
} else {
487+
// If this throws it's a bug :)
488+
k6Throw(fs.ctx, "unable to parse remote object value: %w", err)
489+
}
484490
}
485491
parsedObjects = append(parsedObjects, i)
486492
}

common/remote_object.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,18 @@ import (
3333
k6common "go.k6.io/k6/js/common"
3434
)
3535

36+
type objectOverflowError struct{}
37+
38+
// Error returns the description of the overflow error.
39+
func (*objectOverflowError) Error() string {
40+
return "object is too large and will be parsed partially"
41+
}
42+
3643
func parseRemoteObjectPreview(op *cdpruntime.ObjectPreview, logger *logrus.Entry) (map[string]interface{}, error) {
3744
obj := make(map[string]interface{})
45+
var err error
3846
if op.Overflow {
39-
logger.Warn("object will be parsed partially")
47+
err = &objectOverflowError{}
4048
}
4149

4250
for _, p := range op.Properties {
@@ -48,7 +56,7 @@ func parseRemoteObjectPreview(op *cdpruntime.ObjectPreview, logger *logrus.Entry
4856
obj[p.Name] = val
4957
}
5058

51-
return obj, nil
59+
return obj, err
5260
}
5361

5462
func parseRemoteObjectValue(

common/remote_object_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ import (
2828

2929
"github.com/chromedp/cdproto/runtime"
3030
"github.com/dop251/goja"
31-
"github.com/grafana/xk6-browser/testutils"
3231
"github.com/mailru/easyjson"
3332
"github.com/sirupsen/logrus"
3433
"github.com/stretchr/testify/assert"
@@ -192,6 +191,7 @@ func TestParseRemoteObject(t *testing.T) {
192191
preview *runtime.ObjectPreview
193192
value string
194193
expected map[string]interface{}
194+
expErr error
195195
}{
196196
{
197197
name: "most_types",
@@ -236,6 +236,7 @@ func TestParseRemoteObject(t *testing.T) {
236236
name: "overflow",
237237
preview: &runtime.ObjectPreview{Overflow: true},
238238
expected: map[string]interface{}{},
239+
expErr: &objectOverflowError{},
239240
},
240241
}
241242

@@ -249,14 +250,13 @@ func TestParseRemoteObject(t *testing.T) {
249250
Value: easyjson.RawMessage(tc.value),
250251
}
251252
lg := logrus.New()
252-
logHook := testutils.LogHook(lg)
253253
logger := NewLogger(ctx, lg, false, nil)
254254
val, err := parseRemoteObject(remoteObject, logger.log.WithContext(ctx))
255-
require.NoError(t, err)
256255
assert.Equal(t, tc.expected, val)
257-
258-
if tc.name == "overflow" {
259-
assert.True(t, logHook.Contains("object will be parsed partially"))
256+
if tc.expErr != nil {
257+
assert.ErrorAs(t, err, &tc.expErr)
258+
} else {
259+
assert.NoError(t, err)
260260
}
261261
})
262262
}

0 commit comments

Comments
 (0)