use chromiumoxide::error::CdpError;
use crate::config::Patch;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ValidationErrors(Vec<ValidationIssue>);
impl ValidationErrors {
pub fn issues(&self) -> &[ValidationIssue] {
&self.0
}
pub fn into_issues(self) -> Vec<ValidationIssue> {
self.0
}
}
impl From<Vec<ValidationIssue>> for ValidationErrors {
fn from(issues: Vec<ValidationIssue>) -> Self {
Self(issues)
}
}
impl std::fmt::Display for ValidationErrors {
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
for (index, issue) in self.0.iter().enumerate() {
if index > 0 {
formatter.write_str("; ")?;
}
std::fmt::Display::fmt(issue, formatter)?;
}
Ok(())
}
}
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
#[non_exhaustive]
pub enum ValidationIssue {
#[error("{field} must not be empty")]
EmptyValue {
field: &'static str,
},
#[error("screen dimensions must be greater than zero")]
InvalidScreenDimensions,
#[error("available screen dimensions cannot exceed screen dimensions")]
WorkAreaExceedsScreen,
#[error("device scale factor must be finite and greater than zero")]
InvalidScaleFactor,
#[error("the first navigator language must match the Intl locale")]
LocaleLanguageMismatch,
#[error("touch configuration is contradictory")]
TouchContradiction,
#[error("user agent, navigator platform, and UA Client Hints platform disagree")]
PlatformMismatch,
#[error("Chrome user-agent and UA Client Hints versions disagree")]
ChromeVersionMismatch,
#[error("duplicate UA Client Hint brand: {brand}")]
DuplicateBrand {
brand: String,
},
#[error("reduced and full UA Client Hint brand sets disagree")]
ClientHintBrandSetMismatch,
#[error("UA Client Hints must include Chromium and Google Chrome brands")]
MissingChromeBrands,
#[error("UA Client Hint WoW64 metadata is incompatible with the identity")]
InvalidWow64,
#[error("UA Client Hint architecture and bitness are incompatible")]
InvalidArchitectureBitness,
#[error("UA Client Hint model and form factors contradict the mobile state")]
MobileMetadataMismatch,
#[error("UA Client Hint platform version has an invalid format")]
InvalidPlatformVersion,
}
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum Error {
#[error("failed to encode a target-session CDP command: {0}")]
TargetCommandJson(serde_json::Error),
#[cfg(feature = "interceptor")]
#[error("invalid interceptor header policy: {message}")]
InvalidInterceptorHeader {
message: String,
},
#[cfg(feature = "seeding")]
#[error("invalid profile seed: {message}")]
InvalidSeed {
message: String,
},
#[cfg(feature = "seeding")]
#[error("invalid profile seed JSON: {0}")]
SeedJson(#[from] serde_json::Error),
#[error("stealth configuration failed validation: {issues}")]
Validation {
issues: ValidationErrors,
},
#[error("failed to build the {patch} CDP parameters: {message}")]
InvalidPatchParameters {
patch: &'static str,
message: String,
},
#[error("failed to apply the {patch} CDP patch: {source}")]
Cdp {
patch: &'static str,
#[source]
source: CdpError,
},
#[error("failed to apply {patch:?} after applying {applied:?}: {source}")]
Apply {
patch: Patch,
applied: Vec<Patch>,
#[source]
source: Box<Error>,
},
}
impl Error {
#[cfg(feature = "interceptor")]
pub(crate) fn invalid_interceptor_header(message: impl Into<String>) -> Self {
Self::InvalidInterceptorHeader {
message: message.into(),
}
}
#[cfg(feature = "seeding")]
pub(crate) fn invalid_seed(message: impl Into<String>) -> Self {
Self::InvalidSeed {
message: message.into(),
}
}
pub(crate) fn invalid_parameters(patch: &'static str, message: impl Into<String>) -> Self {
Self::InvalidPatchParameters {
patch,
message: message.into(),
}
}
pub(crate) fn target_command_json(source: serde_json::Error) -> Self {
Self::TargetCommandJson(source)
}
pub(crate) fn cdp(patch: &'static str, source: CdpError) -> Self {
Self::Cdp { patch, source }
}
}
pub type Result<T> = std::result::Result<T, Error>;