Skip to content

Commit

Permalink
[rust] Include --backtrace argument to enable backtrace capture
Browse files Browse the repository at this point in the history
  • Loading branch information
bonigarcia committed Oct 3, 2023
1 parent 4e42d92 commit b4b60b5
Showing 1 changed file with 33 additions and 15 deletions.
48 changes: 33 additions & 15 deletions rust/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
// specific language governing permissions and limitations
// under the License.

use std::backtrace::{Backtrace, BacktraceStatus};
use std::path::Path;
use std::process::exit;

Expand Down Expand Up @@ -125,6 +126,10 @@ struct Cli {
/// Avoid to download browser (even when browser-version is specified)
#[clap(long)]
avoid_browser_download: bool,

/// Capture backtrace when some error happens
#[clap(long)]
backtrace: bool,
}

fn main() {
Expand All @@ -134,6 +139,7 @@ fn main() {

let debug = cli.debug || BooleanKey("debug", false).get_value();
let trace = cli.trace || BooleanKey("trace", false).get_value();
let backtrace = cli.backtrace || BooleanKey("backtrace", false).get_value();
let log = Logger::create(&cli.output, debug, trace);
let grid = cli.grid;
let mut browser_name: String = cli.browser.unwrap_or_default();
Expand All @@ -148,21 +154,21 @@ fn main() {
let mut selenium_manager: Box<dyn SeleniumManager> = if !browser_name.is_empty() {
get_manager_by_browser(browser_name).unwrap_or_else(|err| {
log.error(&err);
flush_and_exit(DATAERR, &log, Some(err));
flush_and_exit(DATAERR, &log, backtrace, Some(err));
})
} else if !driver_name.is_empty() {
get_manager_by_driver(driver_name).unwrap_or_else(|err| {
log.error(&err);
flush_and_exit(DATAERR, &log, Some(err));
flush_and_exit(DATAERR, &log, backtrace, Some(err));
})
} else if grid.is_some() {
GridManager::new(grid.as_ref().unwrap().to_string()).unwrap_or_else(|err| {
log.error(&err);
flush_and_exit(DATAERR, &log, Some(err));
flush_and_exit(DATAERR, &log, backtrace, Some(err));
})
} else {
log.error("You need to specify a browser or driver");
flush_and_exit(DATAERR, &log, None);
flush_and_exit(DATAERR, &log, backtrace, None);
};

if cli.offline {
Expand Down Expand Up @@ -202,8 +208,13 @@ fn main() {
.and_then(|_| selenium_manager.setup())
.map(|driver_path| {
let log = selenium_manager.get_logger();
log_driver_and_browser_path(log, &driver_path, selenium_manager.get_browser_path());
flush_and_exit(OK, log, None);
log_driver_and_browser_path(
log,
backtrace,
&driver_path,
selenium_manager.get_browser_path(),
);
flush_and_exit(OK, log, backtrace, None);
})
.unwrap_or_else(|err| {
let log = selenium_manager.get_logger();
Expand All @@ -217,36 +228,43 @@ fn main() {
));
log_driver_and_browser_path(
log,
backtrace,
&best_driver_from_cache,
selenium_manager.get_browser_path(),
);
flush_and_exit(OK, log, Some(err));
flush_and_exit(OK, log, backtrace, Some(err));
} else if selenium_manager.is_offline() {
log.warn(err.to_string());
flush_and_exit(OK, log, Some(err));
flush_and_exit(OK, log, backtrace, Some(err));
} else {
log.error(err.to_string());
flush_and_exit(DATAERR, log, Some(err));
flush_and_exit(DATAERR, log, backtrace, Some(err));
}
});
}

fn log_driver_and_browser_path(log: &Logger, driver_path: &Path, browser_path: &str) {
fn log_driver_and_browser_path(
log: &Logger,
backtrace: bool,
driver_path: &Path,
browser_path: &str,
) {
if driver_path.exists() {
log.info(format!("{}{}", DRIVER_PATH, driver_path.display()));
} else {
log.error(format!("Driver unavailable: {}", DRIVER_PATH));
flush_and_exit(UNAVAILABLE, log, None);
flush_and_exit(UNAVAILABLE, log, backtrace, None);
}
if !browser_path.is_empty() {
log.info(format!("{}{}", BROWSER_PATH, browser_path));
}
}

fn flush_and_exit(code: i32, log: &Logger, err: Option<Error>) -> ! {
if let Some(error) = err {
let backtrace = error.backtrace();
if !backtrace.to_string().ends_with("backtrace") {
fn flush_and_exit(code: i32, log: &Logger, is_backtrace: bool, err: Option<Error>) -> ! {
if err.is_some() && is_backtrace {
let backtrace = Backtrace::force_capture();
let backtrace_status = backtrace.status();
if backtrace_status == BacktraceStatus::Captured {
log.debug(format!("Backtrace:\n{}", backtrace));
}
}
Expand Down

0 comments on commit b4b60b5

Please sign in to comment.