Skip to content

Commit

Permalink
feat: switch to eyre for error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
cestef committed May 17, 2024
1 parent 4979fbd commit 05d66cd
Show file tree
Hide file tree
Showing 16 changed files with 78 additions and 30 deletions.
5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ edition = "2021"
exclude = ["assets/*", ".github/*"]

[dependencies]
anyhow = "1.0.75"
clap = { version = "4.5.4", features = ["derive", "env", "string"] }
clap-markdown = "0.1.3"
colored = "2.1.0"
Expand Down Expand Up @@ -52,6 +51,10 @@ strsim = "0.11.1"
scraper = "0.19.0"
serde_json = "1.0.117"
rhai = "1.18.0"
better-panic = "0.3.0"
color-eyre = "0.6.3"
strip-ansi-escapes = "0.2.0"
human-panic = "2.0.0"

# Strip the debug symbols from the binary
[profile.release]
Expand Down
4 changes: 2 additions & 2 deletions src/cli/interactive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use crate::cli::opts::Opts;
use crate::utils::tree::tree;
use crate::utils::tree::TreeData;
use crate::utils::tree::TreeNode;
use anyhow::bail;
use anyhow::Result;
use color_eyre::eyre::bail;
use color_eyre::eyre::Result;
use colored::Colorize;
use lazy_static::lazy_static;
use log::error;
Expand Down
2 changes: 1 addition & 1 deletion src/cli/opts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use super::helpers::{
parse_cookie, parse_header, parse_method, parse_url, parse_wordlist, KeyOrKeyVal,
KeyOrKeyValParser, KeyVal, KeyValParser,
};
use anyhow::Result;
use color_eyre::eyre::Result;
use clap::Parser;
use merge::Merge;

Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ use crate::{
table::build_opts_table,
},
};
use anyhow::bail;
use anyhow::Result;
use color_eyre::eyre::bail;
use color_eyre::eyre::Result;
use colored::Colorize;
use futures::{future::abortable, FutureExt};
use indicatif::HumanDuration;
Expand Down
5 changes: 3 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#![allow(dead_code)]

