Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move error types to mod 'errors' #35

Merged
merged 1 commit into from May 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 11 additions & 4 deletions examples/18_error_type_passthrough.rs
Expand Up @@ -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 {
Expand Down Expand Up @@ -57,6 +60,10 @@ async fn subsys5(_subsys: SubsystemHandle<MyError>) -> 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]
Expand Down Expand Up @@ -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) => {
Expand All @@ -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)
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/errors.rs
@@ -1,3 +1,5 @@
//! All the errors that can be caused by this crate.

use miette::Diagnostic;
use thiserror::Error;

Expand Down
6 changes: 1 addition & 5 deletions src/lib.rs
Expand Up @@ -102,7 +102,7 @@ impl<T> ErrTypeTraits for T where
{
}

mod errors;
pub mod errors;
mod exit_state;
mod into_subsystem;
mod runner;
Expand All @@ -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;
Expand Down
6 changes: 5 additions & 1 deletion 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;
Expand Down
2 changes: 1 addition & 1 deletion src/subsystem/data.rs
Expand Up @@ -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<ErrType: ErrTypeTraits> SubsystemData<ErrType> {
pub fn new(
Expand Down
2 changes: 1 addition & 1 deletion src/subsystem/handle.rs
Expand Up @@ -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)]
Expand Down
2 changes: 1 addition & 1 deletion src/subsystem/mod.rs
Expand Up @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion src/toplevel.rs
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions tests/integration_test.rs
Expand Up @@ -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;
Expand Down