Mercurial > crates > systemd-socket
annotate 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 |
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), |
13
f740dadd2948
Added enable_systemd feature
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
6
diff
changeset
|
22 #[cfg(all(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, }, |
13
f740dadd2948
Added enable_systemd feature
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
6
diff
changeset
|
25 #[cfg(all(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, }, |
13
f740dadd2948
Added enable_systemd feature
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
6
diff
changeset
|
28 #[cfg(not(all(linux, feature = "enable_systemd")))] |
f740dadd2948
Added enable_systemd feature
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
6
diff
changeset
|
29 #[cfg_attr(not(linux), error("can't parse {0} because systemd is not supported on this operating system"))] |
f740dadd2948
Added enable_systemd feature
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
6
diff
changeset
|
30 #[cfg_attr(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, }, |
13
f740dadd2948
Added enable_systemd feature
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
6
diff
changeset
|
69 #[cfg(all(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")] | |
13
f740dadd2948
Added enable_systemd feature
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
6
diff
changeset
|
73 #[cfg(all(linux, feature = "enable_systemd"))] |
0 | 74 MissingDescriptor(String), |
75 #[error("the systemd socket {0} is not an internet socket")] | |
13
f740dadd2948
Added enable_systemd feature
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
6
diff
changeset
|
76 #[cfg(all(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. | |
84 #[cfg(any(feature = "tokio_0_2", feature = "tokio_0_3"))] | |
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. | |
99 #[cfg(any(feature = "tokio_0_2", feature = "tokio_0_3"))] | |
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 |