use anyhow::Result;
use color_eyre::eyre::Result;
use clap::{CommandFactory, Parser, ValueEnum};
use clap_complete::{generate, Generator, Shell};
use clap_complete_nushell::Nushell;
Expand All @@ -19,7 +19,8 @@ use std::{fs::OpenOptions, path::Path, process};
#[tokio::main]
async fn main() -> Result<()> {
utils::logger::init_logger();

utils::init_panic()?;
panic!("test");
let mut opts = Opts::parse();

if let Some(p) = opts.config {
Expand Down
2 changes: 1 addition & 1 deletion src/runner/classic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::{
tree::{Tree, TreeData, UrlType},
},
};
use anyhow::{anyhow, Result};
use color_eyre::eyre::{anyhow, Result};
use colored::Colorize;
use indicatif::ProgressBar;
use itertools::Itertools;
Expand Down
2 changes: 1 addition & 1 deletion src/runner/client.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::path::Path;

use anyhow::{Context, Result};
use color_eyre::eyre::{Context, ContextCompat, Result};
use http_rest_file::{model::Header, Parser};
use reqwest::{
header::{HeaderMap, HeaderName},
Expand Down
14 changes: 7 additions & 7 deletions src/runner/extract.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use anyhow::{Context, Result};
use color_eyre::eyre::{Context, Result};
use lazy_static::lazy_static;
use scraper::{Html, Selector};
use std::fmt;
Expand Down Expand Up @@ -66,13 +66,13 @@ impl fmt::Display for Link {
}

pub fn is_same_domain(url: &Url, base: &Url, allow_subdomain: bool) -> Result<bool> {
let url_domain = url
.domain()
.ok_or_else(|| anyhow::anyhow!("Could not parse domain from URL: {}", url.to_string()))?;
let url_domain = url.domain().ok_or_else(|| {
color_eyre::eyre::anyhow!("Could not parse domain from URL: {}", url.to_string())
})?;

let base_domain = base
.domain()
.ok_or_else(|| anyhow::anyhow!("Could not parse domain from URL: {}", base.to_string()))?;
let base_domain = base.domain().ok_or_else(|| {
color_eyre::eyre::anyhow!("Could not parse domain from URL: {}", base.to_string())
})?;

if allow_subdomain {
Ok(url_domain == base_domain || url_domain.ends_with(&format!(".{}", base_domain)))
Expand Down
2 changes: 1 addition & 1 deletion src/runner/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub mod wordlists;

use std::future::Future;

use anyhow::Result;
use color_eyre::eyre::Result;

pub trait Runner {
fn run(self) -> impl Future<Output = Result<()>> + Send;
Expand Down
2 changes: 1 addition & 1 deletion src/runner/recursive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::{
};
use tokio::task::JoinHandle;

use anyhow::{anyhow, Result};
use color_eyre::eyre::{anyhow, Result};
use parking_lot::Mutex;

use crate::{
Expand Down
2 changes: 1 addition & 1 deletion src/runner/scripting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::{
cli::opts::Opts,
utils::tree::{tree, TreeData},
};
use anyhow::{anyhow, Result};
use color_eyre::eyre::{anyhow, Result};
use colored::Colorize;
use indicatif::ProgressBar;

Expand Down
4 changes: 2 additions & 2 deletions src/runner/spider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ use crate::{
tree::{Tree, TreeData, UrlType},
},
};
use anyhow::anyhow;
use anyhow::{Context, Ok, Result};
use color_eyre::eyre::anyhow;
use color_eyre::eyre::{Context, Ok, Result};
use colored::Colorize;
use indicatif::ProgressBar;
use itertools::Itertools;
Expand Down
6 changes: 3 additions & 3 deletions src/runner/wordlists.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::{
path::{Path, PathBuf},
};

use anyhow::{Context, Result};
use color_eyre::eyre::{Context, Result};
use colored::Colorize;
use tokio::io::AsyncReadExt;

Expand Down Expand Up @@ -433,7 +433,7 @@ fn expand_tilde<P: AsRef<Path>>(path_user_input: P) -> Result<PathBuf> {
}
if p == Path::new("~") {
return dirs::home_dir()
.ok_or_else(|| anyhow::anyhow!("Failed to expand tilde in path: {}", p.display()));
.ok_or_else(|| color_eyre::eyre::anyhow!("Failed to expand tilde in path: {}", p.display()));
}
dirs::home_dir()
.map(|mut h| {
Expand All @@ -446,7 +446,7 @@ fn expand_tilde<P: AsRef<Path>>(path_user_input: P) -> Result<PathBuf> {
h
}
})
.ok_or_else(|| anyhow::anyhow!("Failed to expand tilde in path: {}", p.display()))
.ok_or_else(|| color_eyre::eyre::anyhow!("Failed to expand tilde in path: {}", p.display()))
}

#[cfg(test)]
Expand Down
46 changes: 45 additions & 1 deletion src/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use anyhow::{bail, Result};
use color_eyre::eyre::{bail, Result};
use colored::{Colorize, CustomColor};
use parking_lot::Mutex;
use std::{io::Write, sync::Arc};
Expand Down Expand Up @@ -140,6 +140,50 @@ pub fn is_range(s: &str) -> bool {
false
}

pub fn init_panic() -> Result<()> {
let (panic_hook, eyre_hook) = color_eyre::config::HookBuilder::default()
.panic_section(format!(
"This is a bug. Consider reporting it at {}",
env!("CARGO_PKG_REPOSITORY")
))
.capture_span_trace_by_default(false)
.display_location_section(false)
.display_env_section(false)
.into_hooks();
eyre_hook.install()?;
std::panic::set_hook(Box::new(move |panic_info| {
#[cfg(not(debug_assertions))]
{
use human_panic::{handle_dump, print_msg, Metadata};

let meta = Metadata::new(env!("CARGO_PKG_NAME"), env!("CARGO_PKG_VERSION"))
.authors(env!("CARGO_PKG_AUTHORS").replace(':', ", "))
.homepage(env!("CARGO_PKG_HOMEPAGE"));

let file_path = handle_dump(&meta, panic_info);
// prints human-panic message
print_msg(file_path, &meta)
.expect("human-panic: printing error message to console failed");
eprintln!("{}", panic_hook.panic_report(panic_info)); // prints color-eyre stack trace to stderr
}
let msg = format!("{}", panic_hook.panic_report(panic_info));
log::error!("Error: {}", strip_ansi_escapes::strip_str(msg));

#[cfg(debug_assertions)]
{
// Better Panic stacktrace that is only enabled when debugging.
better_panic::Settings::auto()
.most_recent_first(false)
.lineno_suffix(true)
.verbosity(better_panic::Verbosity::Full)
.create_panic_handler()(panic_info);
}

std::process::exit(1);
}));
Ok(())
}

// Write the tree to a file (json, csv, md)
pub fn save_to_file(
opts: &Opts,
Expand Down
6 changes: 3 additions & 3 deletions src/utils/tree.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use anyhow::Result;
use color_eyre::eyre::Result;
use colored::Colorize;
use log::{info, warn};
use parking_lot::Mutex;
Expand Down Expand Up @@ -285,7 +285,7 @@ pub fn from_save(
) -> Result<Arc<Mutex<Tree<TreeData>>>> {
if let Some(root) = &save.tree.clone().lock().root {
if opts.url.is_some() && root.lock().data.url != opts.url.clone().unwrap() {
Err(anyhow::anyhow!(
Err(color_eyre::eyre::anyhow!(
"The URL of the saved state does not match the URL provided"
))
} else {
Expand All @@ -306,7 +306,7 @@ pub fn from_save(
Ok(save.tree.clone())
}
} else {
Err(anyhow::anyhow!("No saved state found"))
Err(color_eyre::eyre::anyhow!("No saved state found"))
}
}

Expand Down
2 changes: 1 addition & 1 deletion tests/integration.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use anyhow::Result;
use clap::Parser;
use color_eyre::eyre::Result;
use rwalk::{
_main,
cli::opts::{Opts, Wordlist},
Expand Down

0 comments on commit 05d66cd

Please sign in to comment.