Skip to main content

torrust_tracker_deployer_lib/adapters/network/
error.rs

1//! Network diagnostic tool error types
2
3use thiserror::Error;
4
5use crate::shared::command::CommandError;
6
7/// Errors that can occur during network diagnostic operations
8#[derive(Debug, Error)]
9pub enum NetworkError {
10    /// Failed to execute netstat command
11    #[error(
12        "Failed to execute netstat command
13Tip: Verify netstat is installed: 'which netstat' or 'apt-get install net-tools'"
14    )]
15    NetstatFailed(#[source] CommandError),
16
17    /// Failed to execute ss command
18    #[error(
19        "Failed to execute ss command
20Tip: Verify ss is installed: 'which ss' or 'apt-get install iproute2'"
21    )]
22    SsFailed(#[source] CommandError),
23}
24
25impl NetworkError {
26    /// Get detailed troubleshooting guidance for this error
27    ///
28    /// This method provides comprehensive troubleshooting steps that can be
29    /// displayed to users when they need more help resolving the error.
30    ///
31    /// # Example
32    ///
33    /// ```rust,no_run
34    /// use torrust_tracker_deployer_lib::adapters::network::NetstatClient;
35    ///
36    /// # fn example() {
37    /// let netstat = NetstatClient::new();
38    ///
39    /// if let Err(e) = netstat.list_tcp_listening_ports() {
40    ///     eprintln!("Error: {e}");
41    ///     eprintln!("\nTroubleshooting:\n{}", e.help());
42    /// }
43    /// # }
44    /// ```
45    #[must_use]
46    pub fn help(&self) -> &'static str {
47        match self {
48            Self::NetstatFailed(_) => {
49                "Netstat Command Failed - Detailed Troubleshooting:
50
511. Check if netstat is installed:
52   which netstat
53
542. Install netstat if missing:
55   Debian/Ubuntu: sudo apt-get install net-tools
56   RHEL/CentOS: sudo yum install net-tools
57   Alpine: sudo apk add net-tools
58   macOS: netstat is pre-installed
59
603. Verify permissions:
61   - Some netstat options require root/sudo for full process information
62   - Try running the command manually: netstat -tlnp
63
644. Alternative: Use ss command instead (modern Linux):
65   - ss is the modern replacement for netstat
66   - Usually available via iproute2 package
67
68For more information, see: man netstat"
69            }
70
71            Self::SsFailed(_) => {
72                "SS Command Failed - Detailed Troubleshooting:
73
741. Check if ss is installed:
75   which ss
76
772. Install ss if missing:
78   Debian/Ubuntu: sudo apt-get install iproute2
79   RHEL/CentOS: sudo yum install iproute2
80   Alpine: sudo apk add iproute2
81   macOS: ss is not available, use netstat instead
82
833. Verify permissions:
84   - Some ss options require root/sudo for full process information
85   - Try running the command manually: ss -tlnp
86
874. Alternative: Use netstat command instead:
88   - netstat is the traditional tool for network statistics
89   - Usually available via net-tools package
90
91For more information, see: man ss"
92            }
93        }
94    }
95}