Create an error from multiple errors
94
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.
Create a BatchLogger class with the following behavior:
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.
Entry Types Support: The logger should accept three types of entries in the array:
message property (e.g., {message: 'Log message', level: 'info'})'Simple log message')Entry Storage: Store the normalized entries internally and provide a getter method entries that returns an array of the stored entries.
Formatted Output: Provide a method format() that returns a formatted string containing all log entries, one per line.
typeof operator to identify the actual type in the error messageProvides error aggregation support that demonstrates input validation patterns.
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.
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-errordocs
evals
scenario-1
scenario-2
scenario-3
scenario-4
scenario-5
scenario-6
scenario-7
scenario-8
scenario-9