Skip to content

Commit

Permalink
[rust] Capture Rust backtrace in case of error (displayed at DEBUG le…
Browse files Browse the repository at this point in the history
…vel) (#12852)

* [rust] Use anyhow crate to display backtrace in case of error

* [rust] Include --backtrace argument to enable backtrace capture

* [rust] Use --backtrace argument in test

* [rust] Enable using RUST_BACKTRACE to capture backtrace as well

* [rust] Remove --backtrace (display backtrace when error is available at DEBUG level)

* [rust] Include debug info in release artifact and enable backtrace by default

* Revert "[rust] Include debug info in release artifact and enable backtrace by default"

This reverts commit aa57f2a.

* [rust] Remove backtrace for warning message

* [rust] Remove --backtrace from test

* [rust] Fix problems after rebasing

* [rust] Update bazel lock file

* [rust] Remove old download browser function in edge module
  • Loading branch information
bonigarcia authored Oct 9, 2023
1 parent bceee4e commit c8d121e
Show file tree
Hide file tree
Showing 20 changed files with 375 additions and 324 deletions.
97 changes: 96 additions & 1 deletion rust/Cargo.Bazel.lock
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"checksum": "7bb62c0cb24820374fb08c7eb1d2c1661ceb1a296f8cf2cad91579a7d2687eaf",
"checksum": "d9d5f22625fe3080b3eef8acfa5d55ae7929945c9570e4d592189d3b69f4149f",
"crates": {
"addr2line 0.19.0": {
"name": "addr2line",
Expand Down Expand Up @@ -478,6 +478,71 @@
},
"license": "MIT OR Apache-2.0"
},
"anyhow 1.0.75": {
"name": "anyhow",
"version": "1.0.75",
"repository": {
"Http": {
"url": "https://crates.io/api/v1/crates/anyhow/1.0.75/download",
"sha256": "a4668cab20f66d8d020e1fbc0ebe47217433c1b6c8f2040faf858554e394ace6"
}
},
"targets": [
{
"Library": {
"crate_name": "anyhow",
"crate_root": "src/lib.rs",
"srcs": [
"**/*.rs"
]
}
},
{
"BuildScript": {
"crate_name": "build_script_build",
"crate_root": "build.rs",
"srcs": [
"**/*.rs"
]
}
}
],
"library_target_name": "anyhow",
"common_attrs": {
"compile_data_glob": [
"**"
],
"crate_features": {
"common": [
"backtrace",
"default",
"std"
],
"selects": {}
},
"deps": {
"common": [
{
"id": "anyhow 1.0.75",
"target": "build_script_build"
},
{
"id": "backtrace 0.3.67",
"target": "backtrace"
}
],
"selects": {}
},
"edition": "2018",
"version": "1.0.75"
},
"build_script_attrs": {
"data_glob": [
"**"
]
},
"license": "MIT OR Apache-2.0"
},
"ar 0.9.0": {
"name": "ar",
"version": "0.9.0",
Expand Down Expand Up @@ -672,6 +737,13 @@
"compile_data_glob": [
"**"
],
"crate_features": {
"common": [
"default",
"std"
],
"selects": {}
},
"deps": {
"common": [
{
Expand Down Expand Up @@ -3860,6 +3932,13 @@
"compile_data_glob": [
"**"
],
"crate_features": {
"common": [
"read",
"read-core"
],
"selects": {}
},
"edition": "2018",
"version": "0.27.3"
},
Expand Down Expand Up @@ -5965,6 +6044,18 @@
"compile_data_glob": [
"**"
],
"crate_features": {
"common": [
"archive",
"coff",
"elf",
"macho",
"pe",
"read_core",
"unaligned"
],
"selects": {}
},
"deps": {
"common": [
{
Expand Down Expand Up @@ -8003,6 +8094,10 @@
],
"deps": {
"common": [
{
"id": "anyhow 1.0.75",
"target": "anyhow"
},
{
"id": "bzip2 0.4.4",
"target": "bzip2"
Expand Down
10 changes: 10 additions & 0 deletions rust/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ bzip2 = "0.4.4"
sevenz-rust = "0.5.2"
walkdir = "2.4.0"
debpkg = "0.6.0"
anyhow = { version = "1.0.75", features = ["backtrace"] }

[dev-dependencies]
assert_cmd = "2.0.12"
Expand Down
55 changes: 23 additions & 32 deletions rust/src/chrome.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ use crate::config::ManagerConfig;
use reqwest::Client;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::error::Error;

use anyhow::anyhow;
use anyhow::Error;
use std::option::Option;
use std::path::PathBuf;

Expand Down Expand Up @@ -60,7 +62,7 @@ pub struct ChromeManager {
}

impl ChromeManager {
pub fn new() -> Result<Box<Self>, Box<dyn Error>> {
pub fn new() -> Result<Box<Self>, Error> {
let browser_name = CHROME_NAME;
let driver_name = CHROMEDRIVER_NAME;
let config = ManagerConfig::default(browser_name, driver_name);
Expand Down Expand Up @@ -94,18 +96,15 @@ impl ChromeManager {
format!("{}{}", CFT_URL, endpoint)
}

fn request_driver_version_from_latest(
&self,
driver_url: String,
) -> Result<String, Box<dyn Error>> {
fn request_driver_version_from_latest(&self, driver_url: String) -> Result<String, Error> {
self.log.debug(format!(
"Reading {} version from {}",
&self.driver_name, driver_url
));
read_version_from_link(self.get_http_client(), driver_url, self.get_logger())
}

fn request_versions_from_online<T>(&self, driver_url: String) -> Result<T, Box<dyn Error>>
fn request_versions_from_online<T>(&self, driver_url: String) -> Result<T, Error>
where
T: Serialize + for<'a> Deserialize<'a>,
{
Expand All @@ -114,7 +113,7 @@ impl ChromeManager {
parse_json_from_url::<T>(self.get_http_client(), driver_url)
}

fn request_latest_driver_version_from_online(&mut self) -> Result<String, Box<dyn Error>> {
fn request_latest_driver_version_from_online(&mut self) -> Result<String, Error> {
let driver_name = self.driver_name;
self.get_logger().trace(format!(
"Using Chrome for Testing (CfT) endpoints to find out latest stable {} version",
Expand Down Expand Up @@ -150,7 +149,7 @@ impl ChromeManager {
Ok(stable_channel.version)
}

fn request_good_driver_version_from_online(&mut self) -> Result<String, Box<dyn Error>> {
fn request_good_driver_version_from_online(&mut self) -> Result<String, Error> {
let browser_or_driver_version = if self.get_driver_version().is_empty() {
self.get_browser_version()
} else {
Expand All @@ -170,13 +169,12 @@ impl ChromeManager {
.filter(|r| r.version.starts_with(version_for_filtering.as_str()))
.collect();
if filtered_versions.is_empty() {
return Err(format_three_args(
return Err(anyhow!(format_three_args(
UNAVAILABLE_DOWNLOAD_WITH_MIN_VERSION_ERR_MSG,
self.get_driver_name(),
&version_for_filtering,
&MIN_CHROMEDRIVER_VERSION_CFT.to_string(),
)
.into());
)));
}

let driver_version = filtered_versions.last().unwrap();
Expand Down Expand Up @@ -255,7 +253,7 @@ impl SeleniumManager for ChromeManager {
])
}

fn discover_browser_version(&mut self) -> Result<Option<String>, Box<dyn Error>> {
fn discover_browser_version(&mut self) -> Result<Option<String>, Error> {
self.general_discover_browser_version(
r#"HKCU\Software\Google\Chrome\BLBeacon"#,
REG_VERSION_ARG,
Expand All @@ -267,7 +265,7 @@ impl SeleniumManager for ChromeManager {
self.driver_name
}

fn request_driver_version(&mut self) -> Result<String, Box<dyn Error>> {
fn request_driver_version(&mut self) -> Result<String, Error> {
let major_browser_version_binding = self.get_major_browser_version();
let major_browser_version = major_browser_version_binding.as_str();
let cache_path = self.get_cache_path()?;
Expand Down Expand Up @@ -327,11 +325,11 @@ impl SeleniumManager for ChromeManager {
}
}

fn request_browser_version(&mut self) -> Result<Option<String>, Box<dyn Error>> {
fn request_browser_version(&mut self) -> Result<Option<String>, Error> {
self.general_request_browser_version(self.browser_name)
}

fn get_driver_url(&mut self) -> Result<String, Box<dyn Error>> {
fn get_driver_url(&mut self) -> Result<String, Error> {
let major_driver_version = self
.get_major_driver_version()
.parse::<i32>()
Expand Down Expand Up @@ -373,7 +371,7 @@ impl SeleniumManager for ChromeManager {
))
}

fn get_driver_path_in_cache(&self) -> Result<PathBuf, Box<dyn Error>> {
fn get_driver_path_in_cache(&self) -> Result<PathBuf, Error> {
Ok(compose_driver_path_in_cache(
self.get_cache_path()?.unwrap_or_default(),
self.driver_name,
Expand Down Expand Up @@ -426,7 +424,7 @@ impl SeleniumManager for ChromeManager {
fn request_latest_browser_version_from_online(
&mut self,
_browser_version: &str,
) -> Result<String, Box<dyn Error>> {
) -> Result<String, Error> {
let browser_name = self.browser_name;
self.get_logger().trace(format!(
"Using Chrome for Testing (CfT) endpoints to find out latest stable {} version",
Expand Down Expand Up @@ -457,7 +455,7 @@ impl SeleniumManager for ChromeManager {
fn request_fixed_browser_version_from_online(
&mut self,
_browser_version: &str,
) -> Result<String, Box<dyn Error>> {
) -> Result<String, Error> {
let browser_name = self.browser_name;
let mut browser_version = self.get_browser_version().to_string();
let major_browser_version = self.get_major_browser_version();
Expand Down Expand Up @@ -497,13 +495,12 @@ impl SeleniumManager for ChromeManager {
.filter(|r| r.version.starts_with(major_browser_version.as_str()))
.collect();
if filtered_versions.is_empty() {
return Err(format_three_args(
return Err(anyhow!(format_three_args(
UNAVAILABLE_DOWNLOAD_WITH_MIN_VERSION_ERR_MSG,
browser_name,
&major_browser_version,
&MIN_CHROME_VERSION_CFT.to_string(),
)
.into());
)));
}
let last_browser = filtered_versions.last().unwrap();
let platform_url: Vec<&PlatformUrl> = last_browser
Expand All @@ -518,14 +515,11 @@ impl SeleniumManager for ChromeManager {
}
}

fn get_min_browser_version_for_download(&self) -> Result<i32, Box<dyn Error>> {
fn get_min_browser_version_for_download(&self) -> Result<i32, Error> {
Ok(MIN_CHROME_VERSION_CFT)
}

fn get_browser_binary_path(
&mut self,
_browser_version: &str,
) -> Result<PathBuf, Box<dyn Error>> {
fn get_browser_binary_path(&mut self, _browser_version: &str) -> Result<PathBuf, Error> {
let browser_in_cache = self.get_browser_path_in_cache()?;
if MACOS.is(self.get_os()) {
Ok(browser_in_cache.join(CFT_MACOS_APP_NAME))
Expand All @@ -534,10 +528,7 @@ impl SeleniumManager for ChromeManager {
}
}

fn get_browser_url_for_download(
&mut self,
browser_version: &str,
) -> Result<String, Box<dyn Error>> {
fn get_browser_url_for_download(&mut self, browser_version: &str) -> Result<String, Error> {
if let Some(browser_url) = self.browser_url.clone() {
Ok(browser_url)
} else {
Expand All @@ -553,7 +544,7 @@ impl SeleniumManager for ChromeManager {
fn get_browser_label_for_download(
&self,
_browser_version: &str,
) -> Result<Option<&str>, Box<dyn Error>> {
) -> Result<Option<&str>, Error> {
Ok(None)
}
}
Expand Down
10 changes: 6 additions & 4 deletions rust/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ use crate::{ARCH_AMD64, ARCH_ARM64, ARCH_X86, TTL_SEC, WMIC_COMMAND_OS};
use std::cell::RefCell;
use std::env;
use std::env::consts::OS;
use std::error::Error;

use anyhow::anyhow;
use anyhow::Error;
use std::fs::read_to_string;
use std::path::Path;
use toml::Table;
Expand Down Expand Up @@ -129,15 +131,15 @@ impl OS {
}
}

pub fn str_to_os(os: &str) -> Result<OS, Box<dyn Error>> {
pub fn str_to_os(os: &str) -> Result<OS, Error> {
if WINDOWS.is(os) {
Ok(WINDOWS)
} else if MACOS.is(os) {
Ok(MACOS)
} else if LINUX.is(os) {
Ok(LINUX)
} else {
Err(format!("Invalid operating system: {os}").into())
Err(anyhow!(format!("Invalid operating system: {os}")))
}
}

Expand Down Expand Up @@ -232,7 +234,7 @@ fn get_env_name(suffix: &str) -> String {
concat(ENV_PREFIX, suffix_uppercase.as_str())
}

fn get_config() -> Result<Table, Box<dyn Error>> {
fn get_config() -> Result<Table, Error> {
let cache_path = read_cache_path();
let config_path = Path::new(&cache_path).to_path_buf().join(CONFIG_FILE);
Ok(read_to_string(config_path)?.parse()?)
Expand Down
Loading

0 comments on commit c8d121e

Please sign in to comment.