From ea1733095536ecee793c8029d46844632d7a5084 Mon Sep 17 00:00:00 2001 From: Laurent Christophe Date: Tue, 15 Feb 2022 17:08:39 +0100 Subject: [PATCH] fix: verify that toString method returned a string Failure happened when the toString method of a recorded value returned a non-string value (specifically null or undefined). In this case, we now fallback to safe stringify operation. --- components/serialization/default/index.mjs | 4 ++++ components/serialization/default/index.test.mjs | 16 ++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/components/serialization/default/index.mjs b/components/serialization/default/index.mjs index 232907269..05dd7574f 100644 --- a/components/serialization/default/index.mjs +++ b/components/serialization/default/index.mjs @@ -124,6 +124,10 @@ export default (dependencies) => { logDebug("%o.toString() failure >> %O", value, error); serial.print = apply(toString, value, noargs); } + if (typeof serial.print !== "string") { + logDebug("%o.toString() returned a non-string result"); + serial.print = apply(toString, value, noargs); + } } else { serial.print = apply(toString, value, noargs); } diff --git a/components/serialization/default/index.test.mjs b/components/serialization/default/index.test.mjs index 408e7b3b2..47d8b68d0 100644 --- a/components/serialization/default/index.test.mjs +++ b/components/serialization/default/index.test.mjs @@ -150,6 +150,22 @@ assertDeepEqual( print: "[object Object]", }, ); +assertDeepEqual( + testSerialize( + { + method: "toString", + "include-constructor-name": false, + }, + { + toString: () => 123, + }, + ), + { + type: "object", + index: 1, + print: "[object Object]", + }, +); // function (toString) assertDeepEqual( testSerialize(