Skip to main content

tokio_process_tools/
error.rs

1//! Error types for process operations.
2
3use std::borrow::Cow;
4use std::io;
5use std::time::Duration;
6use thiserror::Error;
7
8use crate::CollectorError;
9
10/// Errors that can occur when terminating a process.
11#[derive(Debug, Error)]
12pub enum TerminationError {
13    /// Failed to send a signal to the process.
14    #[error("Failed to send '{signal}' signal to process '{process_name}': {source}")]
15    SignallingFailed {
16        /// The name of the process.
17        process_name: Cow<'static, str>,
18        /// The underlying IO error.
19        source: io::Error,
20        /// The signal that could not be sent.
21        signal: &'static str,
22    },
23
24    /// Failed to terminate the process after trying all signals (SIGINT, SIGTERM, SIGKILL).
25    #[error(
26        "Failed to terminate process '{process_name}'. SIGINT failed: {sigint_error}. SIGTERM failed: {sigterm_error}. SIGKILL failed: {sigkill_error}"
27    )]
28    TerminationFailed {
29        /// The name of the process.
30        process_name: Cow<'static, str>,
31        /// Error from SIGINT attempt.
32        sigint_error: String,
33        /// Error from SIGTERM attempt.
34        sigterm_error: String,
35        /// Error from SIGKILL attempt.
36        #[source]
37        sigkill_error: io::Error,
38    },
39}
40
41/// Errors that can occur when waiting for process operations.
42#[derive(Debug, Error)]
43pub enum WaitError {
44    /// A general IO error occurred.
45    #[error("IO error occurred while waiting for process '{process_name}': {source}")]
46    IoError {
47        /// The name of the process.
48        process_name: Cow<'static, str>,
49        /// The underlying IO error.
50        #[source]
51        source: io::Error,
52    },
53
54    /// Wait operation timed out.
55    #[error("Process '{process_name}' did not complete within {timeout:?}")]
56    Timeout {
57        /// The name of the process.
58        process_name: Cow<'static, str>,
59        /// The timeout duration that was exceeded.
60        timeout: Duration,
61    },
62
63    /// Could not terminate the process.
64    #[error("Could not terminate process: {0}")]
65    TerminationError(#[from] TerminationError),
66
67    /// Collector failed to collect output.
68    #[error("Collector failed to collect output: {0}")]
69    CollectorFailed(#[from] CollectorError),
70}
71
72/// Errors that can occur when spawning a process.
73#[derive(Debug, Error)]
74pub enum SpawnError {
75    /// Failed to spawn the process.
76    #[error("Failed to spawn process '{process_name}': {source}")]
77    SpawnFailed {
78        /// The name or description of the process being spawned.
79        process_name: Cow<'static, str>,
80        /// The underlying IO error.
81        #[source]
82        source: io::Error,
83    },
84}
85
86/// Result of waiting for an output line matching a predicate.
87///
88/// Note that not every function returning this enum can produce every variant:
89/// [`crate::output_stream::broadcast::BroadcastOutputStream::wait_for_line`] and
90/// [`crate::output_stream::single_subscriber::SingleSubscriberOutputStream::wait_for_line`]
91/// never return [`WaitForLineResult::Timeout`]. The timeout variant is only produced by the
92/// corresponding `*_with_timeout` methods.
93#[derive(Debug, Clone, Copy, PartialEq, Eq)]
94pub enum WaitForLineResult {
95    /// A matching line was observed before the stream ended or the timeout elapsed.
96    Matched,
97
98    /// The stream ended before any matching line was observed.
99    StreamClosed,
100
101    /// The timeout elapsed before a matching line was observed or the stream ended.
102    Timeout,
103}