leptos_pdf/
errors.rs

1//! Error types used by [`leptos-pdf`](crate).
2
3use thiserror::Error;
4use wasm_bindgen::{JsCast, JsValue};
5use web_sys::js_sys;
6
7/// Errors that can occur while initializing PDFium, or loading, rendering, or processing a
8/// PDF document.
9#[derive(Debug, Error)]
10pub enum PdfError {
11    /// Failed to load or open the PDF document.
12    ///
13    /// This typically occurs when the PDF bytes are invalid, corrupted, or when an
14    /// incorrect password is provided for an encrypted document.
15    #[error("Failed to load PDF: {0}")]
16    LoadingError(String),
17    /// Failed while rendering a PDF page into a raster image.
18    ///
19    /// This may be caused by invalid page dimensions, rendering configuration issues,
20    /// or internal PDFium errors.
21    #[error("Failed to render PDF: {0}")]
22    RenderError(String),
23    /// Failed to extract text from a PDF page.
24    ///
25    /// This can occur if PDFium encounters an internal error during text processing.
26    #[error("Failed to extract text from PDF: {0}")]
27    TextExtractionError(String),
28    #[error("Failed to initialize PDFium: {0}")]
29    PdfiumInitError(String),
30}
31
32pub fn js_error_string_or(value: JsValue, custom: String) -> String {
33    value
34        .dyn_into::<js_sys::Error>()
35        .ok()
36        .and_then(|e| e.to_string().as_string())
37        .unwrap_or(custom)
38}
39
40/// Errors that can occur while fetching a remote PDF.
41#[derive(Debug, Error)]
42pub enum FetchError {
43    /// Failed to fetch a remote binary blob (such as a PDF file).
44    ///
45    /// This may be caused by network errors, CORS issues, invalid URLs,
46    /// or failures in the browser `fetch` API.
47    #[error("Failed to fetch blob: {0}")]
48    FailedToFetch(String),
49}