1use chromiumoxide::error::CdpError;
2
3use crate::config::Patch;
4
5#[derive(Debug, Clone, PartialEq, Eq)]
7pub struct ValidationErrors(Vec<ValidationIssue>);
8
9impl ValidationErrors {
10 pub fn issues(&self) -> &[ValidationIssue] {
12 &self.0
13 }
14
15 pub fn into_issues(self) -> Vec<ValidationIssue> {
17 self.0
18 }
19}
20
21impl From<Vec<ValidationIssue>> for ValidationErrors {
22 fn from(issues: Vec<ValidationIssue>) -> Self {
23 Self(issues)
24 }
25}
26
27impl std::fmt::Display for ValidationErrors {
28 fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
29 for (index, issue) in self.0.iter().enumerate() {
30 if index > 0 {
31 formatter.write_str("; ")?;
32 }
33 std::fmt::Display::fmt(issue, formatter)?;
34 }
35 Ok(())
36 }
37}
38
39#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
41#[non_exhaustive]
42pub enum ValidationIssue {
43 #[error("{field} must not be empty")]
45 EmptyValue {
46 field: &'static str,
48 },
49 #[error("screen dimensions must be greater than zero")]
51 InvalidScreenDimensions,
52 #[error("available screen dimensions cannot exceed screen dimensions")]
54 WorkAreaExceedsScreen,
55 #[error("device scale factor must be finite and greater than zero")]
57 InvalidScaleFactor,
58 #[error("the first navigator language must match the Intl locale")]
60 LocaleLanguageMismatch,
61 #[error("touch configuration is contradictory")]
63 TouchContradiction,
64 #[error("user agent, navigator platform, and UA Client Hints platform disagree")]
66 PlatformMismatch,
67 #[error("Chrome user-agent and UA Client Hints versions disagree")]
69 ChromeVersionMismatch,
70 #[error("duplicate UA Client Hint brand: {brand}")]
72 DuplicateBrand {
73 brand: String,
75 },
76 #[error("reduced and full UA Client Hint brand sets disagree")]
78 ClientHintBrandSetMismatch,
79 #[error("UA Client Hints must include Chromium and Google Chrome brands")]
81 MissingChromeBrands,
82 #[error("UA Client Hint WoW64 metadata is incompatible with the identity")]
84 InvalidWow64,
85 #[error("UA Client Hint architecture and bitness are incompatible")]
87 InvalidArchitectureBitness,
88 #[error("UA Client Hint model and form factors contradict the mobile state")]
90 MobileMetadataMismatch,
91 #[error("UA Client Hint platform version has an invalid format")]
93 InvalidPlatformVersion,
94}
95
96#[derive(Debug, thiserror::Error)]
98#[non_exhaustive]
99pub enum Error {
100 #[error("failed to encode a target-session CDP command: {0}")]
102 TargetCommandJson(serde_json::Error),
103
104 #[cfg(feature = "interceptor")]
106 #[error("invalid interceptor header policy: {message}")]
107 InvalidInterceptorHeader {
108 message: String,
110 },
111
112 #[cfg(feature = "seeding")]
114 #[error("invalid profile seed: {message}")]
115 InvalidSeed {
116 message: String,
118 },
119
120 #[cfg(feature = "seeding")]
122 #[error("invalid profile seed JSON: {0}")]
123 SeedJson(#[from] serde_json::Error),
124
125 #[error("stealth configuration failed validation: {issues}")]
127 Validation {
128 issues: ValidationErrors,
130 },
131
132 #[error("failed to build the {patch} CDP parameters: {message}")]
134 InvalidPatchParameters {
135 patch: &'static str,
137 message: String,
139 },
140
141 #[error("failed to apply the {patch} CDP patch: {source}")]
143 Cdp {
144 patch: &'static str,
146 #[source]
148 source: CdpError,
149 },
150
151 #[error("failed to apply {patch:?} after applying {applied:?}: {source}")]
153 Apply {
154 patch: Patch,
156 applied: Vec<Patch>,
158 #[source]
160 source: Box<Error>,
161 },
162}
163
164impl Error {
165 #[cfg(feature = "interceptor")]
166 pub(crate) fn invalid_interceptor_header(message: impl Into<String>) -> Self {
167 Self::InvalidInterceptorHeader {
168 message: message.into(),
169 }
170 }
171
172 #[cfg(feature = "seeding")]
173 pub(crate) fn invalid_seed(message: impl Into<String>) -> Self {
174 Self::InvalidSeed {
175 message: message.into(),
176 }
177 }
178
179 pub(crate) fn invalid_parameters(patch: &'static str, message: impl Into<String>) -> Self {
180 Self::InvalidPatchParameters {
181 patch,
182 message: message.into(),
183 }
184 }
185
186 pub(crate) fn target_command_json(source: serde_json::Error) -> Self {
187 Self::TargetCommandJson(source)
188 }
189
190 pub(crate) fn cdp(patch: &'static str, source: CdpError) -> Self {
191 Self::Cdp { patch, source }
192 }
193}
194
195pub type Result<T> = std::result::Result<T, Error>;