Exception Classes
EPUBime provides a complete exception handling system, with all exceptions inheriting from the BaseEpubException base class. EPUBime also provides enhanced error handling features, including error code systems, detailed error context collection, and flexible error recovery strategies.
Inheritance Hierarchy
Exception
└── BaseEpubException
├── EpubParseException (Parse Exception)
│ └── EpubXmlParseException (XML Parse Exception)
├── EpubFormatException (Format Exception)
├── EpubPathValidationException (Path Validation Exception)
├── EpubResourceException (Resource Exception)
└── EpubZipException (ZIP Exception)Enhanced Error Handling Features
Error Code System
EPUBime introduces a detailed error code system to help developers quickly locate problems:
public enum ErrorCode {
// File related errors (1000-1999)
FILE_NOT_FOUND(1001, "File not found"),
FILE_ACCESS_DENIED(1002, "File access denied"),
FILE_CORRUPTED(1003, "File appears to be corrupted"),
INVALID_FILE_FORMAT(1004, "Invalid file format"),
// ZIP related errors (2000-2999)
ZIP_INVALID(2001, "Invalid ZIP file format"),
ZIP_ENTRY_NOT_FOUND(2002, "ZIP entry not found"),
ZIP_DECOMPRESSION_FAILED(2003, "ZIP decompression failed"),
// XML parsing errors (3000-3999)
XML_PARSE_ERROR(3001, "XML parsing failed"),
XML_INVALID_STRUCTURE(3002, "Invalid XML structure"),
XML_MISSING_REQUIRED_ELEMENT(3003, "Missing required XML element"),
XML_INVALID_ATTRIBUTE(3004, "Invalid XML attribute"),
// EPUB format errors (4000-4999)
EPUB_INVALID_CONTAINER(4001, "Invalid EPUB container"),
EPUB_MISSING_MIMETYPE(4002, "Missing mimetype file"),
EPUB_INVALID_OPF(4003, "Invalid OPF file"),
EPUB_INVALID_NCX(4004, "Invalid NCX file"),
EPUB_INVALID_NAV(4005, "Invalid NAV file"),
// Path validation errors (5000-5999)
PATH_TRAVERSAL_ATTACK(5001, "Path traversal attack detected"),
PATH_INVALID_CHARACTER(5002, "Invalid character in path"),
PATH_TOO_LONG(5003, "Path too long"),
// Resource related errors (6000-6999)
RESOURCE_NOT_FOUND(6001, "Resource not found"),
RESOURCE_LOAD_FAILED(6002, "Resource loading failed"),
RESOURCE_INVALID_TYPE(6003, "Invalid resource type"),
// General errors (9000-9999)
UNKNOWN_ERROR(9001, "Unknown error occurred"),
OPERATION_NOT_SUPPORTED(9002, "Operation not supported"),
INTERNAL_ERROR(9003, "Internal error");
}Exception Builder Pattern
EPUBime provides a flexible builder pattern to create exceptions:
EpubParseException exception = new EpubParseException.Builder()
.message("Custom error message")
.fileName("book.epub")
.filePath("/path/to/book.epub")
.operation("metadataParsing")
.errorCode(EpubParseException.ErrorCode.XML_PARSE_ERROR)
.lineNumber(10)
.columnNumber(5)
.addContext("customKey", "customValue")
.recoverySuggestion("Custom recovery suggestion")
.build();Error Context Collection
EPUBime provides the ErrorContext class to collect errors, warnings, and debug information during the parsing process:
// Create error context
ErrorContext errorContext = new ErrorContext(100, ParseOptions.LogLevel.DEBUG);
// Record different level information
errorContext.debug("Debug information", "book.epub", "/path", "debugOp", null);
errorContext.info("General information", "book.epub", "/path", "infoOp", null);
errorContext.warning("Warning information", "book.epub", "/path", "warningOp",
EpubParseException.ErrorCode.XML_INVALID_STRUCTURE, null);
errorContext.error("Error information", "book.epub", "/path", "errorOp",
EpubParseException.ErrorCode.FILE_NOT_FOUND, null, null);
errorContext.fatal("Fatal error", "book.epub", "/path", "fatalOp",
EpubParseException.ErrorCode.EPUB_INVALID_CONTAINER, null, null);
// Generate error report
String report = errorContext.generateReport();
System.out.println(report);
// Get error statistics
ErrorContext.ErrorStatistics stats = errorContext.getStatistics();Parse Options Configuration
Through the ParseOptions class, you can configure the error handling behavior during the parsing process:
// Strict mode (default)
ParseOptions strictOptions = ParseOptions.strict();
// Lenient mode
ParseOptions lenientOptions = ParseOptions.lenient();
// Best effort mode
ParseOptions bestEffortOptions = ParseOptions.bestEffort();
// Custom configuration
ParseOptions customOptions = ParseOptions.lenient()
.withErrorHandlingStrategy(ParseOptions.ErrorHandlingStrategy.LENIENT)
.withLogLevel(ParseOptions.LogLevel.WARNING)
.withCollectWarnings(true)
.withContinueOnMetadataError(true)
.withContinueOnNavigationError(true)
.withContinueOnResourceError(true)
.withSkipInvalidResources(true)
.withUseFallbackMetadata(true)
.withIgnoredErrorPattern("optional")
.withIgnoredErrorPattern("missing");Parse Result Handling
The ParseResult class encapsulates the parsing results, including the parsed book object and error information:
ParseResult result = parser.parseWithOptions(file, options);
// Check parsing status
if (result.isSuccess()) {
EpubBook book = result.getEpubBook();
// Process successfully parsed book
} else if (result.isPartialSuccess()) {
EpubBook book = result.getEpubBook();
ErrorContext errorContext = result.getErrorContext();
// Process partially successful parsing results
} else if (result.isFailure()) {
ErrorContext errorContext = result.getErrorContext();
// Handle parsing failure
}
// Get detailed information
System.out.println("Parse Status: " + result.getStatus());
System.out.println("Parse Time: " + result.getParseTimeMs() + "ms");
System.out.println("Parse Summary: " + result.getParseSummary());BaseEpubException
BaseEpubException is the base class for all EPUB-related exceptions.
Constructor
public BaseEpubException(String message)Creates a new BaseEpubException instance.
Parameters:
message: Exception message
public BaseEpubException(String message, Throwable cause)Creates a new BaseEpubException instance.
Parameters:
message: Exception messagecause: Original exception
public BaseEpubException(Throwable cause)Creates a new BaseEpubException instance.
Parameters:
cause: Original exception
EpubParseException
Parse exception, used to handle errors during EPUB file parsing.
Constructor
public EpubParseException(String message)Creates a new EpubParseException instance.
Parameters:
message: Exception message
public EpubParseException(String message, ErrorCode errorCode)Creates a new EpubParseException instance with error code.
Parameters:
message: Exception messageerrorCode: Error code
public EpubParseException(String message, Throwable cause)Creates a new EpubParseException instance with original exception.
Parameters:
message: Exception messagecause: Original exception
public EpubParseException(String message, ErrorCode errorCode, Throwable cause)Creates a new EpubParseException instance with error code and original exception.
Parameters:
message: Exception messageerrorCode: Error codecause: Original exception
public EpubParseException(String message, String fileName, String filePath, String operation, Throwable cause)Creates a new EpubParseException instance with filename, path, operation type, and original exception.
Parameters:
message: Exception messagefileName: FilenamefilePath: File pathoperation: Operation typecause: Original exception
public EpubParseException(String message, String fileName, String filePath, String operation, ErrorCode errorCode, Throwable cause)Creates a new EpubParseException instance with filename, path, operation type, error code, and original exception.
Parameters:
message: Exception messagefileName: FilenamefilePath: File pathoperation: Operation typeerrorCode: Error codecause: Original exception
public EpubParseException(String message, String fileName, String filePath, String operation)Creates a new EpubParseException instance with filename, path, and operation type.
Parameters:
message: Exception messagefileName: FilenamefilePath: File pathoperation: Operation type
public EpubParseException(String message, String fileName, String filePath, String operation, ErrorCode errorCode)Creates a new EpubParseException instance with filename, path, operation type, and error code.
Parameters:
message: Exception messagefileName: FilenamefilePath: File pathoperation: Operation typeerrorCode: Error code
public EpubParseException(Builder builder)Creates a new EpubParseException instance using builder.
Parameters:
builder: Builder
Main Methods
getFileName()
public String getFileName()Gets the name of the related file.
Returns:
String: Filename
getFilePath()
public String getFilePath()Gets the path of the related file.
Returns:
String: File path
getOperation()
public String getOperation()Gets the operation type.
Returns:
String: Operation type
getErrorCode()
public ErrorCode getErrorCode()Gets the error code.
Returns:
ErrorCode: Error code
getErrorCodeValue()
public int getErrorCodeValue()Gets the error code value.
Returns:
int: Error code value
getRecoverySuggestion()
public String getRecoverySuggestion()Gets the recovery suggestion.
Returns:
String: Recovery suggestion
getLineNumber()
public int getLineNumber()Gets the line number.
Returns:
int: Line number, returns -1 if not applicable
getColumnNumber()
public int getColumnNumber()Gets the column number.
Returns:
int: Column number, returns -1 if not applicable
addContext()
public void addContext(String key, Object value)Adds context information.
Parameters:
key: Keyvalue: Value
getContext()
public Object getContext(String key)Gets context information.
Parameters:
key: Key
Returns:
Object: Value
getAllContext()
public Map<String, Object> getAllContext()Gets all context information.
Returns:
Map<String, Object>: Context map
getRootCause()
public Throwable getRootCause()Gets the root cause.
Returns:
Throwable: Root cause
EpubXmlParseException
XML parse exception, specifically for handling XML format errors.
Constructor
public EpubXmlParseException(String message, String fileName)Creates a new EpubXmlParseException instance.
Parameters:
message: Exception messagefileName: Filename
public EpubXmlParseException(String message, String fileName, int lineNumber, int columnNumber, Throwable cause)Creates a new EpubXmlParseException instance.
Parameters:
message: Exception messagefileName: FilenamelineNumber: Line numbercolumnNumber: Column numbercause: Original exception
public EpubXmlParseException(String message, String fileName, Throwable cause)Creates a new EpubXmlParseException instance.
Parameters:
message: Exception messagefileName: Filenamecause: Original exception
Main Methods
getFileName()
public String getFileName()Gets the filename.
Returns:
String: Filename
getLineNumber()
public int getLineNumber()Gets the line number.
Returns:
int: Line number
getColumnNumber()
public int getColumnNumber()Gets the column number.
Returns:
int: Column number
getFilePath()
public String getFilePath()Gets the file path (for backward compatibility).
Returns:
String: File path
getXPath()
public String getXPath()Gets the XPath (for backward compatibility).
Returns:
String: XPath, returns null in simplified design
getErrorCode()
public Object getErrorCode()Gets the error code (for backward compatibility).
Returns:
Object: Error code, returns null in simplified design
getOperation()
public String getOperation()Gets the operation type (for backward compatibility).
Returns:
String: Operation type
EpubFormatException
Format exception, used to handle cases where EPUB format does not comply with specifications.
Constructor
public EpubFormatException(String message, File file)Creates a new EpubFormatException instance.
Parameters:
message: Exception messagefile: File
public EpubFormatException(String message, File file, String details)Creates a new EpubFormatException instance.
Parameters:
message: Exception messagefile: Filedetails: Details
public EpubFormatException(String message, File file, String details, Throwable cause)Creates a new EpubFormatException instance.
Parameters:
message: Exception messagefile: Filedetails: Detailscause: Original exception
public EpubFormatException(String message, String fileName, String filePath)Creates a new EpubFormatException instance (for backward compatibility).
Parameters:
message: Exception messagefileName: FilenamefilePath: File path
public EpubFormatException(String message, String fileName, String filePath, Throwable cause)Creates a new EpubFormatException instance (for backward compatibility).
Parameters:
message: Exception messagefileName: FilenamefilePath: File pathcause: Original exception
Static Factory Methods
public static EpubFormatException missingMimetype(String fileName, String filePath)Creates an exception for missing mimetype file.
Parameters:
fileName: FilenamefilePath: File path
Returns:
EpubFormatException: Exception instance
public static EpubFormatException invalidContainer(String fileName, String filePath, String details)Creates an exception for invalid container.xml file.
Parameters:
fileName: FilenamefilePath: File pathdetails: Details
Returns:
EpubFormatException: Exception instance
public static EpubFormatException invalidOpf(String fileName, String filePath, String opfPath, String details)Creates an exception for invalid OPF file.
Parameters:
fileName: FilenamefilePath: File pathopfPath: OPF file pathdetails: Details
Returns:
EpubFormatException: Exception instance
public static EpubFormatException invalidNcx(String fileName, String filePath, String ncxPath, String details)Creates an exception for invalid NCX file.
Parameters:
fileName: FilenamefilePath: File pathncxPath: NCX file pathdetails: Details
Returns:
EpubFormatException: Exception instance
public static EpubFormatException invalidNav(String fileName, String filePath, String navPath, String details)Creates an exception for invalid NAV file.
Parameters:
fileName: FilenamefilePath: File pathnavPath: NAV file pathdetails: Details
Returns:
EpubFormatException: Exception instance
Main Methods
getFile()
public File getFile()Gets the file.
Returns:
File: File
getDetails()
public String getDetails()Gets the details.
Returns:
String: Details
getFileName()
public String getFileName()Gets the filename (for backward compatibility).
Returns:
String: Filename
getFilePath()
public String getFilePath()Gets the file path (for backward compatibility).
Returns:
String: File path
getOperation()
public String getOperation()Gets the operation type (for backward compatibility).
Returns:
String: Operation type
getErrorCode()
public Object getErrorCode()Gets the error code (for backward compatibility).
Returns:
Object: Error code
getRecoverySuggestion()
public String getRecoverySuggestion()Gets the recovery suggestion (for backward compatibility).
Returns:
String: Recovery suggestion
EpubPathValidationException
Path validation exception, used to prevent directory traversal attacks.
Constructor
public EpubPathValidationException(String message, String invalidPath)Creates a new EpubPathValidationException instance.
Parameters:
message: Exception messageinvalidPath: Invalid path
public EpubPathValidationException(String message, String invalidPath, Throwable cause)Creates a new EpubPathValidationException instance.
Parameters:
message: Exception messageinvalidPath: Invalid pathcause: Original exception
public EpubPathValidationException(String message, String invalidPath, String fileName, Throwable cause)Creates a new EpubPathValidationException instance.
Parameters:
message: Exception messageinvalidPath: Invalid pathfileName: Filenamecause: Original exception
Static Factory Methods
public static EpubPathValidationException createForCompatibility(String message, String fileName, String filePath)Creates a compatibility exception instance.
Parameters:
message: Exception messagefileName: FilenamefilePath: File path
Returns:
EpubPathValidationException: Exception instance
public static EpubPathValidationException createForCompatibility(String message, String fileName, String filePath, Throwable cause)Creates a compatibility exception instance.
Parameters:
message: Exception messagefileName: FilenamefilePath: File pathcause: Original exception
Returns:
EpubPathValidationException: Exception instance
Main Methods
getInvalidPath()
public String getInvalidPath()Gets the invalid path.
Returns:
String: Invalid path
getFileName()
public String getFileName()Gets the filename (for backward compatibility).
Returns:
String: Filename
getFilePath()
public String getFilePath()Gets the file path (for backward compatibility).
Returns:
String: File path
getOperation()
public String getOperation()Gets the operation type (for backward compatibility).
Returns:
String: Operation type
getErrorCode()
public ErrorCode getErrorCode()Gets the error code (for backward compatibility).
Returns:
ErrorCode: Error code
EpubResourceException
Resource exception, used to handle resource file access errors.
Constructor
public EpubResourceException(String message, File file)Creates a new EpubResourceException instance.
Parameters:
message: Exception messagefile: File
public EpubResourceException(String message, File file, String resourcePath)Creates a new EpubResourceException instance.
Parameters:
message: Exception messagefile: FileresourcePath: Resource path
public EpubResourceException(String message, File file, String resourceId, String resourcePath, Throwable cause)Creates a new EpubResourceException instance.
Parameters:
message: Exception messagefile: FileresourceId: Resource IDresourcePath: Resource pathcause: Original exception
public EpubResourceException(String message, String fileName, String resourcePath)Creates a new EpubResourceException instance (for backward compatibility).
Parameters:
message: Exception messagefileName: FilenameresourcePath: Resource path
public EpubResourceException(String message, String fileName, String resourcePath, Throwable cause)Creates a new EpubResourceException instance (for backward compatibility).
Parameters:
message: Exception messagefileName: FilenameresourcePath: Resource pathcause: Original exception
Main Methods
getFile()
public File getFile()Gets the file.
Returns:
File: File
getResourceId()
public String getResourceId()Gets the resource ID.
Returns:
String: Resource ID
getResourcePath()
public String getResourcePath()Gets the resource path.
Returns:
String: Resource path
getFileName()
public String getFileName()Gets the filename (for backward compatibility).
Returns:
String: Filename
getFilePath()
public String getFilePath()Gets the file path (for backward compatibility).
Returns:
String: File path
getOperation()
public String getOperation()Gets the operation type (for backward compatibility).
Returns:
String: Operation type
getErrorCode()
public Object getErrorCode()Gets the error code (for backward compatibility).
Returns:
Object: Error code
EpubZipException
ZIP exception, used to handle ZIP file operation errors.
Constructor
public EpubZipException(String message, File file)Creates a new EpubZipException instance.
Parameters:
message: Exception messagefile: File
public EpubZipException(String message, File file, Throwable cause)Creates a new EpubZipException instance.
Parameters:
message: Exception messagefile: Filecause: Original exception
public EpubZipException(String message, File file, String entryName, Throwable cause)Creates a new EpubZipException instance.
Parameters:
message: Exception messagefile: FileentryName: Entry namecause: Original exception
public EpubZipException(String message, String fileName, String filePath)Creates a new EpubZipException instance (for backward compatibility).
Parameters:
message: Exception messagefileName: FilenamefilePath: File path
public EpubZipException(String message, String fileName, String filePath, Throwable cause)Creates a new EpubZipException instance (for backward compatibility).
Parameters:
message: Exception messagefileName: FilenamefilePath: File pathcause: Original exception
Main Methods
getFile()
public File getFile()Gets the file.
Returns:
File: File
getEntryName()
public String getEntryName()Gets the entry name.
Returns:
String: Entry name
getFileName()
public String getFileName()Gets the filename (for backward compatibility).
Returns:
String: Filename
getFilePath()
public String getFilePath()Gets the file path (for backward compatibility).
Returns:
String: File path
getOperation()
public String getOperation()Gets the operation type (for backward compatibility).
Returns:
String: Operation type
getErrorCode()
public Object getErrorCode()Gets the error code (for backward compatibility).
Returns:
Object: Error code
Usage Example
try {
// Configure parse options
ParseOptions options = ParseOptions.lenient()
.withLogLevel(ParseOptions.LogLevel.INFO)
.withContinueOnMetadataError(true)
.withContinueOnResourceError(true);
// Parse EPUB with configuration options
EpubParser parser = new EpubParser(new File("book.epub"));
ParseResult result = parser.parseWithOptions(new File("book.epub"), options);
if (result.isSuccess() || result.isPartialSuccess()) {
EpubBook book = result.getEpubBook();
// Process successfully parsed book
} else {
// Handle parsing failure
System.err.println("Parsing failed: " + result.getParseSummary());
}
} catch (EpubParseException e) {
// Handle parse exception
System.err.println("Parse error: " + e.getMessage());
System.err.println("Error Code: " + e.getErrorCode());
System.err.println("Recovery Suggestion: " + e.getRecoverySuggestion());
e.printStackTrace();
} catch (EpubXmlParseException e) {
// Handle XML parse exception
System.err.println("XML parse error: " + e.getMessage());
System.err.println("File: " + e.getFileName());
System.err.println("Line Number: " + e.getLineNumber());
System.err.println("Column Number: " + e.getColumnNumber());
} catch (EpubFormatException e) {
// Handle format exception
System.err.println("Format error: " + e.getMessage());
System.err.println("File: " + e.getFileName());
System.err.println("Recovery Suggestion: " + e.getRecoverySuggestion());
} catch (EpubPathValidationException e) {
// Handle path validation exception
System.err.println("Path validation failed: " + e.getMessage() + ", Path: " + e.getFilePath());
} catch (EpubResourceException e) {
// Handle resource exception
System.err.println("Resource error: " + e.getMessage() + ", Path: " + e.getFilePath());
} catch (EpubZipException e) {
// Handle ZIP exception
System.err.println("ZIP error: " + e.getMessage() + ", File: " + e.getFileName());
} catch (Exception e) {
// Handle other exceptions
System.err.println("Unexpected error: " + e.getMessage());
e.printStackTrace();
}