Skip to main content

mod_signal/
error.rs

1//! Error type and `Result` alias.
2
3use core::fmt;
4use std::io;
5
6use crate::signal::Signal;
7
8/// Errors produced by `mod-signal` operations.
9#[non_exhaustive]
10#[derive(Debug)]
11pub enum Error {
12    /// `Coordinator::install` was called twice on the same
13    /// coordinator. Signal handler installation is idempotent within
14    /// a coordinator and the second call is rejected.
15    AlreadyInstalled,
16
17    /// The platform rejected registration of a specific signal.
18    SignalRegistration {
19        /// Which signal failed to register.
20        signal: Signal,
21        /// The underlying OS error.
22        source: io::Error,
23    },
24
25    /// The coordinator was used in a state that disallows the
26    /// requested operation. Carries a static description.
27    InvalidState(&'static str),
28
29    /// A timed operation exceeded its budget. Carries a static
30    /// description of which operation.
31    Timeout(&'static str),
32
33    /// `Coordinator::install` was called with no async-runtime
34    /// feature and no `ctrlc-fallback` feature enabled, so there is
35    /// no back-end available to attach signal handlers.
36    NoRuntime,
37}
38
39impl fmt::Display for Error {
40    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
41        match self {
42            Self::AlreadyInstalled => {
43                f.write_str("signal handlers already installed for this coordinator")
44            }
45            Self::SignalRegistration { signal, source } => {
46                write!(f, "failed to register handler for {signal}: {source}")
47            }
48            Self::InvalidState(s) => write!(f, "invalid coordinator state: {s}"),
49            Self::Timeout(s) => write!(f, "operation timed out: {s}"),
50            Self::NoRuntime => f.write_str(
51                "no async-runtime feature and no ctrlc-fallback feature enabled; \
52                 enable `tokio`, `async-std`, or `ctrlc-fallback`",
53            ),
54        }
55    }
56}
57
58impl std::error::Error for Error {
59    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
60        match self {
61            Self::SignalRegistration { source, .. } => Some(source),
62            _ => None,
63        }
64    }
65}
66
67/// Convenience alias for `Result<T, mod_signal::Error>`.
68pub type Result<T> = core::result::Result<T, Error>;