Skip to content

Commit

Permalink
Add eyre example
Browse files Browse the repository at this point in the history
  • Loading branch information
Finomnis committed Apr 20, 2022
1 parent a32d9fd commit 621173b
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ log = "0.4"
[dev-dependencies]
# Error propagation
anyhow = "1.0"
eyre = "0.6"
miette = { version = "4.4", features = ["fancy"] }

# Logging
Expand Down
28 changes: 28 additions & 0 deletions examples/17_with_eyre.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//! This example shows how to use this library with eyre instead of anyhow
use env_logger::{Builder, Env};
use eyre::{eyre, Result};
use tokio::time::{sleep, Duration};
use tokio_graceful_shutdown::{SubsystemHandle, Toplevel};

async fn subsys1(_subsys: SubsystemHandle) -> Result<()> {
log::info!("Subsystem1 started.");
sleep(Duration::from_millis(500)).await;
log::info!("Subsystem1 stopped.");

// Task ends with an error. This should cause the main program to shutdown.
Err(eyre!("Subsystem1 threw an error."))
}

#[tokio::main]
async fn main() -> Result<()> {
// Init logging
Builder::from_env(Env::default().default_filter_or("debug")).init();

// Create toplevel
Toplevel::new()
.start("Subsys1", subsys1)
.catch_signals()
.handle_shutdown_requests(Duration::from_millis(1000))
.await
}

0 comments on commit 621173b

Please sign in to comment.