Environment
Provide version numbers for the following components (information can be retrieved by running tns info in your project folder or by inspecting the package.json of the project):
CLI: 8.1.5
Cross-platform modules: 6.5.11
Android Runtime: 6.5.3
iOS Runtime (if applicable): Possibly irrelevant
Plugin(s): Possibly irrelevant
Describe the bug
When an object contains a toString or toJSON method, and you print that object through console.log, it will automatically call that function. This is very different from what Chrome console.log does and may cause confusing behavior. For example:
The NativeScript HTTP client returns an object with a property called content that contains an object with a to toJSON method, when you console.log the return object, it will automatically call that toJSON method, while it might not even be JSON, causing the console.log to fail with a weird error from JSON.parse, like Unexpected end of JSON input.
To Reproduce
const randomVar = "test123";
const obj = {
toJSON: () => "toJSON => this should not be logged " + randomVar
};
console.log(obj);
Expected behavior
I think it should print something like:
{
"toJSON": ƒ
}
Or, it should catch any errors that happen during the format, and then fall back to it's normal behavior (going to toString, or not printing the property at all). It should not be breaking off the whole console.log and return the exception.
Additional context
Output when doing something like this in Chrome:
Environment Provide version numbers for the following components (information can be retrieved by running
tns info
in your project folder or by inspecting thepackage.json
of the project):Describe the bug When an object contains a toString or toJSON method, and you print that object through
console.log
, it will automatically call that function. This is very different from what Chromeconsole.log
does and may cause confusing behavior. For example: The NativeScript HTTP client returns an object with a property called content that contains an object with a totoJSON
method, when youconsole.log
the return object, it will automatically call thattoJSON
method, while it might not even be JSON, causing theconsole.log
to fail with a weird error fromJSON.parse
, likeUnexpected end of JSON input
.To Reproduce
Expected behavior I think it should print something like:
Or, it should catch any errors that happen during the format, and then fall back to it's normal behavior (going to toString, or not printing the property at all). It should not be breaking off the whole console.log and return the exception.
Additional context Output when doing something like this in Chrome:![image](https://user-images.githubusercontent.com/1312921/146782411-cb235921-51f6-483d-9f52-f7e02ba62d61.png)
Output when doing something like this in NS:![image](https://user-images.githubusercontent.com/1312921/146782438-ee94e13d-8243-40c5-84ae-3a52da9c98d7.png)
When causing an error inside the function with:
In Chrome:![image](https://user-images.githubusercontent.com/1312921/146782798-70444e4c-966c-45ec-a139-7c60f612413a.png)
In NS:![image](https://user-images.githubusercontent.com/1312921/146782909-d2576235-21db-4f02-8712-489626e035e8.png)