Skip to content

Commit ee5c243

Browse files
authored
Improve error messages (#2617)
1 parent ae7b7b0 commit ee5c243

File tree

2 files changed

+23
-25
lines changed

2 files changed

+23
-25
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1616
* add `use_selection_fg` to theme file to allow customizing selection foreground color [[@Upsylonbare](https://github.com/Upsylonbare)] ([#2515](https://github.com/gitui-org/gitui/pull/2515))
1717

1818
### Changed
19+
* improve error messages [[@acuteenvy](https://github.com/acuteenvy)] ([#2617](https://github.com/gitui-org/gitui/pull/2617))
1920
* increase MSRV from 1.70 to 1.81 [[@naseschwarz](https://github.com/naseschwarz)] ([#2094](https://github.com/gitui-org/gitui/issues/2094))
2021
* improve syntax highlighting file detection [[@acuteenvy](https://github.com/acuteenvy)] ([#2524](https://github.com/extrawurst/gitui/pull/2524))
2122
* Updated project links to point to `gitui-org` instead of `extrawurst` [[@vasleymus](https://github.com/vasleymus)] ([#2538](https://github.com/gitui-org/gitui/pull/2538))

src/main.rs

+22-25
Original file line numberDiff line numberDiff line change
@@ -119,20 +119,24 @@ enum Updater {
119119
NotifyWatcher,
120120
}
121121

122+
/// Do `log::error!` and `eprintln!` in one line.
123+
macro_rules! log_eprintln {
124+
( $($arg:tt)* ) => {{
125+
log::error!($($arg)*);
126+
eprintln!($($arg)*);
127+
}};
128+
}
129+
122130
fn main() -> Result<()> {
123131
let app_start = Instant::now();
124132

125133
let cliargs = process_cmdline()?;
126134

127135
asyncgit::register_tracing_logging();
128-
129-
if !valid_path(&cliargs.repo_path) {
130-
eprintln!("invalid path\nplease run gitui inside of a non-bare git repository");
131-
return Ok(());
132-
}
136+
ensure_valid_path(&cliargs.repo_path)?;
133137

134138
let key_config = KeyConfig::init()
135-
.map_err(|e| eprintln!("KeyConfig loading error: {e}"))
139+
.map_err(|e| log_eprintln!("KeyConfig loading error: {e}"))
136140
.unwrap_or_default();
137141
let theme = Theme::init(&cliargs.theme);
138142

@@ -141,7 +145,7 @@ fn main() -> Result<()> {
141145
shutdown_terminal();
142146
}
143147

144-
set_panic_handlers()?;
148+
set_panic_handler()?;
145149

146150
let mut repo_path = cliargs.repo_path;
147151
let mut terminal = start_terminal(io::stdout(), &repo_path)?;
@@ -291,13 +295,13 @@ fn shutdown_terminal() {
291295
io::stdout().execute(LeaveAlternateScreen).map(|_f| ());
292296

293297
if let Err(e) = leave_screen {
294-
eprintln!("leave_screen failed:\n{e}");
298+
log::error!("leave_screen failed:\n{e}");
295299
}
296300

297301
let leave_raw_mode = disable_raw_mode();
298302

299303
if let Err(e) = leave_raw_mode {
300-
eprintln!("leave_raw_mode failed:\n{e}");
304+
log::error!("leave_raw_mode failed:\n{e}");
301305
}
302306
}
303307

@@ -315,12 +319,14 @@ fn draw(terminal: &mut Terminal, app: &App) -> io::Result<()> {
315319
Ok(())
316320
}
317321

318-
fn valid_path(repo_path: &RepoPath) -> bool {
319-
let error = asyncgit::sync::repo_open_error(repo_path);
320-
if let Some(error) = &error {
321-
log::error!("repo open error: {error}");
322+
fn ensure_valid_path(repo_path: &RepoPath) -> Result<()> {
323+
match asyncgit::sync::repo_open_error(repo_path) {
324+
Some(e) => {
325+
log::error!("{e}");
326+
bail!(e)
327+
}
328+
None => Ok(()),
322329
}
323-
error.is_none()
324330
}
325331

326332
fn select_event(
@@ -388,20 +394,11 @@ fn start_terminal(
388394
Ok(terminal)
389395
}
390396

391-
// do log::error! and eprintln! in one line, pass string, error and backtrace
392-
macro_rules! log_eprintln {
393-
($string:expr, $e:expr, $bt:expr) => {
394-
log::error!($string, $e, $bt);
395-
eprintln!($string, $e, $bt);
396-
};
397-
}
398-
399-
fn set_panic_handlers() -> Result<()> {
400-
// regular panic handler
397+
fn set_panic_handler() -> Result<()> {
401398
panic::set_hook(Box::new(|e| {
402399
let backtrace = Backtrace::new();
403400
shutdown_terminal();
404-
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);
401+
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:?}");
405402
}));
406403

407404
// global threadpool

0 commit comments

Comments
 (0)