use thiserror::Error;
#[derive(Error, Debug)]
pub enum WebDynproError {
#[error("Error in client request/response: {0}")]
Client(#[from] Box<ClientError>),
#[error("Error in parsing document body: {0}")]
Body(#[from] Box<BodyError>),
#[error("Error in updating document body from server response: {0}")]
UpdateBody(#[from] Box<UpdateBodyError>),
#[error("Error in parse or construct event of element: {0}")]
Element(#[from] Box<ElementError>),
}
impl From<ClientError> for WebDynproError {
fn from(err: ClientError) -> Self {
WebDynproError::Client(Box::new(err))
}
}
impl From<BodyError> for WebDynproError {
fn from(err: BodyError) -> Self {
WebDynproError::Body(Box::new(err))
}
}
impl From<UpdateBodyError> for WebDynproError {
fn from(err: UpdateBodyError) -> Self {
WebDynproError::UpdateBody(Box::new(err))
}
}
impl From<ElementError> for WebDynproError {
fn from(err: ElementError) -> Self {
WebDynproError::Element(Box::new(err))
}
}
#[derive(Error, Debug)]
pub enum ClientError {
#[error("Failed to request from web: {0}")]
FailedRequest(String),
#[error("Failed to parse HTML body: {0}")]
Parse(#[from] BodyError),
#[error("Request is made, but failed: {0}")]
InvalidResponse(String),
#[error("Failed to parse base url: {0}")]
ParseBaseUrl(#[from] url::ParseError),
#[error("Server's update response is invalid: {0}")]
InvalidUpdate(#[from] UpdateBodyError),
#[error("Given base url is not valid: {0}")]
InvalidBaseUrl(String),
#[error("No form {0} found in desired application")]
NoSuchForm(String),
#[error("No cookie found: {0}")]
NoSuchCookie(String),
#[error("Empty cookie store for given url: {0}")]
NoCookies(String),
#[error("Network error: {0}")]
NetworkError(String),
}
#[derive(Error, Debug)]
pub enum UpdateBodyError {
#[error("Failed to parse update document: {0}")]
Parse(#[from] roxmltree::Error),
#[error("Cannot find a node from given document: {0}")]
NoSuchNode(String),
#[error("Cannot find an attribute {attribute:?} from a node {node:?}")]
#[allow(missing_docs)]
NoSuchAttribute { node: String, attribute: String },
#[error("{0} has no content")]
NoSuchContent(String),
#[error("Unknown element found: {0}")]
UnknownElement(String),
#[error("Failed to rewrite body document: {0}")]
RewriteBody(#[from] lol_html::errors::RewritingError),
}
#[derive(Error, Debug)]
pub enum BodyError {
#[error("Failed to parse body document")]
Parse,
#[error("Given body document is invalid: {0}")]
Invalid(String),
#[error("Given selector for parsing body is invalid")]
InvalidSelector,
#[error("Invalid element")]
InvalidElement,
#[error("Cannot find element from document: {0}")]
NoSuchElement(String),
#[error("Cannot find attribute: {0}")]
NoSuchAttribute(String),
#[error("Cannot parse event str: {0}")]
ParseEvents(#[from] EventStrUnescapeError),
}
#[allow(missing_docs)]
#[derive(Error, Debug)]
pub enum ElementError {
#[error("Cannot find data {field} in element: {element}")]
NoSuchData { element: String, field: String },
#[error("Cannot fire event {event} in element: {element}")]
NoSuchEvent { element: String, event: String },
#[error("Cannot find content {content} in element: {element}")]
NoSuchContent { element: String, content: String },
#[error("Invalid id {0}")]
InvalidId(String),
#[error("Invalid content {content} in element: {element}")]
InvalidContent { element: String, content: String },
#[error("Cannot parse lsdata: {0}")]
InvalidLSData(String),
#[error("Failed parse lsdata json-like object")]
ParseLSData(#[from] serde_json::Error),
}
#[derive(Error, Debug)]
pub enum EventStrUnescapeError {
#[error("Failed read hex string")]
ParseInt(#[from] std::num::ParseIntError),
#[error("hex string is not valid")]
ParseHex(#[from] std::string::FromUtf16Error),
#[error("No form found in desired application")]
NoForm,
}