#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum Error {
#[error("invalid base URL {base_url:?}: {source}")]
InvalidBaseUrl {
base_url: String,
#[source]
source: url::ParseError,
},
#[error(
"unsupported URL scheme {scheme:?} in {base_url:?}: \
GROBID servers are reached over http or https"
)]
UnsupportedScheme {
base_url: String,
scheme: String,
},
#[error("I/O error: {0}")]
Io(#[from] std::io::Error),
#[error("HTTP transport error: {0}")]
Http(#[from] reqwest::Error),
#[error("GROBID server at {url} returned HTTP {status}: {message}")]
HttpStatus {
status: u16,
url: String,
message: String,
},
#[error(
"GROBID server at {url} is up, but reports that it is not ready \
(/api/isalive returned false)"
)]
ServerNotReady {
url: String,
},
#[error("GROBID server at {url} did not respond in {attempts} attempt(s): {source}")]
ServerUnreachable {
url: String,
attempts: usize,
#[source]
source: reqwest::Error,
},
#[error("GROBID server is busy (HTTP 503/429) after {attempts} attempts")]
ServerBusy {
attempts: usize,
},
#[error("GROBID server returned no content (HTTP 204)")]
NoContent,
#[error("invalid XML: {0}")]
Xml(#[from] roxmltree::Error),
#[error("invalid TEI document: {0}")]
InvalidDocument(#[from] InvalidDocument),
#[error(
"invalid @coords value {value:?}: expected \"page,x,y,width,height\" \
bounding boxes separated by ';'"
)]
InvalidCoords {
value: String,
},
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, thiserror::Error)]
#[non_exhaustive]
pub enum InvalidDocument {
#[error("root element is not <TEI>")]
NotTei,
#[error("missing <teiHeader>")]
MissingTeiHeader,
#[error("missing <appInfo><application>")]
MissingApplication,
}
impl Error {
pub(crate) fn http_status(status: u16, url: impl Into<String>, mut message: String) -> Self {
const MAX_MESSAGE_LEN: usize = 500;
if message.len() > MAX_MESSAGE_LEN {
message.truncate(MAX_MESSAGE_LEN);
message.push('…');
}
let message = message.trim().to_string();
Self::HttpStatus {
status,
url: url.into(),
message,
}
}
}