diff --git a/examples/18_error_type_passthrough.rs b/examples/18_error_type_passthrough.rs index c97abc4..f25fec5 100644 --- a/examples/18_error_type_passthrough.rs +++ b/examples/18_error_type_passthrough.rs @@ -3,7 +3,10 @@ use env_logger::{Builder, Env}; use tokio::time::{sleep, Duration}; -use tokio_graceful_shutdown::{GracefulShutdownError, IntoSubsystem, SubsystemHandle, Toplevel}; +use tokio_graceful_shutdown::{ + errors::{GracefulShutdownError, SubsystemError}, + IntoSubsystem, SubsystemHandle, Toplevel, +}; #[derive(Debug, thiserror::Error)] enum MyError { @@ -57,6 +60,10 @@ async fn subsys5(_subsys: SubsystemHandle) -> Result<(), MyError> { Ok(()) } +// This subsystem implements the IntoSubsystem trait with a custom error type. +// The first generic is the error type returned from the `run()` function, the +// second generic is the error wrapper type used by Toplevel. In this case, +// both are identical. struct Subsys6; #[async_trait::async_trait] @@ -99,7 +106,7 @@ async fn main() -> Result<(), miette::Report> { for subsystem_error in e.get_subsystem_errors() { match subsystem_error { - tokio_graceful_shutdown::SubsystemError::Failed(name, e) => { + SubsystemError::Failed(name, e) => { log::warn!(" Subsystem '{}' failed.", name); match e.get_error() { MyError::WithData(data) => { @@ -110,10 +117,10 @@ async fn main() -> Result<(), miette::Report> { } } } - tokio_graceful_shutdown::SubsystemError::Cancelled(name) => { + SubsystemError::Cancelled(name) => { log::warn!(" Subsystem '{}' was cancelled.", name) } - tokio_graceful_shutdown::SubsystemError::Panicked(name) => { + SubsystemError::Panicked(name) => { log::warn!(" Subsystem '{}' panicked.", name) } } diff --git a/src/errors.rs b/src/errors.rs index c619081..8747787 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -1,3 +1,5 @@ +//! All the errors that can be caused by this crate. + use miette::Diagnostic; use thiserror::Error; diff --git a/src/lib.rs b/src/lib.rs index 2ae9183..6c2d73f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -102,7 +102,7 @@ impl ErrTypeTraits for T where { } -mod errors; +pub mod errors; mod exit_state; mod into_subsystem; mod runner; @@ -112,10 +112,6 @@ mod subsystem; mod toplevel; mod utils; -pub use errors::GracefulShutdownError; -pub use errors::PartialShutdownError; -pub use errors::SubsystemError; -pub use errors::SubsystemFailure; pub use into_subsystem::IntoSubsystem; pub use shutdown_token::ShutdownToken; pub use subsystem::NestedSubsystem; diff --git a/src/runner.rs b/src/runner.rs index af76dfb..6dc01f6 100644 --- a/src/runner.rs +++ b/src/runner.rs @@ -1,4 +1,8 @@ -use crate::{utils::ShutdownGuard, ErrTypeTraits, ShutdownToken, SubsystemError, SubsystemFailure}; +use crate::{ + errors::{SubsystemError, SubsystemFailure}, + utils::ShutdownGuard, + ErrTypeTraits, ShutdownToken, +}; use std::{future::Future, sync::Arc}; use tokio::task::{JoinError, JoinHandle}; use tokio_util::sync::CancellationToken; diff --git a/src/subsystem/data.rs b/src/subsystem/data.rs index dbe2f50..93fd770 100644 --- a/src/subsystem/data.rs +++ b/src/subsystem/data.rs @@ -13,13 +13,13 @@ use super::PartialShutdownError; use super::SubsystemData; use super::SubsystemDescriptor; use super::SubsystemIdentifier; +use crate::errors::SubsystemError; use crate::exit_state::prettify_exit_states; use crate::exit_state::{join_shutdown_results, ShutdownResults, SubprocessExitState}; use crate::runner::SubsystemRunner; use crate::shutdown_token::ShutdownToken; use crate::utils::ShutdownGuard; use crate::ErrTypeTraits; -use crate::SubsystemError; impl SubsystemData { pub fn new( diff --git a/src/subsystem/handle.rs b/src/subsystem/handle.rs index 3122b48..a1a7552 100644 --- a/src/subsystem/handle.rs +++ b/src/subsystem/handle.rs @@ -4,9 +4,9 @@ use std::sync::Arc; use super::NestedSubsystem; use super::SubsystemData; use super::SubsystemHandle; +use crate::errors::PartialShutdownError; use crate::runner::SubsystemRunner; use crate::ErrTypeTraits; -use crate::PartialShutdownError; use crate::ShutdownToken; #[cfg(doc)] diff --git a/src/subsystem/mod.rs b/src/subsystem/mod.rs index 56d6fcf..dcd04cf 100644 --- a/src/subsystem/mod.rs +++ b/src/subsystem/mod.rs @@ -8,11 +8,11 @@ use std::sync::Weak; use tokio_util::sync::CancellationToken; +use crate::errors::PartialShutdownError; use crate::runner::SubsystemRunner; use crate::shutdown_token::ShutdownToken; use crate::utils::ShutdownGuard; use crate::ErrTypeTraits; -use crate::PartialShutdownError; use self::identifier::SubsystemIdentifier; diff --git a/src/toplevel.rs b/src/toplevel.rs index eb787a8..f7f5d60 100644 --- a/src/toplevel.rs +++ b/src/toplevel.rs @@ -6,13 +6,13 @@ use std::time::Duration; use tokio_util::sync::CancellationToken; +use crate::errors::GracefulShutdownError; use crate::exit_state::prettify_exit_states; use crate::shutdown_token::create_shutdown_token; use crate::signal_handling::wait_for_signal; use crate::utils::wait_forever; use crate::utils::ShutdownGuard; use crate::ErrTypeTraits; -use crate::GracefulShutdownError; use crate::{ShutdownToken, SubsystemHandle}; use super::subsystem::SubsystemData; diff --git a/tests/integration_test.rs b/tests/integration_test.rs index 053d7fd..a017707 100644 --- a/tests/integration_test.rs +++ b/tests/integration_test.rs @@ -2,8 +2,8 @@ use anyhow::anyhow; use env_logger; use tokio::time::{sleep, timeout, Duration}; use tokio_graceful_shutdown::{ - GracefulShutdownError, IntoSubsystem, PartialShutdownError, SubsystemError, SubsystemHandle, - Toplevel, + errors::{GracefulShutdownError, PartialShutdownError, SubsystemError}, + IntoSubsystem, SubsystemHandle, Toplevel, }; mod common;