Skip to main content

pdf_cos/
error.rs

1use thiserror::Error;
2
3use crate::encodings::cmap::UnicodeCMapError;
4use crate::{ObjectId, encryption};
5
6pub type Result<T> = std::result::Result<T, Error>;
7
8#[derive(Debug, Error)]
9pub enum Error {
10    /// Lopdf does not (yet) implement a needed feature.
11    #[error(
12        "missing feature of lopdf: {0}; please open an issue at https://github.com/J-F-Liu/lopdf/ to let the developers know of your usecase"
13    )]
14    Unimplemented(&'static str),
15
16    /// An Object has the wrong type, e.g. the Object is an Array where a Name would be expected.
17    #[error("object has wrong type; expected type {expected} but found type {found}")]
18    ObjectType {
19        expected: &'static str,
20        found: &'static str,
21    },
22    #[error("dictionary has wrong type: ")]
23    DictType { expected: &'static str, found: String },
24    /// PDF document is already encrypted.
25    #[error("PDF document is already encrypted")]
26    AlreadyEncrypted,
27    /// The encountered character encoding is invalid.
28    #[error("invalid character encoding")]
29    CharacterEncoding,
30    /// The stream couldn't be decompressed.
31    #[error("couldn't decompress stream {0}")]
32    Decompress(#[from] DecompressError),
33    /// Failed to parse input.
34    #[error("couldn't parse input: {0}")]
35    Parse(#[from] ParseError),
36    /// Error when decrypting the contents of the file
37    #[error("decryption error: {0}")]
38    Decryption(#[from] encryption::DecryptionError),
39    /// Dictionary key was not found.
40    #[error("missing required dictionary key \"{0}\"")]
41    DictKey(String),
42    /// Invalid inline image.
43    #[error("invalid inline image: {0}")]
44    InvalidInlineImage(String),
45    /// Invalid document outline.
46    #[error("invalid document outline: {0}")]
47    InvalidOutline(String),
48    /// Invalid stream.
49    #[error("invalid stream: {0}")]
50    InvalidStream(String),
51    /// Invalid object stream.
52    #[error("invalid object stream: {0}")]
53    InvalidObjectStream(String),
54    /// Byte offset in stream or file is invalid.
55    #[error("invalid byte offset")]
56    InvalidOffset(usize),
57    /// IO error
58    #[error("IO error: {0}")]
59    IO(#[from] std::io::Error),
60    // TODO: Maybe remove, as outline is not required in spec.
61    /// PDF document has no outline.
62    #[error("PDF document does not have an outline")]
63    NoOutline,
64    /// PDF document is not encrypted.
65    #[error("PDF document is not encrypted")]
66    NotEncrypted,
67    /// Invalid password provided for encrypted PDF.
68    #[error("invalid password for encrypted PDF")]
69    InvalidPassword,
70    /// Missing xref entry.
71    #[error("missing xref entry")]
72    MissingXrefEntry,
73    /// The Object ID was not found.
74    #[error("object ID {} {} not found", .0.0, .0.1)]
75    ObjectNotFound(ObjectId),
76    /// Dereferencing object failed due to a reference cycle.
77    #[error("reference cycle with object ID {} {}", .0.0, .0.1)]
78    ReferenceCycle(ObjectId),
79    /// Page number was not found in document.
80    #[error("page number not found")]
81    PageNumberNotFound(u32),
82    /// Numeric type cast failed.
83    #[error("numberic type cast failed: {0}")]
84    NumericCast(String),
85    /// Dereferencing object reached the limit.
86    /// This might indicate a reference loop.
87    #[error("dereferencing object reached limit, may indicate a reference cycle")]
88    ReferenceLimit,
89    /// Decoding text string failed.
90    #[error("decoding text string failed")]
91    TextStringDecode,
92    /// Error while parsing cross reference table.
93    #[error("failed parsing cross reference table: {0}")]
94    Xref(XrefError),
95    /// Invalid indirect object while parsing at offset.
96    #[error("invalid indirect object at byte offset {offset}")]
97    IndirectObject { offset: usize },
98    /// Found object ID does not match expected object ID.
99    #[error("found object ID does not match expected object ID")]
100    ObjectIdMismatch,
101    /// Error when handling images.
102    #[cfg(feature = "embed_image")]
103    #[error("image error: {0}")]
104    Image(#[from] image::ImageError),
105    /// Syntax error while processing the content stream.
106    #[error("syntax error in content stream: {0}")]
107    Syntax(String),
108    /// Could not parse ToUnicodeCMap.
109    #[error("failed parsing ToUnicode CMap: {0}")]
110    ToUnicodeCMap(#[from] UnicodeCMapError),
111    #[error("converting integer: {0}")]
112    TryFromInt(#[from] std::num::TryFromIntError),
113    /// Encountered an unsupported security handler.
114    #[error("unsupported security handler")]
115    UnsupportedSecurityHandler(Vec<u8>),
116}
117
118#[derive(Error, Debug)]
119pub enum DecompressError {
120    #[error("decoding ASCII85 failed: {0}")]
121    Ascii85(&'static str),
122}
123
124#[derive(Error, Debug)]
125pub enum ParseError {
126    #[error("unexpected end of input")]
127    EndOfInput,
128    #[error("invalid content stream")]
129    InvalidContentStream,
130    #[error("invalid file header")]
131    InvalidFileHeader,
132    #[error("invalid file trailer")]
133    InvalidTrailer,
134    #[error("invalid cross reference table")]
135    InvalidXref,
136}
137
138#[derive(Debug, Error)]
139pub enum XrefError {
140    /// Could not find start of cross reference table.
141    #[error("invalid start value")]
142    Start,
143    /// The trailer's "Prev" field was invalid.
144    #[error("invalid start value in Prev field")]
145    PrevStart,
146    /// The trailer's "XRefStm" field was invalid.
147    #[error("invalid start value of XRefStm")]
148    StreamStart,
149}