Handling Errors
Tell why a conversion failed and respond to each error.
On this page
Conversions return a laravel::Result, and each of the package's errors carries its HTTP status. Reach laravel_anydoc::Error with downcast_ref to handle a failure yourself.
Conversion errors
A document that cannot be converted fails with laravel_anydoc::Error::Convert, which responds with 422 Unprocessable Content. It wraps anydoc's ConvertError, which says why: an unsupported or unknown format, a PDF that needs OCR, or a document that is malformed, encrypted, over one of anydoc's resource limits, or missing a part.
Errors reading a file or an upload, such as a missing file, and storage errors pass through unchanged.
Documents over the size limit
A document over ANYDOC_MAX_FILE_SIZE fails with laravel_anydoc::Error::TooLarge, which responds with 413 Content Too Large and carries the limit. Configuration explains how the package measures each source.
OCR errors
When Firecrawl fails, the conversion fails with laravel_anydoc::Error::Ocr, which responds with 502 Bad Gateway. OCR lists the causes.
Handling an error
Downcast the error to laravel_anydoc::Error and match the cases you handle:
use laravel::prelude::*;
use laravel_anydoc::anydoc::ConvertError;
use laravel_anydoc::{Anydoc, Error};
pub async fn import(anydoc: Inject<Anydoc>) -> laravel::Result<String> {
let error = match anydoc.file("storage/app/contract.docx").markdown().await {
Ok(markdown) => return Ok(markdown),
Err(error) => error,
};
match error.downcast_ref::<Error>() {
Some(Error::Convert(ConvertError::Encrypted)) => Ok(String::from("The contract is encrypted.")),
Some(Error::TooLarge { limit }) => Ok(format!("Send a document of at most {limit} bytes.")),
_ => Err(error),
}
}