Exceptions API

Custom exception classes used by tzst for comprehensive error handling and debugging.

Exception classes for tzst.

exception tzst.exceptions.TzstArchiveError[source]

Bases: TzstError

Exception raised when archive operations fail.

This can occur when: - Archive file cannot be opened or created - File permissions prevent archive access - Archive structure is malformed - Tar operations fail within the archive - Atomic file operations fail during creation

exception tzst.exceptions.TzstCompressionError[source]

Bases: TzstError

Exception raised when compression operations fail.

This can occur when: - Invalid compression level is specified - Disk space is insufficient during compression - Input data cannot be compressed due to corruption - Zstandard compression encounters an internal error

exception tzst.exceptions.TzstDecompressionError[source]

Bases: TzstError

Exception raised when decompression operations fail.

This can occur when: - Archive file is corrupted or incomplete - Archive was not created with zstandard compression - Decompression buffer overflows or underflows - Archive format is invalid or unsupported

exception tzst.exceptions.TzstError[source]

Bases: Exception

Base exception for all tzst operations.

This is the parent class for all tzst-specific exceptions. Catch this to handle any tzst-related error.

exception tzst.exceptions.TzstFileNotFoundError[source]

Bases: TzstError, FileNotFoundError

Exception raised when a required file is not found.

This can occur when: - Archive file does not exist for reading operations - Input files for archiving do not exist - Output directory cannot be created for extraction - Temporary files cannot be created during atomic operations

Inherits from both TzstError and FileNotFoundError for compatibility with standard Python exception handling patterns.

Overview

The tzst library provides a comprehensive hierarchy of exceptions to help identify and handle different types of errors that may occur during archive operations. All exceptions inherit from the base TzstError class, making it easy to catch all tzst-related errors with a single exception handler.

Exception Hierarchy

TzstError (base exception)
├── TzstArchiveError (archive operation failures)
├── TzstCompressionError (compression failures)
└── TzstDecompressionError (decompression failures)

Exception Classes

Base Exception

TzstError

exception tzst.exceptions.TzstError[source]

Bases: Exception

Base exception for all tzst operations.

This is the parent class for all tzst-specific exceptions. Catch this to handle any tzst-related error.

The base exception class for all tzst operations. Catch this exception to handle any tzst-related error in your application.

Usage:

from tzst import create_archive, TzstError

try:
    create_archive("backup.tzst", ["files/"])
except TzstError as e:
    print(f"tzst operation failed: {e}")

Archive Operation Exceptions

TzstArchiveError

exception tzst.exceptions.TzstArchiveError[source]

Bases: TzstError

Exception raised when archive operations fail.

This can occur when: - Archive file cannot be opened or created - File permissions prevent archive access - Archive structure is malformed - Tar operations fail within the archive - Atomic file operations fail during creation

Raised when archive operations fail, such as:

  • Archive file cannot be opened or created

  • File permissions prevent archive access

  • Archive structure is malformed

  • Tar operations fail within the archive

  • Atomic file operations fail during creation

Common Scenarios:

  • Invalid archive file path

  • Insufficient disk space

  • File permission errors

  • Corrupt archive structure

Compression Exceptions

TzstCompressionError

exception tzst.exceptions.TzstCompressionError[source]

Bases: TzstError

Exception raised when compression operations fail.

This can occur when: - Invalid compression level is specified - Disk space is insufficient during compression - Input data cannot be compressed due to corruption - Zstandard compression encounters an internal error

Raised when compression operations fail, including:

  • Invalid compression level is specified

  • Disk space is insufficient during compression

  • Input data cannot be compressed due to corruption

  • Zstandard compression encounters an internal error

Common Scenarios:

  • Compression level out of range (1-22)

  • Insufficient disk space during compression

  • Source file corruption

  • Zstandard library errors

Decompression Exceptions

TzstDecompressionError

exception tzst.exceptions.TzstDecompressionError[source]

