html2pdf_secure/
error.rs

1//! Error types for the html2pdf-secure crate.
2
3use thiserror::Error;
4
5/// Result type alias for this crate.
6pub type Result<T> = std::result::Result<T, Html2PdfError>;
7
8/// Main error type for HTML to PDF conversion and encryption operations.
9#[derive(Error, Debug)]
10pub enum Html2PdfError {
11    /// Chrome/browser related errors
12    #[error("Chrome browser error: {0}")]
13    ChromeError(String),
14
15    /// PDF manipulation errors
16    #[error("PDF error: {0}")]
17    PdfError(#[from] lopdf::Error),
18
19    /// IO errors
20    #[error("IO error: {0}")]
21    IoError(#[from] std::io::Error),
22
23    /// Encryption errors
24    #[error("Encryption error: {0}")]
25    EncryptionError(String),
26
27    /// Invalid input errors
28    #[error("Invalid input: {0}")]
29    InvalidInput(String),
30
31    /// Configuration errors
32    #[error("Configuration error: {0}")]
33    ConfigError(String),
34
35    /// Timeout errors
36    #[error("Operation timed out: {0}")]
37    TimeoutError(String),
38
39    /// Generic errors
40    #[error("Error: {0}")]
41    Other(#[from] anyhow::Error),
42}
43
44impl Html2PdfError {
45    /// Create a new chrome error.
46    pub fn chrome<S: Into<String>>(msg: S) -> Self {
47        Self::ChromeError(msg.into())
48    }
49
50    /// Create a new encryption error.
51    pub fn encryption<S: Into<String>>(msg: S) -> Self {
52        Self::EncryptionError(msg.into())
53    }
54
55    /// Create a new invalid input error.
56    pub fn invalid_input<S: Into<String>>(msg: S) -> Self {
57        Self::InvalidInput(msg.into())
58    }
59
60    /// Create a new configuration error.
61    pub fn config<S: Into<String>>(msg: S) -> Self {
62        Self::ConfigError(msg.into())
63    }
64
65    /// Create a new timeout error.
66    pub fn timeout<S: Into<String>>(msg: S) -> Self {
67        Self::TimeoutError(msg.into())
68    }
69}
70
71// Note: headless_chrome uses anyhow::Error internally,
72// so we handle conversions manually when needed