Mercurial > crates > systemd-socket
view src/error.rs @ 13:f740dadd2948
Added enable_systemd feature
This feature makes systemd support optional, on by default. While it may
seem strange that this feature exists, it makes sense for authors of
applications who want to make systemd optional. Thanks to this feature
the interface stays the same, it just fails to parse `systemd://`
addresses with a helpful error message.
author | Martin Habovstiak <martin.habovstiak@gmail.com> |
---|---|
date | Thu, 03 Dec 2020 16:34:09 +0100 |
parents | a7893294e9b2 |
children | bc76507dd878 |
line wrap: on
line source
//! Error types that can occur when dealing with `SocketAddr` //! //! This module separates the error types from root module to avoid clutter. use thiserror::Error; use std::io; /// Error that can occur during parsing of `SocketAddr` from a string /// /// This encapsulates possible errors that can occur when parsing the input. /// It is currently opaque because the representation is not certain yet. /// It can be displayed using the standard `Error` trait. #[derive(Debug, Error)] #[error(transparent)] pub struct ParseError(#[from] pub(crate) ParseErrorInner); #[derive(Debug, Error)] pub(crate) enum ParseErrorInner { #[error("failed to parse socket address")] ResolvAddr(#[from] crate::resolv_addr::ResolvAddrError), #[cfg(all(linux, feature = "enable_systemd"))] #[error("invalid character '{c}' in systemd socket name {string} at position {pos}")] InvalidCharacter { string: String, c: char, pos: usize, }, #[cfg(all(linux, feature = "enable_systemd"))] #[error("systemd socket name {string} is {len} characters long which is more than the limit 255")] LongSocketName { string: String, len: usize, }, #[cfg(not(all(linux, feature = "enable_systemd")))] #[cfg_attr(not(linux), error("can't parse {0} because systemd is not supported on this operating system"))] #[cfg_attr(linux, error("can't parse {0} because systemd support was disabled during build"))] SystemdUnsupported(String), } /// Error that can occur during parsing of `SocketAddr` from a `OsStr`/`OsString` /// /// As opposed to parsing from `&str` or `String`, parsing from `OsStr` can fail due to one more /// reason: invalid UTF-8. This error type expresses this possibility and is returned whenever such /// conversion is attempted. It is not opaque because the possible variants are pretty much /// certain, but it may contain `ParseError` which is opaque. /// /// This error can be displayed using standard `Error` trait. /// See `ParseError` for more information. #[derive(Debug, Error)] pub enum ParseOsStrError { /// The input was not a valid UTF-8 string #[error("the address is not a valid UTF-8 string")] InvalidUtf8, /// The input was a valid UTF-8 string but the address was invalid #[error(transparent)] InvalidAddress(#[from] ParseError), } /// Error that can occur during binding of a socket /// /// This encapsulates possible errors that can occur when binding a socket or receiving a socket /// from systemd. /// It is currently opaque because the representation is not certain yet. /// It can be displayed using the standard `Error` trait. #[derive(Debug, Error)] #[error(transparent)] pub struct BindError(#[from] pub(crate) BindErrorInner); #[derive(Debug, Error)] pub(crate) enum BindErrorInner { #[error("failed to bind {addr}")] BindFailed { addr: std::net::SocketAddr, #[source] error: io::Error, }, #[error("failed to bind {addr}")] BindOrResolvFailed { addr: crate::resolv_addr::ResolvAddr, #[source] error: io::Error, }, #[cfg(all(linux, feature = "enable_systemd"))] #[error("failed to receive descriptors with names")] ReceiveDescriptors(#[source] crate::systemd_sockets::Error), #[error("missing systemd socket {0} - a typo or an attempt to bind twice")] #[cfg(all(linux, feature = "enable_systemd"))] MissingDescriptor(String), #[error("the systemd socket {0} is not an internet socket")] #[cfg(all(linux, feature = "enable_systemd"))] NotInetSocket(String), } /// Error that can happen when binding Tokio socket. /// /// As opposed to `std` and `async_std` sockets, tokio sockets can fail to convert. /// This error type expresses this possibility. #[cfg(any(feature = "tokio_0_2", feature = "tokio_0_3"))] #[derive(Debug, Error)] #[error(transparent)] pub enum TokioBindError { /// Either binding of socket or receiving systemd socket failed Bind(#[from] BindError), /// Conversion from std `std::net::TcpListener` to `tokio::net::TcpListener` failed Convert(#[from] TokioConversionError), } /// Error that can happen when converting Tokio socket. /// /// As opposed to `std` and `async_std` sockets, tokio sockets can fail to convert. /// This error type encapsulates conversion error together with additional information so that it /// can be displayed nicely. The encapsulation also allows for future-proofing. #[cfg(any(feature = "tokio_0_2", feature = "tokio_0_3"))] #[derive(Debug, Error)] #[error("failed to convert std socket {addr} into tokio socket")] pub struct TokioConversionError { pub(crate) addr: crate::SocketAddrInner, #[source] pub(crate) error: io::Error, }