Skip to main content

rpdfium_page/
error.rs

1// Derived from PDFium's cpdf_page.cpp error handling
2// Original: Copyright 2014 The PDFium Authors
3// Licensed under BSD-3-Clause / Apache-2.0
4// See pdfium-upstream/LICENSE for the original license.
5
6//! Error types for the rpdfium-page crate.
7
8use rpdfium_core::Name;
9
10/// Errors that can occur during page interpretation and traversal.
11#[derive(Debug, thiserror::Error)]
12pub enum PageError {
13    /// The content stream contains too many operators.
14    #[error("too many operators in content stream")]
15    TooManyOperators,
16
17    /// The graphics state stack exceeded the maximum nesting depth.
18    #[error("graphics state stack overflow (depth limit exceeded)")]
19    StateStackOverflow,
20
21    /// A `Q` (RestoreState) was issued with an empty graphics state stack.
22    #[error("graphics state stack underflow (unmatched Q)")]
23    StateStackUnderflow,
24
25    /// The page tree exceeds the maximum depth or is circular.
26    #[error("page tree too deep or circular")]
27    PageTreeTooDeep,
28
29    /// A required resource was not found.
30    #[error("resource not found: {name}")]
31    ResourceNotFound {
32        /// The name of the missing resource.
33        name: Name,
34    },
35
36    /// The page tree root is missing or malformed.
37    #[error("missing or invalid page tree root")]
38    InvalidPageTree,
39
40    /// Form XObject nesting exceeds the maximum depth.
41    #[error("form XObject nesting too deep")]
42    FormXObjectTooDeep,
43
44    /// An underlying PDF error occurred.
45    #[error(transparent)]
46    Pdf(#[from] rpdfium_core::PdfError),
47}
48
49#[cfg(test)]
50mod tests {
51    use super::*;
52
53    #[test]
54    fn test_page_error_display() {
55        let err = PageError::TooManyOperators;
56        assert_eq!(format!("{err}"), "too many operators in content stream");
57    }
58
59    #[test]
60    fn test_page_error_from_pdf_error() {
61        let pdf_err = rpdfium_core::PdfError::RecursionLimitExceeded;
62        let page_err: PageError = pdf_err.into();
63        assert!(matches!(page_err, PageError::Pdf(_)));
64    }
65
66    #[test]
67    fn test_page_error_is_send_sync() {
68        fn assert_send_sync<T: Send + Sync>() {}
69        assert_send_sync::<PageError>();
70    }
71
72    #[test]
73    fn test_resource_not_found_display() {
74        let err = PageError::ResourceNotFound {
75            name: Name::from("F1"),
76        };
77        assert!(format!("{err}").contains("F1"));
78    }
79}