Skip to content

Improve error messages #2617

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 21, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Improve error messages
  • Loading branch information
acuteenvy committed Apr 16, 2025
commit c1f28eb4912c556e71c0216eb243a1d5528b7fcf
54 changes: 22 additions & 32 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ use std::{
io::{self, Stdout},
panic,
path::Path,
process,
time::{Duration, Instant},
};
use ui::style::Theme;
Expand Down Expand Up @@ -120,20 +119,24 @@ enum Updater {
NotifyWatcher,
}

/// Do `log::error!` and `eprintln!` in one line.
macro_rules! log_eprintln {
( $($arg:tt)* ) => {{
log::error!($($arg)*);
eprintln!($($arg)*);
}};
}

fn main() -> Result<()> {
let app_start = Instant::now();

let cliargs = process_cmdline()?;

asyncgit::register_tracing_logging();

if !valid_path(&cliargs.repo_path) {
eprintln!("invalid path\nplease run gitui inside of a non-bare git repository");
return Ok(());
}
ensure_valid_path(&cliargs.repo_path)?;

let key_config = KeyConfig::init()
.map_err(|e| eprintln!("KeyConfig loading error: {e}"))
.map_err(|e| log_eprintln!("KeyConfig loading error: {e}"))
.unwrap_or_default();
let theme = Theme::init(&cliargs.theme);

Expand All @@ -142,7 +145,7 @@ fn main() -> Result<()> {
shutdown_terminal();
}

set_panic_handlers()?;
set_panic_handler()?;

let mut repo_path = cliargs.repo_path;
let mut terminal = start_terminal(io::stdout(), &repo_path)?;
Expand Down Expand Up @@ -292,13 +295,13 @@ fn shutdown_terminal() {
io::stdout().execute(LeaveAlternateScreen).map(|_f| ());

if let Err(e) = leave_screen {
eprintln!("leave_screen failed:\n{e}");
log::error!("leave_screen failed:\n{e}");
}

let leave_raw_mode = disable_raw_mode();

if let Err(e) = leave_raw_mode {
eprintln!("leave_raw_mode failed:\n{e}");
log::error!("leave_raw_mode failed:\n{e}");
}
}

Expand All @@ -316,12 +319,14 @@ fn draw(terminal: &mut Terminal, app: &App) -> io::Result<()> {
Ok(())
}

fn valid_path(repo_path: &RepoPath) -> bool {
let error = asyncgit::sync::repo_open_error(repo_path);
if let Some(error) = &error {
log::error!("repo open error: {error}");
fn ensure_valid_path(repo_path: &RepoPath) -> Result<()> {
match asyncgit::sync::repo_open_error(repo_path) {
Some(e) => {
log::error!("{e}");
bail!(e)
}
None => Ok(()),
}
error.is_none()
}

fn select_event(
Expand Down Expand Up @@ -389,30 +394,15 @@ fn start_terminal(
Ok(terminal)
}

// do log::error! and eprintln! in one line, pass string, error and backtrace
macro_rules! log_eprintln {
($string:expr, $e:expr, $bt:expr) => {
log::error!($string, $e, $bt);
eprintln!($string, $e, $bt);
};
}

fn set_panic_handlers() -> Result<()> {
// regular panic handler
fn set_panic_handler() -> Result<()> {
panic::set_hook(Box::new(|e| {
let backtrace = Backtrace::new();
shutdown_terminal();
log_eprintln!("\nGitUI was close due to an unexpected panic.\nPlease file an issue on https://github.com/gitui-org/gitui/issues with the following info:\n\n{:?}\ntrace:\n{:?}", e, backtrace);
log_eprintln!("\nGitUI was closed due to an unexpected panic.\nPlease file an issue on https://github.com/gitui-org/gitui/issues with the following info:\n\n{e}\n\ntrace:\n{backtrace:?}");
}));

// global threadpool
rayon_core::ThreadPoolBuilder::new()
.panic_handler(|e| {
let backtrace = Backtrace::new();
shutdown_terminal();
log_eprintln!("\nGitUI was close due to an unexpected panic.\nPlease file an issue on https://github.com/gitui-org/gitui/issues with the following info:\n\n{:?}\ntrace:\n{:?}", e, backtrace);
process::abort();
})
.num_threads(4)
.build_global()?;

Expand Down
Loading