UNPKG

@othree.io/excuses

Version:

Excuses

38 lines 1.65 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ValidationError = void 0; /** * Converts validation errors object to a JSON string. * * @param {FieldsValidationErrors} validationErrors - Object mapping field names to arrays of error messages. * @returns {string} A JSON string representation of the validation errors. */ const getErrorsMessage = (validationErrors) => { return JSON.stringify(validationErrors); }; /** * Custom Error class representing a validation error. */ class ValidationError extends Error { /** * Creates a new ValidationError. * * @param {FieldsValidationErrors} [validationErrors] - Optional object containing field validation errors. * @param {string} [message] - Optional generic error message. If `validationErrors` is provided, this parameter is ignored. */ constructor(validationErrors, message) { super(validationErrors ? getErrorsMessage(validationErrors) : message); // https://github.com/Microsoft/TypeScript-wiki/blob/main/Breaking-Changes.md#extending-built-ins-like-error-array-and-map-may-no-longer-work Object.setPrototypeOf(this, new.target.prototype); // restore prototype chain this.name = ValidationError.ERROR; this.stack = new Error().stack; if (validationErrors) { this.errors = validationErrors; this.errorsMessage = getErrorsMessage(validationErrors); } } } exports.ValidationError = ValidationError; /** Static error code to identify the error type. */ ValidationError.ERROR = 'ValidationError'; //# sourceMappingURL=validation-error.js.map