CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-safe-stable-stringify

Deterministic and safely JSON.stringify to quickly serialize JavaScript objects

90

1.08x

Evaluation90%

1.08x

Agent success when using this tile

Overview
Eval results
Files

task.mdevals/scenario-1/

Robust Data Logger

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.

Requirements

Your task is to create a data logger that can safely serialize objects with the following behaviors:

  1. 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.

  2. Handle objects with failing toJSON methods: Objects may implement toJSON methods that throw errors. The logger should handle these errors without crashing.

  3. Preserve other properties: When an error occurs with a specific property or toJSON method, other valid properties should still be serialized correctly.

  4. Return valid JSON string: The output should always be a valid JSON string, even when errors occur during serialization.

Implementation

@generates

Create a function safeLog(data) that takes any JavaScript value and returns a JSON string representation, handling any errors that occur during serialization.

Test Cases

  • 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

API

/**
 * 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 };

Dependencies { .dependencies }

safe-stable-stringify { .dependency }

Provides safe JSON serialization with error handling capabilities.

Install with Tessl CLI

npx tessl i tessl/npm-safe-stable-stringify

tile.json