Bases: TzstError

Exception raised when decompression operations fail.

This can occur when: - Archive file is corrupted or incomplete - Archive was not created with zstandard compression - Decompression buffer overflows or underflows - Archive format is invalid or unsupported

Raised when decompression operations fail, such as:

  • Archive file is corrupted or incomplete

  • Archive was not created with zstandard compression

  • Decompression buffer overflows or underflows

  • Archive format is invalid or unsupported

Common Scenarios:

  • Corrupted or truncated archive files

  • Non-zstandard compressed archives

  • Invalid tar structure within archive

  • Archive format version mismatches

Error Handling Best Practices

Basic Error Handling

from tzst import create_archive, TzstArchiveError, TzstCompressionError

try:
    create_archive("backup.tzst", ["documents/"])
except TzstCompressionError as e:
    print(f"Compression failed: {e}")
except TzstArchiveError as e:
    print(f"Archive operation failed: {e}")

Comprehensive Error Handling

from tzst import extract_archive, TzstError

try:
    extract_archive("backup.tzst", "restore/")
except TzstError as e:
    # Catch any tzst-related error
    print(f"Operation failed: {e}")
    # Perform cleanup or fallback operations

Specific Exception Handling

from tzst import TzstArchive, TzstDecompressionError, TzstArchiveError

def safe_extract(archive_path, output_dir):
    try:
        with TzstArchive(archive_path, "r") as archive:
            # Test integrity first
            if not archive.test():
                print("Archive integrity check failed")
                return False
            
            # Extract files
            archive.extractall(output_dir)
            return True
            
    except TzstDecompressionError as e:
        print(f"Archive is corrupted or invalid: {e}")
        return False
    except TzstArchiveError as e:
        print(f"Archive operation failed: {e}")
        return False
    except FileNotFoundError:        print(f"Archive file not found: {archive_path}")
        return False
    except PermissionError:
        print(f"Permission denied accessing: {archive_path}")
        return False

Logging Integration

import logging
from tzst import test_archive, TzstDecompressionError, TzstError

logger = logging.getLogger(__name__)

def verify_archive(archive_path):
    """Verify archive integrity with comprehensive logging."""
    try:
        if test_archive(archive_path):
            logger.info(f"Archive {archive_path} is valid")
            return True
    except TzstDecompressionError as e:
        logger.error(f"Archive {archive_path} is corrupted: {e}")
    except TzstError as e:
        logger.error(f"tzst error for {archive_path}: {e}")
    except Exception as e:
        logger.error(f"Unexpected error testing {archive_path}: {e}")
    
    return False

Error Recovery Patterns

from tzst import create_archive, extract_archive, TzstError
from pathlib import Path
import tempfile
import shutil

def robust_backup_and_restore(source_dir, backup_path, restore_dir):
    """Robust backup with error recovery and validation."""
    temp_backup = None
    
    try:
        # Create backup with temporary file for atomicity
        with tempfile.NamedTemporaryFile(suffix='.tzst', delete=False) as temp_file:
            temp_backup = Path(temp_file.name)
        
        # Create archive
        create_archive(temp_backup, [source_dir], compression_level=6)
        
        # Verify archive before moving to final location
        if not test_archive(temp_backup):
            raise TzstArchiveError("Created archive failed integrity check")
        
        # Move to final location atomically
        shutil.move(temp_backup, backup_path)
        temp_backup = None  # Successfully moved
        
        # Test restoration
        extract_archive(backup_path, restore_dir)
        
        print(f"Backup and restore completed successfully")
        return True
        
    except TzstError as e:
        print(f"tzst operation failed: {e}")
        # Cleanup and recovery logic
        if restore_dir.exists():
            shutil.rmtree(restore_dir)
        return False
          except Exception as e:
        print(f"Unexpected error: {e}")
        return False
        
    finally:
        # Cleanup temporary files
        if temp_backup and temp_backup.exists():
            temp_backup.unlink()