Mercurial > crates > systemd-socket
annotate src/error.rs @ 29:efc69e99db70
Set socket to nonblocking before passing it to tokio.
| author | Paul Fisher <paul@pfish.zone> |
|---|---|
| date | Sat, 19 Apr 2025 01:41:25 -0400 |
| parents | cfef4593e207 |
| children |
| rev | line source |
|---|---|
| 0 | 1 //! Error types that can occur when dealing with `SocketAddr` |
| 2 //! | |
| 3 //! This module separates the error types from root module to avoid clutter. | |
| 4 | |
|
24
1941e9d9819c
Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
21
diff
changeset
|
5 use std::fmt; |
| 0 | 6 use std::io; |
| 28 | 7 use thiserror::Error; |
| 0 | 8 |
|
24
1941e9d9819c
Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
21
diff
changeset
|
9 /// Error returned when the library initialization fails. |
|
1941e9d9819c
Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
21
diff
changeset
|
10 #[derive(Debug)] |
|
1941e9d9819c
Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
21
diff
changeset
|
11 pub struct InitError(pub(crate) InitErrorInner); |
|
1941e9d9819c
Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
21
diff
changeset
|
12 |
|
1941e9d9819c
Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
21
diff
changeset
|
13 #[cfg(all(target_os = "linux", feature = "enable_systemd"))] |
|
1941e9d9819c
Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
21
diff
changeset
|
14 type InitErrorInner = super::systemd_sockets::InitError; |
|
1941e9d9819c
Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
21
diff
changeset
|
15 |
|
1941e9d9819c
Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
21
diff
changeset
|
16 #[cfg(not(all(target_os = "linux", feature = "enable_systemd")))] |
|
1941e9d9819c
Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
21
diff
changeset
|
17 type InitErrorInner = std::convert::Infallible; |
|
1941e9d9819c
Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
21
diff
changeset
|
18 |
|
1941e9d9819c
Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
21
diff
changeset
|
19 impl fmt::Display for InitError { |
|
1941e9d9819c
Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
21
diff
changeset
|
20 #[inline] |
|
1941e9d9819c
Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
21
diff
changeset
|
21 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
|
1941e9d9819c
Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
21
diff
changeset
|
22 fmt::Display::fmt(&self.0, f) |
|
1941e9d9819c
Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
21
diff
changeset
|
23 } |
|
1941e9d9819c
Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
21
diff
changeset
|
24 } |
|
1941e9d9819c
Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
21
diff
changeset
|
25 |
|
1941e9d9819c
Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
21
diff
changeset
|
26 impl std::error::Error for InitError { |
|
1941e9d9819c
Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
21
diff
changeset
|
27 #[inline] |
|
1941e9d9819c
Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
21
diff
changeset
|
28 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { |
|
1941e9d9819c
Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
21
diff
changeset
|
29 self.0.source() |
|
1941e9d9819c
Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
21
diff
changeset
|
30 } |
|
1941e9d9819c
Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
21
diff
changeset
|
31 } |
|
1941e9d9819c
Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
21
diff
changeset
|
32 |
| 0 | 33 /// Error that can occur during parsing of `SocketAddr` from a string |
| 34 /// | |
| 35 /// This encapsulates possible errors that can occur when parsing the input. | |
| 36 /// It is currently opaque because the representation is not certain yet. | |
| 37 /// It can be displayed using the standard `Error` trait. | |
| 38 #[derive(Debug, Error)] | |
| 39 #[error(transparent)] | |
| 40 pub struct ParseError(#[from] pub(crate) ParseErrorInner); | |
| 41 | |
| 42 #[derive(Debug, Error)] | |
| 43 pub(crate) enum ParseErrorInner { | |
| 44 #[error("failed to parse socket address")] | |
|
4
66c0e10c89fc
Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
2
diff
changeset
|
45 ResolvAddr(#[from] crate::resolv_addr::ResolvAddrError), |
|
16
bc76507dd878
Fixed conditional compilation based on OS
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
13
diff
changeset
|
46 #[cfg(all(target_os = "linux", feature = "enable_systemd"))] |
| 0 | 47 #[error("invalid character '{c}' in systemd socket name {string} at position {pos}")] |
| 28 | 48 InvalidCharacter { string: String, c: char, pos: usize }, |
|
16
bc76507dd878
Fixed conditional compilation based on OS
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
13
diff
changeset
|
49 #[cfg(all(target_os = "linux", feature = "enable_systemd"))] |
| 28 | 50 #[error( |
| 51 "systemd socket name {string} is {len} characters long which is more than the limit 255" | |
| 52 )] | |
| 53 LongSocketName { string: String, len: usize }, | |
|
16
bc76507dd878
Fixed conditional compilation based on OS
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
13
diff
changeset
|
54 #[cfg(not(all(target_os = "linux", feature = "enable_systemd")))] |
| 28 | 55 #[cfg_attr( |
| 56 not(target_os = "linux"), | |
| 57 error("can't parse {0} because systemd is not supported on this operating system") | |
| 58 )] | |
| 59 #[cfg_attr( | |
| 60 target_os = "linux", | |
| 61 error("can't parse {0} because systemd support was disabled during build") | |
| 62 )] | |
|
6
a7893294e9b2
Make the crate compilable on non-linux systems
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
4
diff
changeset
|
63 SystemdUnsupported(String), |
| 0 | 64 } |
| 65 | |
| 66 /// Error that can occur during parsing of `SocketAddr` from a `OsStr`/`OsString` | |
| 67 /// | |
| 68 /// As opposed to parsing from `&str` or `String`, parsing from `OsStr` can fail due to one more | |
| 69 /// reason: invalid UTF-8. This error type expresses this possibility and is returned whenever such | |
| 70 /// conversion is attempted. It is not opaque because the possible variants are pretty much | |
| 71 /// certain, but it may contain `ParseError` which is opaque. | |
| 72 /// | |
| 73 /// This error can be displayed using standard `Error` trait. | |
| 74 /// See `ParseError` for more information. | |
| 75 #[derive(Debug, Error)] | |
| 76 pub enum ParseOsStrError { | |
| 77 /// The input was not a valid UTF-8 string | |
| 78 #[error("the address is not a valid UTF-8 string")] | |
| 79 InvalidUtf8, | |
| 80 /// The input was a valid UTF-8 string but the address was invalid | |
| 81 #[error(transparent)] | |
| 82 InvalidAddress(#[from] ParseError), | |
| 83 } | |
| 84 | |
| 85 /// Error that can occur during binding of a socket | |
| 86 /// | |
| 87 /// This encapsulates possible errors that can occur when binding a socket or receiving a socket | |
| 88 /// from systemd. | |
| 89 /// It is currently opaque because the representation is not certain yet. | |
| 90 /// It can be displayed using the standard `Error` trait. | |
| 91 #[derive(Debug, Error)] | |
| 92 #[error(transparent)] | |
| 93 pub struct BindError(#[from] pub(crate) BindErrorInner); | |
| 94 | |
| 95 #[derive(Debug, Error)] | |
| 96 pub(crate) enum BindErrorInner { | |
| 97 #[error("failed to bind {addr}")] | |
| 28 | 98 BindFailed { |
| 99 addr: std::net::SocketAddr, | |
| 100 #[source] | |
| 101 error: io::Error, | |
| 102 }, | |
|
4
66c0e10c89fc
Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
2
diff
changeset
|
103 #[error("failed to bind {addr}")] |
| 28 | 104 BindOrResolvFailed { |
| 105 addr: crate::resolv_addr::ResolvAddr, | |
| 106 #[source] | |
| 107 error: io::Error, | |
| 108 }, | |
|
16
bc76507dd878
Fixed conditional compilation based on OS
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
13
diff
changeset
|
109 #[cfg(all(target_os = "linux", feature = "enable_systemd"))] |
| 0 | 110 #[error("failed to receive descriptors with names")] |
| 111 ReceiveDescriptors(#[source] crate::systemd_sockets::Error), | |
| 112 #[error("missing systemd socket {0} - a typo or an attempt to bind twice")] | |
|
16
bc76507dd878
Fixed conditional compilation based on OS
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
13
diff
changeset
|
113 #[cfg(all(target_os = "linux", feature = "enable_systemd"))] |
| 0 | 114 MissingDescriptor(String), |
| 115 #[error("the systemd socket {0} is not an internet socket")] | |
|
16
bc76507dd878
Fixed conditional compilation based on OS
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
13
diff
changeset
|
116 #[cfg(all(target_os = "linux", feature = "enable_systemd"))] |
| 0 | 117 NotInetSocket(String), |
| 118 } | |
| 119 | |
| 120 /// Error that can happen when binding Tokio socket. | |
| 121 /// | |
| 122 /// As opposed to `std` and `async_std` sockets, tokio sockets can fail to convert. | |
| 123 /// This error type expresses this possibility. | |
|
21
f6334887e3c8
Support `tokio` 1.0
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
16
diff
changeset
|
124 #[cfg(any(feature = "tokio", feature = "tokio_0_2", feature = "tokio_0_3"))] |
| 0 | 125 #[derive(Debug, Error)] |
| 126 #[error(transparent)] | |
| 127 pub enum TokioBindError { | |
| 128 /// Either binding of socket or receiving systemd socket failed | |
| 129 Bind(#[from] BindError), | |
| 130 /// Conversion from std `std::net::TcpListener` to `tokio::net::TcpListener` failed | |
| 131 Convert(#[from] TokioConversionError), | |
| 132 } | |
| 133 | |
| 134 /// Error that can happen when converting Tokio socket. | |
| 135 /// | |
| 136 /// As opposed to `std` and `async_std` sockets, tokio sockets can fail to convert. | |
| 137 /// This error type encapsulates conversion error together with additional information so that it | |
| 138 /// can be displayed nicely. The encapsulation also allows for future-proofing. | |
|
21
f6334887e3c8
Support `tokio` 1.0
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
16
diff
changeset
|
139 #[cfg(any(feature = "tokio", feature = "tokio_0_2", feature = "tokio_0_3"))] |
| 0 | 140 #[derive(Debug, Error)] |
| 141 #[error("failed to convert std socket {addr} into tokio socket")] | |
| 142 pub struct TokioConversionError { | |
| 143 pub(crate) addr: crate::SocketAddrInner, | |
| 144 #[source] | |
| 145 pub(crate) error: io::Error, | |
| 146 } |
