pdfium_render/error.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
//! Defines the [PdfiumError] enum, used to wrap Pdfium errors as `Err` values.
use crate::bindgen::{
FPDF_ERR_FILE, FPDF_ERR_FORMAT, FPDF_ERR_PAGE, FPDF_ERR_PASSWORD, FPDF_ERR_SECURITY,
FPDF_ERR_UNKNOWN,
};
use std::error::Error;
use std::ffi::IntoStringError;
use std::fmt::{Display, Formatter, Result};
use std::num::ParseIntError;
#[cfg(target_arch = "wasm32")]
use wasm_bindgen::JsValue;
/// A wrapped internal library error from Pdfium's `FPDF_ERR_*` constant values.
///
/// Pdfium only provides detailed internal error information for document loading functions.
/// All other functions in the Pdfium API return a value indicating success or failure,
/// but otherwise detailed error information for failed API calls is not available. In these
/// cases, an error value of [PdfiumInternalError::Unknown] will be returned.
// For more information, see: https://github.com/ajrcarey/pdfium-render/issues/78
#[derive(Debug)]
pub enum PdfiumInternalError {
/// The document could not be loaded due to a file system error.
FileError = FPDF_ERR_FILE as isize,
/// The document could not be loaded due to a format parsing error.
FormatError = FPDF_ERR_FORMAT as isize,
/// The document could not be loaded because the wrong password was supplied.
PasswordError = FPDF_ERR_PASSWORD as isize,
/// The document could not be loaded because of the document's security settings.
SecurityError = FPDF_ERR_SECURITY as isize,
/// The page could not be loaded due to an internal error.
PageError = FPDF_ERR_PAGE as isize,
/// A generic error value returned in all other unhandled situations.
Unknown = FPDF_ERR_UNKNOWN as isize,
}
/// A wrapper enum for handling Pdfium errors as standard Rust `Err` values.
#[derive(Debug)]
pub enum PdfiumError {
/// The Pdfium WASM module has not been configured.
/// It is essential that the exported `initialize_pdfium_render()` function be called
/// from Javascript _before_ calling any `pdfium-render` function from within your Rust code.
/// See: <https://github.com/ajrcarey/pdfium-render/blob/master/examples/index.html>
#[cfg(target_arch = "wasm32")]
PdfiumWASMModuleNotConfigured,
/// An error occurred during dynamic binding to an external Pdfium library.
#[cfg(not(target_arch = "wasm32"))]
LoadLibraryError(libloading::Error),
/// An error occurred during dynamic binding while converting an FPDF_* function name
/// to a C string. The wrapped string value contains more information.
#[cfg(not(target_arch = "wasm32"))]
LoadLibraryFunctionNameError(String),
UnrecognizedPath,
PageIndexOutOfBounds,
LinkIndexOutOfBounds,
UnknownBitmapFormat,
UnknownBitmapRotation,
UnknownFormType,
UnknownFormFieldType,
UnknownActionType,
UnknownAppearanceMode,
PageObjectIndexOutOfBounds,
PageAnnotationIndexOutOfBounds,
OwnershipNotAttachedToDocument,
OwnershipNotAttachedToPage,
OwnershipAlreadyAttachedToDifferentPage,
OwnershipNotAttachedToAnnotation,
FormFieldOptionIndexOutOfBounds,
FormFieldAppearanceStreamUndefined,
PageFlattenFailure,
PageMissingEmbeddedThumbnail,
UnknownPdfPageObjectType,
UnknownPdfPageTextRenderMode,
UnknownPdfPagePathFillMode,
UnknownPdfAnnotationType,
UnknownPdfDestinationViewType,
UnknownPdfSecurityHandlerRevision,
UnknownPdfSignatureModificationDetectionPermissionLevel,
UnsupportedPdfPageObjectType,
TextSegmentIndexOutOfBounds,
CharIndexOutOfBounds,
NoCharsInPageObject,
NoCharsInAnnotation,
NoCharsInRect,
ImageObjectFilterIndexOutOfBounds,
ImageObjectFilterIndexInBoundsButFilterUndefined,
UnknownPdfColorSpace,
InvalidTransformationMatrix,
SignatureIndexOutOfBounds,
AttachmentIndexOutOfBounds,
NoDataInAttachment,
FontGlyphIndexOutOfBounds,
UnknownPathSegmentType,
NoPagesInDocument,
NoPageObjectsInCollection,
NoPageLinksInCollection,
NoAnnotationsInCollection,
PageObjectNotCopyable,
ImageObjectFiltersNotCopyable,
PathObjectBezierControlPointsNotCopyable,
PathObjectUnknownSegmentTypeNotCopyable,
GroupContainsNonCopyablePageObjects,
SourcePageIndexNotInCache,
NoUriForAction,
DestinationPageIndexNotAvailable,
DestinationPageLocationNotAvailable,
PageAnnotationAttachmentPointIndexOutOfBounds,
NoAttachmentPointsInPageAnnotation,
CoordinateConversionFunctionIndicatedError,
/// A call to `FPDFDest_GetView()` returned a valid `FPDFDEST_VIEW_*` value, but the number
/// of view parameters returned does not match the PDF specification.
PdfDestinationViewInvalidParameters,
/// A [ParseIntError] occurred while attempting to parse a `PdfColor` from a hexadecimal string
/// in `PdfColor::from_hex()`.
ParseHexadecimalColorError(ParseIntError),
/// The hexadecimal string given to `PdfColor::from_hex()` was not either exactly 7 or 9
/// characters long.
ParseHexadecimalColorUnexpectedLength,
/// The leading `#` character was not found while attempting to parse a `PdfColor` from
/// a hexadecimal string in `PdfColor::from_hex()`.
ParseHexadecimalColorMissingLeadingHash,
/// An error occurred converting a byte stream into a `CString`.
CStringConversionError(IntoStringError),
/// Two data buffers are expected to have the same size, but they do not.
DataBufferLengthMismatch,
/// The setting cannot be returned because this `PdfPageGroupObject` is empty.
EmptyPageObjectGroup,
/// A call to a internal Pdfium `FPDF_*` function returned a value indicating failure.
///
/// For Pdfium functions that return enumerations, this means the function returned
/// a value of -1 rather than a valid enumeration constant.
///
/// For Pdfium functions that return C-style boolean integers, this means that the function
/// returned a value other than `PdfiumLibraryBindings::TRUE`.
PdfiumFunctionReturnValueIndicatedFailure,
/// A call to a Pdfium function that returns a standard 8-bit color component value
/// (for example, `FPDFPageObj_GetStrokeColor()` and `FPDFPageObj_GetStrokeColor()`)
/// successfully returned a value, but the value could not be converted from a c_int
/// to a standard Rust u8.
UnableToConvertPdfiumColorValueToRustu8(std::num::TryFromIntError),
/// The browser's built-in `Window` object could not be retrieved.
WebSysWindowObjectNotAvailable,
#[cfg(target_arch = "wasm32")]
/// A JsValue returned from a function call was set to JsValue::UNDEFINED instead of
/// a valid value of the expected type.
JsValueUndefined,
#[cfg(target_arch = "wasm32")]
/// An error was returned when attempting to use the browser's built-in `fetch()` API.
WebSysFetchError(JsValue),
#[cfg(target_arch = "wasm32")]
/// An invalid Response object was returned when attempting to use the browser's built-in `fetch()` API.
WebSysInvalidResponseError,
#[cfg(target_arch = "wasm32")]
/// An error was returned when attempting to construct a `Blob` object from a byte buffer.
JsSysErrorConstructingBlobFromBytes,
#[cfg(target_arch = "wasm32")]
/// An error occurred when attempting to retrieve the function table for the compiled
/// Pdfium WASM module.
JsSysErrorRetrievingFunctionTable(JsValue),
#[cfg(target_arch = "wasm32")]
/// An error occurred when attempting to retrieve an exported function from
/// `pdfium-render`'s WASM module.
JsSysErrorRetrievingFunction(JsValue),
#[cfg(target_arch = "wasm32")]
/// An error occurred when attempting to update an entry in Pdfium's WASM function table.
JsSysErrorPatchingFunctionTable(JsValue),
#[cfg(target_arch = "wasm32")]
/// No previously cached function was available for a WASM function table restore operation.
///
/// This error should never occur; if it does, it indicates a programming error in pdfium-render.
/// Please file an issue: https://github.com/ajrcarey/pdfium-render/issues
NoPreviouslyCachedFunctionSet,
/// An error occurred during an image processing operation.
ImageError,
/// Dimensions of `Image::Image` are specified in `u32`, but bitmaps in Pdfium are sized in
/// `c_int` (`i32`), meaning that an `Image::Image` can have dimensions that overflow
/// the maximum size of a Pdfium bitmap. As a compromise, Image dimensions in `pdfium-render`
/// are limited to `u16`.
///
/// This error indicates that an `Image::Image` had a width or height larger than the maximum
/// `u16` size allowed by `pdfium-render`.
ImageSizeOutOfBounds,
/// An I/O error occurred during a Pdfium file operation.
IoError(std::io::Error),
/// A wrapped internal library error from Pdfium's `FPDF_ERR_*` constant values.
PdfiumLibraryInternalError(PdfiumInternalError),
}
impl Display for PdfiumError {
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
write!(f, "{:#?}", self)
}
}
impl Error for PdfiumError {}