Deterministic and safely JSON.stringify to quickly serialize JavaScript objects
90
Evaluation — 90%
↑ 1.08xAgent success when using this tile
Build a logging utility that safely serializes JavaScript objects for storage, even when those objects contain problematic getters or toJSON methods that may throw errors.
Your task is to create a data logger that can safely serialize objects with the following behaviors:
Handle objects with failing getters: Objects may have getter properties that throw errors when accessed. The logger should gracefully handle these errors and still produce output.
Handle objects with failing toJSON methods: Objects may implement toJSON methods that throw errors. The logger should handle these errors without crashing.
Preserve other properties: When an error occurs with a specific property or toJSON method, other valid properties should still be serialized correctly.
Return valid JSON string: The output should always be a valid JSON string, even when errors occur during serialization.
@generates
Create a function safeLog(data) that takes any JavaScript value and returns a JSON string representation, handling any errors that occur during serialization.
When an object has a getter that throws an error, the logger produces a valid JSON string and includes error information for the problematic property. @test
When an object has a toJSON method that throws an error, the logger produces a valid JSON string containing error information. @test
When an object has both valid properties and a problematic getter, the valid properties are included in the output. @test
When passed a simple object with no errors, the logger produces standard JSON output. @test
/**
* Safely serializes a JavaScript value to a JSON string, handling errors from
* getters and toJSON methods gracefully.
*
* @param {*} data - The data to serialize
* @returns {string} A JSON string representation of the data
*/
function safeLog(data) {
// IMPLEMENTATION HERE
}
module.exports = { safeLog };Provides safe JSON serialization with error handling capabilities.
Install with Tessl CLI
npx tessl i tessl/npm-safe-stable-stringifydocs
evals
scenario-1
scenario-2
scenario-3
scenario-4
scenario-5
scenario-6
scenario-7
scenario-8
scenario-9
scenario-10