Skip to content

Commit

Permalink
error handling part 1
Browse files Browse the repository at this point in the history
  • Loading branch information
Kacperacy committed Apr 18, 2024
1 parent c57dd46 commit 551ad51
Show file tree
Hide file tree
Showing 4 changed files with 236 additions and 0 deletions.
200 changes: 200 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
color-eyre = "0.6.3"
crossterm = "0.27.0"
ratatui = "0.26.2"
28 changes: 28 additions & 0 deletions src/errors.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
use std::panic;

use color_eyre::{
config::HookBuilder,
eyre::{self, Ok},
};

use crate::tui;

pub fn install_hooks() -> color_eyre::Result<()> {
let (panic_hook, eyre_hook) = HookBuilder::default().into_hooks();

let panic_hook = panic_hook.into_panic_hook();
panic::set_hook(Box::new(move |panic_info| {
tui::restore().unwrap();
panic_hook(panic_info);
}));

let eyre_hook = eyre_hook.into_eyre_hook();
eyre::set_hook(Box::new(
move |error: &(dyn std::error::Error + 'static)| {
tui::restore().unwrap();
eyre_hook(error)
},
))?;

Ok(())
}
7 changes: 7 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,16 @@ use ratatui::{
widgets::{block::*, *},
};

use color_eyre::{
eyre::{bail, WrapErr},
Result,
};

mod errors;
mod tui;

fn main() -> io::Result<()> {
errors::install_hooks()?;
let mut terminal = tui::init()?;
let app_result = App::default().run(&mut terminal);
tui::restore()?;
Expand Down

0 comments on commit 551ad51

Please sign in to comment.