Skip to content

Commit

Permalink
refactor(app): refactor error handling (#400)
Browse files Browse the repository at this point in the history
* refactor(app): refactor error handling

* refactor
  • Loading branch information
kyu08 authored Dec 19, 2024
1 parent 826fa11 commit 9400938
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 38 deletions.
9 changes: 3 additions & 6 deletions src/controller/controller_main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,9 @@ pub async fn run() {
let command_line_args = env::args().collect();
let usecase = args_to_usecase(command_line_args);

match usecase.run().await {
Err(e) => {
print_error(&e);
std::process::exit(1);
}
Ok(_) => std::process::exit(0),
if let Err(e) = usecase.run().await {
print_error(&e);
std::process::exit(1);
}
}

Expand Down
5 changes: 3 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ mod usecase;

#[tokio::main]
async fn main() {
let result = controller::controller_main::run();
// ref: https://zenn.dev/techno_tanoc/articles/4c207397df3ab0#assertunwindsafe
let res = AssertUnwindSafe(result).catch_unwind().await;
let res = AssertUnwindSafe(controller::controller_main::run())
.catch_unwind()
.await;

if let Err(e) = res {
println!("{}", err::any_to_string::any_to_string(&*e));
Expand Down
50 changes: 20 additions & 30 deletions src/usecase/tui/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ use std::{
env,
io::{self, Stderr},
path::PathBuf,
process,
sync::{Arc, Mutex},
};
use std::{panic::AssertUnwindSafe, time::Duration};
Expand Down Expand Up @@ -174,43 +173,34 @@ pub async fn main(config: config::Config) -> Result<()> {
let mut terminal = Terminal::new(backend)?;

let result = AssertUnwindSafe(async {
let mut model = match Model::new(config) {
Ok(m) => m,
Err(e) => {
shutdown_terminal(&mut terminal)?;
return Err(e);
}
};

let command = match run(&mut terminal, &mut model).await {
Ok(t) => t,
Err(e) => {
shutdown_terminal(&mut terminal)?;
return Err(e);
}
};

shutdown_terminal(&mut terminal)?;

match command {
Some((runner, command)) => {
runner.show_command(&command);
let _ = runner.execute(&command); // TODO: handle error
Ok(())
match Model::new(config) {
Ok(mut m) => {
match run(&mut terminal, &mut m).await {
// If async closure will be stabilized, use map instead of match
Ok(command) => match command {
Some((runner, command)) => Ok(Some((runner, command))),
None => Ok(None), // If no command selected, show nothing.
},
Err(e) => Err(e),
}
}
None => Ok(()),
Err(e) => Err(e),
}
})
.catch_unwind()
.await;

shutdown_terminal(&mut terminal)?;

match result {
Ok(usecase_result) => usecase_result,
Err(e) => {
shutdown_terminal(&mut terminal)?;
println!("{}", any_to_string::any_to_string(&*e));
process::exit(1);
// some kind of command was selected
Ok(Ok(Some((runner, command)))) => {
runner.show_command(&command);
runner.execute(&command)
}
Ok(Ok(None)) => Ok(()), // no command was selected
Ok(Err(e)) => Err(e), // Model::new or run returned Err
Err(e) => Err(anyhow!(any_to_string::any_to_string(&*e))), // panic occurred
}
}

Expand Down

0 comments on commit 9400938

Please sign in to comment.