Skip to content

Commit

Permalink
Allow user to ignore invalid messages
Browse files Browse the repository at this point in the history
  • Loading branch information
Sytten committed Oct 24, 2024
1 parent be96141 commit 3e5dcdc
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 3 deletions.
13 changes: 13 additions & 0 deletions src/browser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ impl Browser {

let handler_config = HandlerConfig {
ignore_https_errors: config.ignore_https_errors,
ignore_invalid_messages: config.ignore_invalid_messages,
viewport: config.viewport.clone(),
context_ids: Vec::new(),
request_timeout: config.request_timeout,
Expand Down Expand Up @@ -642,6 +643,8 @@ pub struct BrowserConfig {

/// Ignore https errors, default is true
ignore_https_errors: bool,
/// Ignore invalid messages, default is true
ignore_invalid_messages: bool,
viewport: Option<Viewport>,
/// The duration after a request with no response should time out
request_timeout: Duration,
Expand Down Expand Up @@ -673,6 +676,7 @@ pub struct BrowserConfigBuilder {
incognito: bool,
launch_timeout: Duration,
ignore_https_errors: bool,
ignore_invalid_events: bool,
viewport: Option<Viewport>,
request_timeout: Duration,
args: Vec<String>,
Expand Down Expand Up @@ -706,6 +710,7 @@ impl Default for BrowserConfigBuilder {
incognito: false,
launch_timeout: Duration::from_millis(LAUNCH_TIMEOUT),
ignore_https_errors: true,
ignore_invalid_events: true,
viewport: Some(Default::default()),
request_timeout: Duration::from_millis(REQUEST_TIMEOUT),
args: Vec::new(),
Expand Down Expand Up @@ -752,6 +757,13 @@ impl BrowserConfigBuilder {
self
}

/// The browser handler will return [CdpError::InvalidMessage] if a received
/// message cannot be parsed.
pub fn surface_invalid_messages(mut self) -> Self {
self.ignore_invalid_events = false;
self
}

pub fn port(mut self, port: u16) -> Self {
self.port = port;
self
Expand Down Expand Up @@ -887,6 +899,7 @@ impl BrowserConfigBuilder {
incognito: self.incognito,
launch_timeout: self.launch_timeout,
ignore_https_errors: self.ignore_https_errors,
ignore_invalid_messages: self.ignore_invalid_events,
viewport: self.viewport,
request_timeout: self.request_timeout,
args: self.args,
Expand Down
5 changes: 2 additions & 3 deletions src/conn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,8 @@ impl<T: EventMessage + Unpin> Stream for Connection<T> {
Ok(msg)
}
Err(err) => {
tracing::debug!(target: "chromiumoxide::conn::raw_ws::parse_errors", msg = text, "Failed to parse raw WS message");
tracing::error!("Failed to deserialize WS response {}", err);
Err(err.into())
tracing::debug!(target: "chromiumoxide::conn::raw_ws::parse_errors", msg = text, "Failed to parse raw WS message {}", err);
Err(CdpError::InvalidMessage(text, err))
}
};
Poll::Ready(Some(ready))
Expand Down
2 changes: 2 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ pub enum CdpError {
JavascriptException(Box<ExceptionDetails>),
#[error("{0}")]
Url(#[from] url::ParseError),
#[error("{1}")]
InvalidMessage(String, serde_json::Error),
}
impl CdpError {
pub fn msg(msg: impl Into<String>) -> Self {
Expand Down
10 changes: 10 additions & 0 deletions src/handler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -624,6 +624,13 @@ impl Stream for Handler {
Ok(Message::Event(ev)) => {
pin.on_event(ev);
}
Err(err @ CdpError::InvalidMessage(_, _)) => {
if pin.config.ignore_invalid_messages {
tracing::warn!("WS Invalid message: {}", err);
} else {
return Poll::Ready(Some(Err(err)));
}
}
Err(err) => {
tracing::error!("WS Connection error: {:?}", err);
return Poll::Ready(Some(Err(err)));
Expand All @@ -650,6 +657,8 @@ impl Stream for Handler {
pub struct HandlerConfig {
/// Whether the `NetworkManager`s should ignore https errors
pub ignore_https_errors: bool,
/// Whether to ignore invalid messages
pub ignore_invalid_messages: bool,
/// Window and device settings
pub viewport: Option<Viewport>,
/// Context ids to set from the get go
Expand All @@ -666,6 +675,7 @@ impl Default for HandlerConfig {
fn default() -> Self {
Self {
ignore_https_errors: true,
ignore_invalid_messages: true,
viewport: Default::default(),
context_ids: Vec::new(),
request_timeout: Duration::from_millis(REQUEST_TIMEOUT),
Expand Down

0 comments on commit 3e5dcdc

Please sign in to comment.