Mercurial > crates > systemd-socket
annotate src/error.rs @ 21:f6334887e3c8
Support `tokio` 1.0
Tokio 1.0 was already out for a while and this adds the missing support
for it. Deprecation of 0.2 and 0.3 is planned but they are staying for
now.
author | Martin Habovstiak <martin.habovstiak@gmail.com> |
---|---|
date | Sat, 13 Jul 2024 15:09:13 +0200 |
parents | bc76507dd878 |
children | 1941e9d9819c |
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 | |
5 | |
6 use thiserror::Error; | |
7 use std::io; | |
8 | |
9 /// Error that can occur during parsing of `SocketAddr` from a string | |
10 /// | |
11 /// This encapsulates possible errors that can occur when parsing the input. | |
12 /// It is currently opaque because the representation is not certain yet. | |
13 /// It can be displayed using the standard `Error` trait. | |
14 #[derive(Debug, Error)] | |
15 #[error(transparent)] | |
16 pub struct ParseError(#[from] pub(crate) ParseErrorInner); | |
17 | |
18 #[derive(Debug, Error)] | |
19 pub(crate) enum ParseErrorInner { | |
20 #[error("failed to parse socket address")] | |
4
66c0e10c89fc
Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
2
diff
changeset
|
21 ResolvAddr(#[from] crate::resolv_addr::ResolvAddrError), |
16
bc76507dd878
Fixed conditional compilation based on OS
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
13
diff
changeset
|
22 #[cfg(all(target_os = "linux", feature = "enable_systemd"))] |
0 | 23 #[error("invalid character '{c}' in systemd socket name {string} at position {pos}")] |
2
cabc4aafdd85
Added length check and a few tests
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
0
diff
changeset
|
24 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
|
25 #[cfg(all(target_os = "linux", feature = "enable_systemd"))] |
2
cabc4aafdd85
Added length check and a few tests
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
0
diff
changeset
|
26 #[error("systemd socket name {string} is {len} characters long which is more than the limit 255")] |
cabc4aafdd85
Added length check and a few tests
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
0
diff
changeset
|
27 LongSocketName { string: String, len: usize, }, |
16
bc76507dd878
Fixed conditional compilation based on OS
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
13
diff
changeset
|
28 #[cfg(not(all(target_os = "linux", feature = "enable_systemd")))] |
bc76507dd878
Fixed conditional compilation based on OS
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
13
diff
changeset
|
29 #[cfg_attr(not(target_os = "linux"), error("can't parse {0} because systemd is not supported on this operating system"))] |
bc76507dd878
Fixed conditional compilation based on OS
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
13
diff
changeset
|
30 #[cfg_attr(target_os = "linux", error("can't parse {0} because systemd support was disabled during build"))] |
6
a7893294e9b2
Make the crate compilable on non-linux systems
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
4
diff
changeset
|
31 SystemdUnsupported(String), |
0 | 32 } |
33 | |
34 /// Error that can occur during parsing of `SocketAddr` from a `OsStr`/`OsString` | |
35 /// | |
36 /// As opposed to parsing from `&str` or `String`, parsing from `OsStr` can fail due to one more | |
37 /// reason: invalid UTF-8. This error type expresses this possibility and is returned whenever such | |
38 /// conversion is attempted. It is not opaque because the possible variants are pretty much | |
39 /// certain, but it may contain `ParseError` which is opaque. | |
40 /// | |
41 /// This error can be displayed using standard `Error` trait. | |
42 /// See `ParseError` for more information. | |
43 #[derive(Debug, Error)] | |
44 pub enum ParseOsStrError { | |
45 /// The input was not a valid UTF-8 string | |
46 #[error("the address is not a valid UTF-8 string")] | |
47 InvalidUtf8, | |
48 /// The input was a valid UTF-8 string but the address was invalid | |
49 #[error(transparent)] | |
50 InvalidAddress(#[from] ParseError), | |
51 } | |
52 | |
53 /// Error that can occur during binding of a socket | |
54 /// | |
55 /// This encapsulates possible errors that can occur when binding a socket or receiving a socket | |
56 /// from systemd. | |
57 /// It is currently opaque because the representation is not certain yet. | |
58 /// It can be displayed using the standard `Error` trait. | |
59 #[derive(Debug, Error)] | |
60 #[error(transparent)] | |
61 pub struct BindError(#[from] pub(crate) BindErrorInner); | |
62 | |
63 #[derive(Debug, Error)] | |
64 pub(crate) enum BindErrorInner { | |
65 #[error("failed to bind {addr}")] | |
66 BindFailed { addr: std::net::SocketAddr, #[source] error: io::Error, }, | |
4
66c0e10c89fc
Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
2
diff
changeset
|
67 #[error("failed to bind {addr}")] |
66c0e10c89fc
Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
2
diff
changeset
|
68 BindOrResolvFailed { addr: crate::resolv_addr::ResolvAddr, #[source] error: io::Error, }, |
16
bc76507dd878
Fixed conditional compilation based on OS
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
13
diff
changeset
|
69 #[cfg(all(target_os = "linux", feature = "enable_systemd"))] |
0 | 70 #[error("failed to receive descriptors with names")] |
71 ReceiveDescriptors(#[source] crate::systemd_sockets::Error), | |
72 #[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
|
73 #[cfg(all(target_os = "linux", feature = "enable_systemd"))] |
0 | 74 MissingDescriptor(String), |
75 #[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
|
76 #[cfg(all(target_os = "linux", feature = "enable_systemd"))] |
0 | 77 NotInetSocket(String), |
78 } | |
79 | |
80 /// Error that can happen when binding Tokio socket. | |
81 /// | |
82 /// As opposed to `std` and `async_std` sockets, tokio sockets can fail to convert. | |
83 /// This error type expresses this possibility. | |
21
f6334887e3c8
Support `tokio` 1.0
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
16
diff
changeset
|
84 #[cfg(any(feature = "tokio", feature = "tokio_0_2", feature = "tokio_0_3"))] |
0 | 85 #[derive(Debug, Error)] |
86 #[error(transparent)] | |
87 pub enum TokioBindError { | |
88 /// Either binding of socket or receiving systemd socket failed | |
89 Bind(#[from] BindError), | |
90 /// Conversion from std `std::net::TcpListener` to `tokio::net::TcpListener` failed | |
91 Convert(#[from] TokioConversionError), | |
92 } | |
93 | |
94 /// Error that can happen when converting Tokio socket. | |
95 /// | |
96 /// As opposed to `std` and `async_std` sockets, tokio sockets can fail to convert. | |
97 /// This error type encapsulates conversion error together with additional information so that it | |
98 /// 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
|
99 #[cfg(any(feature = "tokio", feature = "tokio_0_2", feature = "tokio_0_3"))] |
0 | 100 #[derive(Debug, Error)] |
101 #[error("failed to convert std socket {addr} into tokio socket")] | |
102 pub struct TokioConversionError { | |
103 pub(crate) addr: crate::SocketAddrInner, | |
104 #[source] | |
105 pub(crate) error: io::Error, | |
106 } | |
107 |