comparison src/error.rs @ 0:a65053246c29

Initial commit
author Martin Habovstiak <martin.habovstiak@gmail.com>
date Thu, 26 Nov 2020 22:53:35 +0100
parents
children cabc4aafdd85
comparison
equal deleted inserted replaced
-1:000000000000 0:a65053246c29
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")]
21 SocketAddr(std::net::AddrParseError),
22 #[error("invalid character '{c}' in systemd socket name {string} at position {pos}")]
23 InvalidCharacter { string: String, c: char, pos: usize, }
24 }
25
26 /// Error that can occur during parsing of `SocketAddr` from a `OsStr`/`OsString`
27 ///
28 /// As opposed to parsing from `&str` or `String`, parsing from `OsStr` can fail due to one more
29 /// reason: invalid UTF-8. This error type expresses this possibility and is returned whenever such
30 /// conversion is attempted. It is not opaque because the possible variants are pretty much
31 /// certain, but it may contain `ParseError` which is opaque.
32 ///
33 /// This error can be displayed using standard `Error` trait.
34 /// See `ParseError` for more information.
35 #[derive(Debug, Error)]
36 pub enum ParseOsStrError {
37 /// The input was not a valid UTF-8 string
38 #[error("the address is not a valid UTF-8 string")]
39 InvalidUtf8,
40 /// The input was a valid UTF-8 string but the address was invalid
41 #[error(transparent)]
42 InvalidAddress(#[from] ParseError),
43 }
44
45 /// Error that can occur during binding of a socket
46 ///
47 /// This encapsulates possible errors that can occur when binding a socket or receiving a socket
48 /// from systemd.
49 /// It is currently opaque because the representation is not certain yet.
50 /// It can be displayed using the standard `Error` trait.
51 #[derive(Debug, Error)]
52 #[error(transparent)]
53 pub struct BindError(#[from] pub(crate) BindErrorInner);
54
55 #[derive(Debug, Error)]
56 pub(crate) enum BindErrorInner {
57 #[error("failed to bind {addr}")]
58 BindFailed { addr: std::net::SocketAddr, #[source] error: io::Error, },
59 #[error("failed to receive descriptors with names")]
60 ReceiveDescriptors(#[source] crate::systemd_sockets::Error),
61 #[error("missing systemd socket {0} - a typo or an attempt to bind twice")]
62 MissingDescriptor(String),
63 #[error("the systemd socket {0} is not an internet socket")]
64 NotInetSocket(String),
65 }
66
67 /// Error that can happen when binding Tokio socket.
68 ///
69 /// As opposed to `std` and `async_std` sockets, tokio sockets can fail to convert.
70 /// This error type expresses this possibility.
71 #[cfg(any(feature = "tokio_0_2", feature = "tokio_0_3"))]
72 #[derive(Debug, Error)]
73 #[error(transparent)]
74 pub enum TokioBindError {
75 /// Either binding of socket or receiving systemd socket failed
76 Bind(#[from] BindError),
77 /// Conversion from std `std::net::TcpListener` to `tokio::net::TcpListener` failed
78 Convert(#[from] TokioConversionError),
79 }
80
81 /// Error that can happen when converting Tokio socket.
82 ///
83 /// As opposed to `std` and `async_std` sockets, tokio sockets can fail to convert.
84 /// This error type encapsulates conversion error together with additional information so that it
85 /// can be displayed nicely. The encapsulation also allows for future-proofing.
86 #[cfg(any(feature = "tokio_0_2", feature = "tokio_0_3"))]
87 #[derive(Debug, Error)]
88 #[error("failed to convert std socket {addr} into tokio socket")]
89 pub struct TokioConversionError {
90 pub(crate) addr: crate::SocketAddrInner,
91 #[source]
92 pub(crate) error: io::Error,
93 }
94