Skip to content

Commit d8b5a4b

Browse files
author
Stephan Dilly
committed
remove panic usage entirely
1 parent ad09282 commit d8b5a4b

File tree

3 files changed

+18
-10
lines changed

3 files changed

+18
-10
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1313
- log tab refreshes when head changes ([#78](https://github.com/extrawurst/gitui/issues/78))
1414
- performance optimization of the log tab in big repos
1515
- more readable default color for the commit hash in the log tab
16+
- more error/panic resiliance (`unwrap`/`panic` denied by clippy now)
1617

1718
### Fixes
1819
- panic on small terminal width ([#72](https://github.com/extrawurst/gitui/issues/72))

src/app.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use crate::{
1111
tabs::{Revlog, Stashing, Status},
1212
ui::style::Theme,
1313
};
14-
use anyhow::Result;
14+
use anyhow::{anyhow, Result};
1515
use asyncgit::{sync, AsyncNotification, CWD};
1616
use crossbeam_channel::Sender;
1717
use crossterm::event::Event;
@@ -96,7 +96,7 @@ impl App {
9696
0 => self.status_tab.draw(f, chunks_main[1])?,
9797
1 => self.revlog.draw(f, chunks_main[1])?,
9898
2 => self.stashing_tab.draw(f, chunks_main[1])?,
99-
_ => panic!("unknown tab"),
99+
_ => return Err(anyhow!("unknown tab")),
100100
};
101101

102102
Self::draw_commands(

src/main.rs

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
#![allow(clippy::cargo::multiple_crate_versions)]
66
#![deny(clippy::pedantic)]
77
#![deny(clippy::result_unwrap_used)]
8+
#![deny(clippy::panic)]
89
#![allow(clippy::module_name_repetitions)]
9-
use anyhow::{anyhow, Result};
1010

1111
mod app;
1212
mod components;
@@ -20,6 +20,7 @@ mod ui;
2020
mod version;
2121

2222
use crate::{app::App, poll::QueueEvent};
23+
use anyhow::{anyhow, Result};
2324
use asyncgit::AsyncNotification;
2425
use backtrace::Backtrace;
2526
use crossbeam_channel::{tick, unbounded, Receiver, Select};
@@ -30,7 +31,6 @@ use crossterm::{
3031
},
3132
ExecutableCommand,
3233
};
33-
use log::error;
3434
use scopeguard::defer;
3535
use scopetime::scope_time;
3636
use simplelog::{Config, LevelFilter, WriteLogger};
@@ -41,6 +41,7 @@ use std::{
4141
io::{self, Write},
4242
panic,
4343
path::PathBuf,
44+
process,
4445
time::{Duration, Instant},
4546
};
4647
use tui::{
@@ -177,7 +178,7 @@ fn select_event(
177178
3 => oper
178179
.recv(rx_spinner)
179180
.map(|_| events.push(QueueEvent::SpinnerUpdate)),
180-
_ => panic!("unknown select source"),
181+
_ => return Err(anyhow!("unknown select source")),
181182
}?;
182183

183184
Ok(events)
@@ -222,17 +223,23 @@ fn set_panic_handlers() -> Result<()> {
222223
// regular panic handler
223224
panic::set_hook(Box::new(|e| {
224225
let backtrace = Backtrace::new();
225-
error!("panic: {:?}\ntrace:\n{:?}", e, backtrace);
226+
log::error!("panic: {:?}\ntrace:\n{:?}", e, backtrace);
226227
shutdown_terminal().expect("shutdown failed inside panic");
227228
eprintln!("panic: {:?}\ntrace:\n{:?}", e, backtrace);
228229
}));
229230

230231
// global threadpool
231-
Ok(rayon_core::ThreadPoolBuilder::new()
232+
rayon_core::ThreadPoolBuilder::new()
232233
.panic_handler(|e| {
233-
error!("thread panic: {:?}", e);
234-
panic!(e)
234+
let backtrace = Backtrace::new();
235+
log::error!("panic: {:?}\ntrace:\n{:?}", e, backtrace);
236+
shutdown_terminal()
237+
.expect("shutdown failed inside panic");
238+
eprintln!("panic: {:?}\ntrace:\n{:?}", e, backtrace);
239+
process::abort();
235240
})
236241
.num_threads(4)
237-
.build_global()?)
242+
.build_global()?;
243+
244+
Ok(())
238245
}

0 commit comments

Comments
 (0)