Skip to content

Commit

Permalink
Handle more .unwrap()'s (#369)
Browse files Browse the repository at this point in the history
* handled more unwraps

* unhandled more unwraps
  • Loading branch information
Billy-Sheppard committed Feb 17, 2023
1 parent b446327 commit 930fb55
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 21 deletions.
4 changes: 2 additions & 2 deletions src/browser/fetcher.rs
Expand Up @@ -264,7 +264,7 @@ impl Fetcher {
);
if let Some(p) = out_path.parent() {
if !p.exists() {
fs::create_dir_all(&p).unwrap();
fs::create_dir_all(&p)?;
}
}
let mut out_file = BufWriter::new(File::create(&out_path)?);
Expand All @@ -276,7 +276,7 @@ impl Fetcher {
use std::os::unix::fs::PermissionsExt;

if let Some(mode) = file.unix_mode() {
fs::set_permissions(&out_path, fs::Permissions::from_mode(mode)).unwrap();
fs::set_permissions(&out_path, fs::Permissions::from_mode(mode))?;
}
}
}
Expand Down
9 changes: 4 additions & 5 deletions src/browser/mod.rs
Expand Up @@ -4,7 +4,7 @@ use std::sync::Arc;
use std::sync::Mutex;
use std::time::Duration;

use anyhow::Result;
use anyhow::{anyhow, Result};
use log::{debug, error, info, trace};

use process::Process;
Expand Down Expand Up @@ -105,10 +105,9 @@ impl Browser {
/// binary can be found on the system.
pub fn default() -> Result<Self> {
let launch_options = LaunchOptions::default_builder()
.path(Some(default_executable().unwrap()))
.build()
.unwrap();
Ok(Self::new(launch_options).unwrap())
.path(Some(default_executable().map_err(|e| anyhow!(e))?))
.build()?;
Self::new(launch_options)
}

/// Allows you to drive an externally-launched Chrome process instead of launch one via [`Browser::new`].
Expand Down
6 changes: 3 additions & 3 deletions src/browser/process.rs
Expand Up @@ -388,9 +388,9 @@ impl Process {
where
R: Read,
{
let port_taken_re = Regex::new(r"ERROR.*bind\(\)").unwrap();
let port_taken_re = Regex::new(r"ERROR.*bind\(\)")?;

let re = Regex::new(r"listening on (.*/devtools/browser/.*)$").unwrap();
let re = Regex::new(r"listening on (.*/devtools/browser/.*)$")?;

let extract = |text: &str| -> Option<String> {
let caps = re.captures(text);
Expand All @@ -416,7 +416,7 @@ impl Process {

fn ws_url_from_output(child_process: &mut Child) -> Result<Url> {
let chrome_output_result = util::Wait::with_timeout(Duration::from_secs(30)).until(|| {
let my_stderr = BufReader::new(child_process.stderr.as_mut().unwrap());
let my_stderr = BufReader::new(child_process.stderr.as_mut()?);
match Self::ws_url_from_reader(my_stderr) {
Ok(output_option) => output_option.map(Ok),
Err(err) => Some(Err(err)),
Expand Down
4 changes: 2 additions & 2 deletions src/browser/tab/mod.rs
Expand Up @@ -676,7 +676,7 @@ impl Tab {
/// assert_eq!(attrs["id"], "foobar");
/// #
/// # Ok(())
/// # }
/// # }z
/// ```
pub fn find_element(&self, selector: &str) -> Result<Element<'_>> {
let root_node_id = self.get_document()?.node_id;
Expand Down Expand Up @@ -1727,7 +1727,7 @@ impl Tab {
Some(mut ua) => {
ua = ua.replace("HeadlessChrome/", "Chrome/");

let re = regex::Regex::new(r"\(([^)]+)\)").unwrap();
let re = regex::Regex::new(r"\(([^)]+)\)")?;
ua = re.replace(&ua, "(Windows NT 10.0; Win64; x64)").to_string();

self.set_user_agent(&ua, None, None)?;
Expand Down
2 changes: 1 addition & 1 deletion src/testing_utils/logging.rs
Expand Up @@ -19,7 +19,7 @@ pub fn enable_logging() {
let seconds_millis = date.format("%S%.3f").to_string();
let fmt_seconds = style.set_bold(true).value(seconds_millis);

let truncated_module_path = &record.module_path().unwrap()[5..];
let truncated_module_path = &record.module_path()?[5..];

writeln!(
buf,
Expand Down
12 changes: 6 additions & 6 deletions src/testing_utils/server.rs
Expand Up @@ -13,7 +13,7 @@ impl Server {
pub fn new(
mut responder: impl FnMut(tiny_http::Request) -> Result<(), io::Error> + Send + 'static,
) -> Self {
let server = Arc::new(tiny_http::Server::http("127.0.0.1:0").unwrap());
let server = Arc::new(tiny_http::Server::http("127.0.0.1:0")?);
let shall_exit = Arc::new(atomic::AtomicBool::new(false));
let srv = server.clone();
let exit = shall_exit.clone();
Expand Down Expand Up @@ -41,7 +41,7 @@ impl Server {
let response = tiny_http::Response::new(
200.into(),
vec![
tiny_http::Header::from_bytes(&b"Content-Type"[..], &b"text/html"[..]).unwrap(),
tiny_http::Header::from_bytes(&b"Content-Type"[..], &b"text/html"[..])?,
],
io::Cursor::new(data),
Some(data.len()),
Expand All @@ -58,21 +58,21 @@ impl Server {
}

pub fn port(&self) -> u16 {
self.server.server_addr().to_ip().unwrap().port()
self.server.server_addr().to_ip()?.port()
}

pub fn exit(&mut self) -> Result<(), io::Error> {
self.shall_exit.store(true, atomic::Ordering::Relaxed);
match self.handler.take() {
Some(h) => h.join().unwrap(),
Some(h) => h.join()?,
None => Ok(()),
}
}
}

impl Drop for Server {
fn drop(&mut self) {
self.exit().unwrap()
self.exit()?
}
}

Expand All @@ -82,7 +82,7 @@ fn basic_http_response<'a>(
) -> tiny_http::Response<&'a [u8]> {
tiny_http::Response::new(
200.into(),
vec![tiny_http::Header::from_bytes(&b"Content-Type"[..], content_type.as_bytes()).unwrap()],
vec![tiny_http::Header::from_bytes(&b"Content-Type"[..], content_type.as_bytes())?],
body.as_bytes(),
Some(body.len()),
None,
Expand Down
4 changes: 2 additions & 2 deletions src/types.rs
Expand Up @@ -42,7 +42,7 @@ where
return Err(error.into());
}

let result: T = serde_json::from_value(response.result.unwrap()).unwrap();
let result: T = serde_json::from_value(response.result.unwrap())?;

Ok(result)
}
Expand Down Expand Up @@ -336,7 +336,7 @@ mod tests {
"targetId": "26DEBCB2A45BEFC67A84012AC32C8B2A"
}
});
let event: Event = serde_json::from_value(received_target_msg_event).unwrap();
let event: Event = serde_json::from_value(received_target_msg_event)?;
match event {
Event::ReceivedMessageFromTarget(ev) => {
trace!("{:?}", ev);
Expand Down

0 comments on commit 930fb55

Please sign in to comment.