Skip to main content

terminator/
error.rs

1//! Error type for the crate.
2
3use std::fmt;
4
5/// Convenience alias used throughout the crate.
6pub type Result<T> = std::result::Result<T, Error>;
7
8/// Something that went wrong while trying to stop or message a process.
9#[derive(Debug)]
10#[non_exhaustive]
11pub enum Error {
12    /// The target process does not exist (any more).
13    ProcessNotFound(u32),
14    /// The target process has no console attached, so console control events
15    /// and console input cannot be delivered to it.
16    NoConsole(u32),
17    /// The target process has no visible top-level window to close.
18    NoWindow(u32),
19    /// A detached helper process was required but could not be used.
20    ///
21    /// The string explains what to configure (for example, a library loaded
22    /// as a DLL into a foreign host that owns its own console).
23    HelperUnavailable(String),
24    /// The helper process ran but reported an unexpected exit code.
25    HelperFailed {
26        /// Raw process exit code of the helper.
27        code: i32,
28        /// Short description of what the helper was attempting.
29        what: &'static str,
30    },
31    /// The operation is not supported on this platform or in this context.
32    Unsupported(&'static str),
33    /// An underlying OS call failed.
34    Os(std::io::Error),
35}
36
37impl fmt::Display for Error {
38    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
39        match self {
40            Error::ProcessNotFound(pid) => write!(f, "process {pid} does not exist"),
41            Error::NoConsole(pid) => write!(f, "process {pid} has no console attached"),
42            Error::NoWindow(pid) => write!(f, "process {pid} has no visible main window"),
43            Error::HelperUnavailable(why) => write!(f, "helper process unavailable: {why}"),
44            Error::HelperFailed { code, what } => {
45                write!(f, "helper process failed while {what} (exit code {code})")
46            }
47            Error::Unsupported(what) => write!(f, "unsupported: {what}"),
48            Error::Os(e) => write!(f, "OS error: {e}"),
49        }
50    }
51}
52
53impl std::error::Error for Error {
54    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
55        match self {
56            Error::Os(e) => Some(e),
57            _ => None,
58        }
59    }
60}
61
62impl From<std::io::Error> for Error {
63    fn from(e: std::io::Error) -> Self {
64        Error::Os(e)
65    }
66}