@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
97 lines (96 loc) • 3.49 kB
JavaScript
import { InvalidExpressionError, OperatorError, ValidationError, TypeMismatchError, GeospatialError, ConfigurationError, } from './filter-errors.js';
export const ErrorCodes = {
INVALID_EXPRESSION: 'INVALID_EXPRESSION',
OPERATOR_ERROR: 'OPERATOR_ERROR',
VALIDATION_ERROR: 'VALIDATION_ERROR',
CONFIGURATION_ERROR: 'CONFIGURATION_ERROR',
TYPE_MISMATCH: 'TYPE_MISMATCH',
GEOSPATIAL_ERROR: 'GEOSPATIAL_ERROR',
PERFORMANCE_LIMIT: 'PERFORMANCE_LIMIT',
};
export function isFilterError(error) {
return error instanceof Error && 'code' in error;
}
export function formatErrorMessage(message, context) {
if (!context)
return message;
const contextStr = Object.entries(context)
.map(([key, value]) => ` ${key}: ${JSON.stringify(value)}`)
.join('\n');
return `${message}\n\nContext:\n${contextStr}`;
}
export function createValidationError(field, errors) {
const details = errors.join(', ');
return new ValidationError(details, field, errors);
}
export function createOperatorError(operator, value, expected) {
const details = `Expected ${expected}, received ${typeof value}`;
return new OperatorError(operator, value, details);
}
export function createTypeMismatchError(field, expected, received) {
return new TypeMismatchError(expected, typeof received, field);
}
export function createGeospatialError(coordinates, reason) {
return new GeospatialError(reason, coordinates);
}
export function createConfigurationError(option, value, reason) {
return new ConfigurationError(`${reason}. Received: ${JSON.stringify(value)}`, option);
}
export function wrapError(error, context) {
if (isFilterError(error)) {
return error;
}
const message = error instanceof Error ? error.message : String(error);
const stack = error instanceof Error ? error.stack : undefined;
const filterError = new (class extends Error {
code = 'UNKNOWN_ERROR';
context = context;
})(message);
if (stack) {
filterError.stack = stack;
}
return filterError;
}
export function extractErrorDetails(error) {
if (isFilterError(error)) {
return {
message: error.message,
code: error.code,
context: error.context,
stack: error.stack,
};
}
if (error instanceof Error) {
return {
message: error.message,
stack: error.stack,
};
}
return {
message: String(error),
};
}
export function getUserFriendlyMessage(error) {
if (error instanceof InvalidExpressionError) {
return 'The filter expression is invalid. Please check the syntax and try again.';
}
if (error instanceof OperatorError) {
return `Invalid operator usage: ${error.operator}. Please check the documentation for correct usage.`;
}
if (error instanceof ValidationError) {
return `Validation failed: ${error.message}`;
}
if (error instanceof TypeMismatchError) {
return `Type error: Expected ${error.expected} but received ${error.received}`;
}
if (error instanceof GeospatialError) {
return 'Invalid geographic coordinates. Please provide valid latitude and longitude values.';
}
if (error instanceof ConfigurationError) {
return `Configuration error: ${error.message}`;
}
if (error instanceof Error) {
return error.message;
}
return 'An unknown error occurred';
}