@mcabreradev/filter
Version:
A powerful, SQL-like array filtering library for TypeScript and JavaScript with advanced pattern matching, MongoDB-style operators, deep object comparison, and zero dependencies
127 lines (126 loc) • 3.85 kB
JavaScript
export class FilterError extends Error {
code;
context;
constructor(message, code, context) {
super(message);
this.code = code;
this.context = context;
this.name = 'FilterError';
if (Error.captureStackTrace) {
Error.captureStackTrace(this, FilterError);
}
}
toJSON() {
return {
name: this.name,
message: this.message,
code: this.code,
context: this.context,
stack: this.stack,
};
}
toString() {
const contextStr = this.context ? `\nContext: ${JSON.stringify(this.context, null, 2)}` : '';
return `${this.name} [${this.code}]: ${this.message}${contextStr}`;
}
}
export class InvalidExpressionError extends FilterError {
validationErrors;
constructor(expression, details, validationErrors) {
const message = `Invalid filter expression: ${details}\nReceived: ${JSON.stringify(expression)}`;
super(message, 'INVALID_EXPRESSION', {
expression,
details,
validationErrors,
});
this.validationErrors = validationErrors;
this.name = 'InvalidExpressionError';
}
}
export class OperatorError extends FilterError {
operator;
constructor(operator, value, details) {
const message = `Operator '${operator}' error: ${details}\nValue: ${JSON.stringify(value)}`;
super(message, 'OPERATOR_ERROR', {
operator,
value,
details,
});
this.operator = operator;
this.name = 'OperatorError';
}
}
export class ValidationError extends FilterError {
field;
errors;
constructor(details, field, errors) {
const fieldStr = field ? ` for field '${field}'` : '';
const message = `Validation failed${fieldStr}: ${details}`;
super(message, 'VALIDATION_ERROR', {
field,
details,
errors,
});
this.field = field;
this.errors = errors;
this.name = 'ValidationError';
}
}
export class ConfigurationError extends FilterError {
option;
constructor(details, option) {
const optionStr = option ? ` for option '${option}'` : '';
const message = `Configuration error${optionStr}: ${details}`;
super(message, 'CONFIGURATION_ERROR', {
option,
details,
});
this.option = option;
this.name = 'ConfigurationError';
}
}
export class TypeMismatchError extends FilterError {
expected;
received;
field;
constructor(expected, received, field) {
const fieldStr = field ? ` for field '${field}'` : '';
const message = `Type mismatch${fieldStr}: expected ${expected}, received ${received}`;
super(message, 'TYPE_MISMATCH', {
expected,
received,
field,
});
this.expected = expected;
this.received = received;
this.field = field;
this.name = 'TypeMismatchError';
}
}
export class GeospatialError extends FilterError {
coordinates;
constructor(details, coordinates) {
const message = `Geospatial error: ${details}`;
super(message, 'GEOSPATIAL_ERROR', {
details,
coordinates,
});
this.coordinates = coordinates;
this.name = 'GeospatialError';
}
}
export class PerformanceLimitError extends FilterError {
limit;
actual;
constructor(details, limit, actual) {
const message = `Performance limit exceeded: ${details}`;
super(message, 'PERFORMANCE_LIMIT', {
details,
limit,
actual,
});
this.limit = limit;
this.actual = actual;
this.name = 'PerformanceLimitError';
}
}