lopdf/
error.rs

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