UNPKG

subtexty

Version:

Extract clean plain-text from subtitle files

161 lines 5.41 kB
"use strict"; /** * Custom error classes for better error handling and categorization */ Object.defineProperty(exports, "__esModule", { value: true }); exports.ErrorFactory = exports.GenericValidationError = exports.GenericParsingError = exports.GenericFileError = exports.InputValidationError = exports.ValidationError = exports.InvalidFormatError = exports.UnsupportedFormatError = exports.ParsingError = exports.WriteError = exports.OutputDirectoryError = exports.FileNotReadableError = exports.FileNotFoundError = exports.FileError = exports.SubtextyError = void 0; exports.isSubtextyError = isSubtextyError; exports.getExitCode = getExitCode; class SubtextyError extends Error { constructor(message, context) { super(message); this.context = context; this.name = this.constructor.name; // Maintain proper stack trace in V8 if (Error.captureStackTrace) { Error.captureStackTrace(this, this.constructor); } } } exports.SubtextyError = SubtextyError; class FileError extends SubtextyError { constructor(message, filePath, context) { super(message, { ...context, filePath }); this.filePath = filePath; this.exitCode = 1; } } exports.FileError = FileError; class FileNotFoundError extends FileError { constructor(filePath) { super(`Input file not found: ${filePath}`, filePath); this.code = 'FILE_NOT_FOUND'; } } exports.FileNotFoundError = FileNotFoundError; class FileNotReadableError extends FileError { constructor(filePath) { super(`Cannot read input file: ${filePath}`, filePath); this.code = 'FILE_NOT_READABLE'; } } exports.FileNotReadableError = FileNotReadableError; class OutputDirectoryError extends FileError { constructor(directory) { super(`Output directory does not exist: ${directory}`, directory); this.code = 'OUTPUT_DIR_NOT_EXIST'; } } exports.OutputDirectoryError = OutputDirectoryError; class WriteError extends FileError { constructor(filePath, originalError) { super(`Failed to write output file: ${filePath}`, filePath, { originalError }); this.code = 'WRITE_ERROR'; } } exports.WriteError = WriteError; class ParsingError extends SubtextyError { constructor(message, format, context) { super(message, { ...context, format }); this.format = format; this.exitCode = 2; } } exports.ParsingError = ParsingError; class UnsupportedFormatError extends ParsingError { constructor(extension) { super(`Unsupported file format: ${extension}`, extension); this.code = 'UNSUPPORTED_FORMAT'; } } exports.UnsupportedFormatError = UnsupportedFormatError; class InvalidFormatError extends ParsingError { constructor(format, details) { super(`Invalid ${format} format: ${details}`, format); this.code = 'INVALID_FORMAT'; } } exports.InvalidFormatError = InvalidFormatError; class ValidationError extends SubtextyError { constructor(message, field, context) { super(message, { ...context, field }); this.field = field; this.exitCode = 2; } } exports.ValidationError = ValidationError; class InputValidationError extends ValidationError { constructor(field, reason) { super(`Invalid input for ${field}: ${reason}`, field); this.code = 'INPUT_VALIDATION_ERROR'; } } exports.InputValidationError = InputValidationError; // Add concrete implementations for the base error types class GenericFileError extends FileError { constructor() { super(...arguments); this.code = 'FILE_ERROR'; } } exports.GenericFileError = GenericFileError; class GenericParsingError extends ParsingError { constructor() { super(...arguments); this.code = 'PARSING_ERROR'; } } exports.GenericParsingError = GenericParsingError; class GenericValidationError extends ValidationError { constructor() { super(...arguments); this.code = 'VALIDATION_ERROR'; } } exports.GenericValidationError = GenericValidationError; /** * Error factory for creating specific error types */ class ErrorFactory { static fileNotFound(filePath) { return new FileNotFoundError(filePath); } static fileNotReadable(filePath) { return new FileNotReadableError(filePath); } static outputDirectoryNotExist(directory) { return new OutputDirectoryError(directory); } static writeError(filePath, originalError) { return new WriteError(filePath, originalError); } static unsupportedFormat(extension) { return new UnsupportedFormatError(extension); } static invalidFormat(format, details) { return new InvalidFormatError(format, details); } static inputValidation(field, reason) { return new InputValidationError(field, reason); } static parsing(message, format) { return new GenericParsingError(message, format); } } exports.ErrorFactory = ErrorFactory; /** * Type guard to check if an error is a SubtextyError */ function isSubtextyError(error) { return error instanceof SubtextyError; } /** * Get appropriate exit code from any error */ function getExitCode(error) { if (isSubtextyError(error)) { return error.exitCode; } return 2; // Default to parsing error } //# sourceMappingURL=errors.js.map