CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-aggregate-error

Create an error from multiple errors

94

1.28x
Overview
Eval results
Files

task.mdevals/scenario-9/

Batch Logger Implementation { .spec }

Overview

Implement a batch logger that accepts multiple log entries and formats them for output. The logger should validate its inputs and handle different entry types appropriately.

Requirements

Create a BatchLogger class with the following behavior:

  1. Constructor Input Validation: The constructor must accept a single parameter entries which must be an array. If the input is not an array, throw a TypeError with a descriptive message indicating the expected type and the actual type received.

  2. Entry Types Support: The logger should accept three types of entries in the array:

    • Objects with a message property (e.g., {message: 'Log message', level: 'info'})
    • Plain strings (e.g., 'Simple log message')
    • Any other primitive values that should be converted to strings
  3. Entry Storage: Store the normalized entries internally and provide a getter method entries that returns an array of the stored entries.

  4. Formatted Output: Provide a method format() that returns a formatted string containing all log entries, one per line.

Implementation Notes

  • The class should properly validate the constructor parameter before processing
  • The validation error message should clearly state what type was expected and what type was received
  • Use the typeof operator to identify the actual type in the error message
  • Ensure entries are stored in a way that allows retrieval via the getter

Dependencies { .dependencies }

aggregate-error { .dependency }

Provides error aggregation support that demonstrates input validation patterns.

Test Cases

Test 1: Array Validation { .test-case @test }

File: test/batch-logger.test.js

Test Code:

import {test} from 'node:test';
import assert from 'node:assert';
import BatchLogger from '../batch-logger.js';

test('throws TypeError when input is not an array', () => {
  assert.throws(
    () => new BatchLogger('not an array'),
    {
      name: 'TypeError',
      message: /Expected input to be an Array/
    }
  );
});

Expected Behavior: Creating a BatchLogger with a non-array value should throw a TypeError with a message that mentions the expected type (Array) and includes information about the actual type received.

Test 2: Basic Functionality { .test-case @test }

File: test/batch-logger.test.js

Test Code:

test('accepts array of entries and provides access', () => {
  const logger = new BatchLogger([
    {message: 'First log', level: 'info'},
    'Second log',
    123
  ]);

  assert.ok(Array.isArray(logger.entries));
  assert.strictEqual(logger.entries.length, 3);
});

Expected Behavior: The BatchLogger should accept an array of mixed entry types and store them so they can be accessed through the entries getter property.

Install with Tessl CLI

npx tessl i tessl/npm-aggregate-error

tile.json