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, InspectorError};
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/// Errors that can occur when waiting for output patterns.
87#[derive(Debug, Error)]
88pub enum OutputError {
89    /// Waiting for output pattern timed out.
90    #[error("Timed out after {timeout:?} waiting for output pattern on {stream_name}")]
91    Timeout {
92        /// The name of the stream.
93        stream_name: &'static str,
94        /// The timeout duration that was exceeded.
95        timeout: Duration,
96    },
97
98    /// Inspector task failed.
99    #[error("Inspector failed: {0}")]
100    InspectorFailed(#[from] InspectorError),
101}