Skip to content
This repository has been archived by the owner on Mar 15, 2024. It is now read-only.

Commit

Permalink
fix: verify that toString method returned a string
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
lachrist committed Feb 15, 2022
1 parent 0920649 commit ea17330
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
4 changes: 4 additions & 0 deletions components/serialization/default/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
16 changes: 16 additions & 0 deletions components/serialization/default/index.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit ea17330

Please sign in to comment.