import { inspect } from 'node:util'; // § GROUP: Configuration field errors export class Data_Validation_Failed extends Error { constructor(data) { const { value, target, expected_description, validation_function, upstream_error } = data; const type = value === null ? 'null' : typeof value; const target_info = target?.info ? inspect(target.info) : 'unknown target'; const field_ref = target?.name ? `field "${target.name}"` : 'unknown field'; const expected_desc = expected_description ?? `data that would pass validation using ${validation_function}`; const upstream_error_description = upstream_error ? ` Upstream error was: ${upstream_error}` : ''; super(`Data validation failed for ${field_ref} of ${target_info}. Encountered data of type "${type}" while expecting "${expected_desc}".${upstream_error_description}`); this.data = data; } } export class Superfluous_Data_Field extends Error { constructor(data) { const { field_value, field_name, target, upstream_error } = data; const type = field_value === null ? 'null' : typeof field_value; const target_info = target?.info ? inspect(target.info) : 'unknown target'; const upstream_error_description = upstream_error ? ` Upstream error was: ${upstream_error}` : ''; super(`Data validation failed for ${target_info}. Superfluous field "${field_name}" with value of type "${type}" encountered.${upstream_error_description}`); this.data = data; } } export class Data_Coercion_Failed extends Error { constructor(data) { const { value, target, expected_description, coercion_function, upstream_error } = data; const type = value === null ? 'null' : typeof value; const target_info = target?.info ? inspect(target.info) : 'unknown target'; const field_ref = target?.name ? `field "${target.name}"` : 'unknown field'; const expected_desc = expected_description ?? `data that would be coerced using ${coercion_function}`; const upstream_error_description = upstream_error ? ` Upstream error was: ${upstream_error}` : ''; super(`Data coercion failed for ${field_ref} of ${target_info}. Encountered data of type "${type}" while expecting "${expected_desc}".${upstream_error_description}`); this.data = data; } } // § GROUP: Resolving errors export class Item_Unresolvable extends Error { constructor(data) { const { resolver, item } = data; const type = item === null ? 'null' : typeof item; super(`Cannot resolve item ${inspect(item)} of type "${type}" using resolver ${resolver.constructor.name}`); this.data = data; } } // § GROUP: Storage errors export class Not_Storable_Error extends Error { constructor(data) { const { storage, value, property, id } = data; const type = value === null ? 'null' : typeof value; const location = property ? `property "${property}" of record #${id}` : `record #${id}`; super(`Cannot store value of type "${type}" in ${location}: ${inspect(value)}`); this.data = data; } } export class Not_Mutable_Error extends Error { constructor(data) { const { storage, value, property, id } = data; const type = value === null ? 'null' : typeof value; const location = property ? `property "${property}" of record #${id}` : `record #${id}`; super(`Cannot set ${location}: record is immutable (tried to store value of type "${type}": ${inspect(value)})`); this.data = data; } } export class No_Such_Object_Index_Error extends Error { constructor(data) { const { storage, property, id } = data; const location = property ? `property "${property}" of record #${id}` : `record #${id}`; super(`Cannot get ${location}: no such record index`); this.data = data; } }