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-5/

Custom Validation Error

Build a custom error class for validation scenarios that can aggregate multiple validation failures into a single throwable error. The error should be compatible with standard JavaScript error handling patterns and maintain proper error interface compliance.

Requirements

You need to create a ValidationError class that:

  1. Extends the standard Error class and maintains all standard error properties (name, message, stack)
  2. Accepts an array of validation failures where each failure can be:
    • An Error instance
    • A plain object with a message property (and optionally other metadata like field or code)
    • A string describing the failure
  3. Provides read-only access to the individual validation failures through a failures property
  4. Sets the error name to 'ValidationError' for easy identification
  5. Formats the error message to include all validation failures in a readable format

Behavior

  • If a non-array value is passed to the constructor, it should throw a TypeError with a clear message
  • The failures property should return the validation errors but prevent external modification of the internal error list
  • All input types (errors, objects, strings) should be normalized to Error instances internally
  • The class should be throwable and catchable like any standard JavaScript error

Test Cases

  • Creating a ValidationError with an array of mixed error types (Error instance, string, and object) works correctly and all failures are accessible through the failures property @test
  • Attempting to create a ValidationError with a non-array value (like a string or object) throws a TypeError @test
  • A ValidationError can be thrown and caught in a try-catch block, and it is recognized as an instance of both ValidationError and Error @test

Implementation

@generates

API

/**
 * Custom error class for aggregating validation failures
 */
class ValidationError extends Error {
  /**
   * @param {Array} failures - Array of validation failures (Error, object, or string)
   * @throws {TypeError} If failures is not an array
   */
  constructor(failures);

  /**
   * The error name, always 'ValidationError'
   * @type {string}
   */
  name;

  /**
   * Read-only access to the validation failures
   * @type {Error[]}
   */
  get failures();
}

export default ValidationError;

Dependencies { .dependencies }

aggregate-error { .dependency }

Provides functionality for creating errors from multiple errors with proper error normalization and standard Error interface compliance.

@satisfied-by

Install with Tessl CLI

npx tessl i tessl/npm-aggregate-error

tile.json