/** * Base error class for smartarchive */ export class SmartArchiveError extends Error { public readonly code: string; constructor(message: string, code: string) { super(message); this.name = 'SmartArchiveError'; this.code = code; // Maintains proper stack trace for where error was thrown (V8) if (Error.captureStackTrace) { Error.captureStackTrace(this, this.constructor); } } } /** * BZIP2-specific decompression errors */ export class Bzip2Error extends SmartArchiveError { constructor(message: string, code: string = 'BZIP2_ERROR') { super(message, code); this.name = 'Bzip2Error'; } } /** * Archive format detection errors */ export class ArchiveFormatError extends SmartArchiveError { constructor(message: string) { super(message, 'ARCHIVE_FORMAT_ERROR'); this.name = 'ArchiveFormatError'; } } /** * Stream processing errors */ export class StreamError extends SmartArchiveError { constructor(message: string) { super(message, 'STREAM_ERROR'); this.name = 'StreamError'; } } /** * BZIP2 error codes for programmatic error handling */ export const BZIP2_ERROR_CODES = { NO_MAGIC_NUMBER: 'BZIP2_NO_MAGIC', INVALID_ARCHIVE: 'BZIP2_INVALID_ARCHIVE', CRC_MISMATCH: 'BZIP2_CRC_MISMATCH', INVALID_BLOCK_DATA: 'BZIP2_INVALID_BLOCK', BUFFER_OVERFLOW: 'BZIP2_BUFFER_OVERFLOW', INVALID_HUFFMAN: 'BZIP2_INVALID_HUFFMAN', INVALID_SELECTOR: 'BZIP2_INVALID_SELECTOR', INVALID_POSITION: 'BZIP2_INVALID_POSITION', PREMATURE_END: 'BZIP2_PREMATURE_END', } as const; export type TBzip2ErrorCode = typeof BZIP2_ERROR_CODES[keyof typeof BZIP2_ERROR_CODES]; /** * Throw a BZIP2 error with a specific code */ export function throwBzip2Error(message: string, code: TBzip2ErrorCode): never { throw new Bzip2Error(message, code); }