Skip to content

Commit

Permalink
Refactor parseConsoleRemoteObject to return error
Browse files Browse the repository at this point in the history
There is another use case for parseConsoleRemoteObject, which is when
JSONValue is called. At the moment this API uses valueFromRemoteObject
which returns a goja.Value. Interestingly, the return value is always
converted to string before being returned to the caller, so the extra
step to convert to goja and then back to string is not needed.
  • Loading branch information
ankur22 committed Jan 19, 2024
1 parent 5f754c2 commit 7302c41
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 20 deletions.
5 changes: 4 additions & 1 deletion common/frame_session.go
Original file line number Diff line number Diff line change
Expand Up @@ -625,7 +625,10 @@ func (fs *FrameSession) onConsoleAPICalled(event *cdpruntime.EventConsoleAPICall

parsedObjects := make([]string, 0, len(event.Args))
for _, robj := range event.Args {
s := parseConsoleRemoteObject(fs.logger, robj)
s, err := parseConsoleRemoteObject(fs.logger, robj)
if err != nil {
fs.logger.Errorf("onConsoleAPICalled", "failed to parse console message %v", err)
}
parsedObjects = append(parsedObjects, s)
}

Expand Down
5 changes: 4 additions & 1 deletion common/page.go
Original file line number Diff line number Diff line change
Expand Up @@ -1374,7 +1374,10 @@ func (p *Page) consoleMsgFromConsoleEvent(e *cdpruntime.EventConsoleAPICalled) (
)

for _, robj := range e.Args {
s := parseConsoleRemoteObject(p.logger, robj)
s, err := parseConsoleRemoteObject(p.logger, robj)
if err != nil {
p.logger.Errorf("consoleMsgFromConsoleEvent", "failed to parse console message %v", err)
}

objects = append(objects, s)
objectHandles = append(objectHandles, NewJSHandle(
Expand Down
42 changes: 24 additions & 18 deletions common/remote_object.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,42 +229,48 @@ func valueFromRemoteObject(ctx context.Context, robj *cdpruntime.RemoteObject) (
return k6ext.Runtime(ctx).ToValue(val), err
}

func parseConsoleRemoteObjectPreview(logger *log.Logger, op *cdpruntime.ObjectPreview) string {
func parseConsoleRemoteObjectPreview(logger *log.Logger, op *cdpruntime.ObjectPreview) (string, error) {
obj := make(map[string]string)
if op.Overflow {
logger.Infof("parseConsoleRemoteObjectPreview", "object is too large and will be parsed partially")
}

for _, p := range op.Properties {
val := parseConsoleRemoteObjectValue(logger, p.Type, p.Subtype, p.Value, p.ValuePreview)
val, err := parseConsoleRemoteObjectValue(logger, p.Type, p.Subtype, p.Value, p.ValuePreview)
if err != nil {
return "", err
}
obj[p.Name] = val
}

bb, err := json.Marshal(obj)
if err != nil {
logger.Errorf("parseConsoleRemoteObjectPreview", "failed to marshal object to string: %v", err)
return "", fmt.Errorf("marshaling object to string: %w", err)
}

return string(bb)
return string(bb), nil
}

func parseConsoleRemoteArrayPreview(logger *log.Logger, op *cdpruntime.ObjectPreview) string {
func parseConsoleRemoteArrayPreview(logger *log.Logger, op *cdpruntime.ObjectPreview) (string, error) {
arr := make([]any, 0, len(op.Properties))
if op.Overflow {
logger.Warnf("parseConsoleRemoteArrayPreview", "array is too large and will be parsed partially")
logger.Infof("parseConsoleRemoteArrayPreview", "array is too large and will be parsed partially")
}

for _, p := range op.Properties {
val := parseConsoleRemoteObjectValue(logger, p.Type, p.Subtype, p.Value, p.ValuePreview)
val, err := parseConsoleRemoteObjectValue(logger, p.Type, p.Subtype, p.Value, p.ValuePreview)
if err != nil {
return "", err
}
arr = append(arr, val)
}

bb, err := json.Marshal(arr)
if err != nil {
logger.Errorf("parseConsoleRemoteArrayPreview", "failed to marshal array to string: %v", err)
return "", fmt.Errorf("marshaling array to string: %w", err)
}

return string(bb)
return string(bb), nil
}

//nolint:cyclop
Expand All @@ -274,12 +280,12 @@ func parseConsoleRemoteObjectValue(
st cdpruntime.Subtype,
val string,
op *cdpruntime.ObjectPreview,
) string {
) (string, error) {
switch t {
case cdpruntime.TypeAccessor:
return "accessor"
return "accessor", nil
case cdpruntime.TypeFunction:
return "function()"
return "function()", nil
case cdpruntime.TypeString:
if strings.HasPrefix(val, `"`) {
val = strings.TrimPrefix(val, `"`)
Expand All @@ -293,13 +299,13 @@ func parseConsoleRemoteObjectValue(
return parseConsoleRemoteObjectPreview(logger, op)
}
if val == "Object" {
return val
return val, nil
}
if st == "null" {
return "null"
return "null", nil
}
case cdpruntime.TypeUndefined:
return "undefined"
return "undefined", nil
// The following cases are here to clarify that all cases have been
// considered, but that the result will return val without processing it.
case cdpruntime.TypeNumber:
Expand All @@ -308,15 +314,15 @@ func parseConsoleRemoteObjectValue(
case cdpruntime.TypeBigint:
}

return val
return val, nil
}

// parseConsoleRemoteObject is to be used by callers that are working with
// console messages that are written to Chrome's console by the website under
// test.
func parseConsoleRemoteObject(logger *log.Logger, obj *cdpruntime.RemoteObject) string {
func parseConsoleRemoteObject(logger *log.Logger, obj *cdpruntime.RemoteObject) (string, error) {
if obj.UnserializableValue != "" {
return obj.UnserializableValue.String()
return obj.UnserializableValue.String(), nil
}

return parseConsoleRemoteObjectValue(logger, obj.Type, obj.Subtype, string(obj.Value), obj.Preview)
Expand Down

0 comments on commit 7302c41

Please sign in to comment.