Skip to main content

ez_rust_discovery/
error.rs

1use std::io;
2use std::net::AddrParseError;
3use std::num::ParseIntError;
4
5/// Crate-wide `Result` alias.
6pub type Result<T, E = Error> = std::result::Result<T, E>;
7
8/// Errors produced during service-discovery operations.
9#[derive(Debug, thiserror::Error)]
10#[non_exhaustive]
11pub enum Error {
12    /// I/O error, typically from the underlying network or runtime setup.
13    #[error("I/O error: {0}")]
14    Io(#[from] io::Error),
15
16    /// Failed to read an environment variable.
17    #[error("missing or invalid environment variable `{name}`: {source}")]
18    Env {
19        /// Name of the environment variable.
20        name: String,
21        /// Underlying cause.
22        #[source]
23        source: std::env::VarError,
24    },
25
26    /// Failed to parse a socket address.
27    #[error("invalid socket address: {0}")]
28    AddrParse(#[from] AddrParseError),
29
30    /// Failed to parse a port number.
31    #[error("invalid port number: {0}")]
32    PortParse(#[from] ParseIntError),
33
34    /// Failed to detect the local IP address.
35    #[error("failed to detect local IP: {0}")]
36    LocalIp(#[from] local_ip_address::Error),
37
38    /// Error raised by the Nacos client during build or registration.
39    #[error("nacos error: {0}")]
40    Nacos(#[from] nacos_sdk::api::error::Error),
41
42    /// Invalid configuration (missing required fields, malformed address, etc.).
43    #[error("invalid configuration: {0}")]
44    InvalidConfig(String),
45}
46
47impl Error {
48    /// Build an [`Error::InvalidConfig`].
49    pub(crate) fn invalid_config(msg: impl Into<String>) -> Self {
50        Self::InvalidConfig(msg.into())
51    }
52}