Mercurial > crates > systemd-socket
annotate src/error.rs @ 4:66c0e10c89fc
Support resolving hostnames
Until now the crate supported only IP addresses and systemd sockets.
This was troublesome because it prevented the popular `localhost:1234`
format. This commit changes the behavior so that if parsing of
`std::net::SocketAddr` fails it attempts to parse it as `hostname:port`.
`bind_*()` methods were also modified to be async because of this.
author | Martin Habovstiak <martin.habovstiak@gmail.com> |
---|---|
date | Fri, 27 Nov 2020 15:05:19 +0100 |
parents | cabc4aafdd85 |
children | a7893294e9b2 |
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), |
0 | 22 #[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
|
23 InvalidCharacter { string: String, c: char, pos: usize, }, |
cabc4aafdd85
Added length check and a few tests
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
0
diff
changeset
|
24 #[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
|
25 LongSocketName { string: String, len: usize, }, |
0 | 26 } |
27 | |
28 /// Error that can occur during parsing of `SocketAddr` from a `OsStr`/`OsString` | |
29 /// | |
30 /// As opposed to parsing from `&str` or `String`, parsing from `OsStr` can fail due to one more | |
31 /// reason: invalid UTF-8. This error type expresses this possibility and is returned whenever such | |
32 /// conversion is attempted. It is not opaque because the possible variants are pretty much | |
33 /// certain, but it may contain `ParseError` which is opaque. | |
34 /// | |
35 /// This error can be displayed using standard `Error` trait. | |
36 /// See `ParseError` for more information. | |
37 #[derive(Debug, Error)] | |
38 pub enum ParseOsStrError { | |
39 /// The input was not a valid UTF-8 string | |
40 #[error("the address is not a valid UTF-8 string")] | |
41 InvalidUtf8, | |
42 /// The input was a valid UTF-8 string but the address was invalid | |
43 #[error(transparent)] | |
44 InvalidAddress(#[from] ParseError), | |
45 } | |
46 | |
47 /// Error that can occur during binding of a socket | |
48 /// | |
49 /// This encapsulates possible errors that can occur when binding a socket or receiving a socket | |
50 /// from systemd. | |
51 /// It is currently opaque because the representation is not certain yet. | |
52 /// It can be displayed using the standard `Error` trait. | |
53 #[derive(Debug, Error)] | |
54 #[error(transparent)] | |
55 pub struct BindError(#[from] pub(crate) BindErrorInner); | |
56 | |
57 #[derive(Debug, Error)] | |
58 pub(crate) enum BindErrorInner { | |
59 #[error("failed to bind {addr}")] | |
60 BindFailed { addr: std::net::SocketAddr, #[source] error: io::Error, }, | |
4
66c0e10c89fc
Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
2
diff
changeset
|
61 #[error("failed to bind {addr}")] |
66c0e10c89fc
Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
2
diff
changeset
|
62 BindOrResolvFailed { addr: crate::resolv_addr::ResolvAddr, #[source] error: io::Error, }, |
0 | 63 #[error("failed to receive descriptors with names")] |
64 ReceiveDescriptors(#[source] crate::systemd_sockets::Error), | |
65 #[error("missing systemd socket {0} - a typo or an attempt to bind twice")] | |
66 MissingDescriptor(String), | |
67 #[error("the systemd socket {0} is not an internet socket")] | |
68 NotInetSocket(String), | |
69 } | |
70 | |
71 /// Error that can happen when binding Tokio socket. | |
72 /// | |
73 /// As opposed to `std` and `async_std` sockets, tokio sockets can fail to convert. | |
74 /// This error type expresses this possibility. | |
75 #[cfg(any(feature = "tokio_0_2", feature = "tokio_0_3"))] | |
76 #[derive(Debug, Error)] | |
77 #[error(transparent)] | |
78 pub enum TokioBindError { | |
79 /// Either binding of socket or receiving systemd socket failed | |
80 Bind(#[from] BindError), | |
81 /// Conversion from std `std::net::TcpListener` to `tokio::net::TcpListener` failed | |
82 Convert(#[from] TokioConversionError), | |
83 } | |
84 | |
85 /// Error that can happen when converting Tokio socket. | |
86 /// | |
87 /// As opposed to `std` and `async_std` sockets, tokio sockets can fail to convert. | |
88 /// This error type encapsulates conversion error together with additional information so that it | |
89 /// can be displayed nicely. The encapsulation also allows for future-proofing. | |
90 #[cfg(any(feature = "tokio_0_2", feature = "tokio_0_3"))] | |
91 #[derive(Debug, Error)] | |
92 #[error("failed to convert std socket {addr} into tokio socket")] | |
93 pub struct TokioConversionError { | |
94 pub(crate) addr: crate::SocketAddrInner, | |
95 #[source] | |
96 pub(crate) error: io::Error, | |
97 } | |
98 |