selium_std/
errors.rs

1use quinn::{ConnectError, ConnectionError, WriteError};
2use selium_log::error::LogError;
3use std::net::AddrParseError;
4use thiserror::Error;
5
6pub type Result<T, E = SeliumError> = std::result::Result<T, E>;
7
8#[derive(Error, Debug)]
9pub enum CryptoError {
10    #[error("Failed to read private key from file.")]
11    OpenKeyFileError(#[source] std::io::Error),
12
13    #[error("Malformed PKCS #1 private key.")]
14    MalformedPKCS1PrivateKey(#[source] std::io::Error),
15
16    #[error("Malformed PKCS #8 private key.")]
17    MalformedPKCS8PrivateKey(#[source] std::io::Error),
18
19    #[error("No private keys found in file.")]
20    NoPrivateKeysFound,
21
22    #[error("Failed to read certificate chain from file.")]
23    OpenCertFileError(#[source] std::io::Error),
24
25    #[error("Invalid PEM-encoded certificate")]
26    InvalidPemCertificate(#[source] std::io::Error),
27
28    #[error("No valid root cert found in file.")]
29    InvalidRootCert,
30}
31
32#[derive(Error, Debug)]
33pub enum ProtocolError {
34    #[error("Payload size ({0} bytes) is greater than maximum allowed size ({1} bytes).")]
35    PayloadTooLarge(u64, u64),
36
37    #[error("Unknown message type: {0}")]
38    UnknownMessageType(u8),
39
40    #[error("Failed to serialize/deserialize message on protocol.")]
41    SerdeError(#[source] bincode::Error),
42}
43
44#[derive(Error, Debug)]
45pub enum CodecError {
46    #[error("Failed to compress payload.")]
47    CompressFailure(#[source] anyhow::Error),
48
49    #[error("Failed to decompress payload.")]
50    DecompressFailure(#[source] anyhow::Error),
51
52    #[error("Failed to encode message payload.")]
53    EncodeFailure(#[source] anyhow::Error),
54
55    #[error("Failed to decode message frame.")]
56    DecodeFailure(#[source] anyhow::Error),
57}
58
59#[derive(Error, Debug)]
60pub enum QuicError {
61    #[error("Error sending message to topic.")]
62    WriteError(#[from] WriteError),
63
64    #[error("Failed to establish connection.")]
65    ConnectError(#[from] ConnectError),
66
67    #[error("An error occured on an existing connection.")]
68    ConnectionError(#[from] ConnectionError),
69
70    #[error("Too many connection retries.")]
71    TooManyRetries,
72}
73
74#[derive(Error, Debug)]
75pub enum ParseRemoteAddressError {
76    #[error("Missing remote address port.")]
77    MissingPort,
78
79    #[error("Poorly formatted address.")]
80    InvalidAddress(#[source] std::io::Error),
81
82    #[error("Couldn't resolve an address.")]
83    NoAddressResolved,
84}
85
86#[derive(Error, Debug)]
87pub enum ParseCertificateHostError {
88    #[error("No host address could be resolved.")]
89    InvalidHostAddress,
90}
91
92#[derive(Error, Debug)]
93pub enum ParseEndpointAddressError {
94    #[error("Invalid endpoint address.")]
95    InvalidAddress(#[source] AddrParseError),
96}
97
98#[derive(Error, Debug)]
99pub enum TopicError {
100    #[error("Failed to notify subscribers of new event.")]
101    NotifySubscribers(#[source] futures::channel::mpsc::SendError),
102}
103
104#[derive(Error, Debug)]
105pub enum SeliumError {
106    #[error(transparent)]
107    Quic(#[from] QuicError),
108
109    #[error(transparent)]
110    Crypto(#[from] CryptoError),
111
112    #[error(transparent)]
113    Log(#[from] LogError),
114
115    #[error(transparent)]
116    Topic(#[from] TopicError),
117
118    #[error(transparent)]
119    ParseEndpointAddress(#[from] ParseEndpointAddressError),
120
121    #[error(transparent)]
122    ParseRemoteAddress(#[from] ParseRemoteAddressError),
123
124    #[error(transparent)]
125    ParseCertificateHost(#[from] ParseCertificateHostError),
126
127    #[error("Failed to parse milliseconds from duration.")]
128    ParseDurationMillis,
129
130    #[error(transparent)]
131    Codec(#[from] CodecError),
132
133    #[error(transparent)]
134    Protocol(#[from] ProtocolError),
135
136    #[error("The request to the specified endpoint failed.")]
137    RequestFailed,
138
139    #[error("The request handler encountered an error: {0}.")]
140    RequestHandlerFailure(String),
141
142    #[error("The request timed out before receiving a reply.")]
143    RequestTimeout,
144
145    #[error("Failed to open stream on Selium Cloud endpoint.")]
146    OpenCloudStreamFailed(#[source] ConnectionError),
147
148    #[error("Failed to retrieve server address from Selium Cloud.")]
149    GetServerAddressFailed,
150
151    #[error("Cannot connect directly to the Selium cloud endpoint. Use the `selium::cloud` builder instead.")]
152    ConnectDirectToCloud,
153
154    #[error("Poorly formatted topic name, must be in the format [namespace]/[topic]")]
155    ParseTopicNameError,
156
157    #[error("Cannot use a reserved namespace prefix.")]
158    ReservedNamespaceError,
159
160    #[error(transparent)]
161    IoError(#[from] std::io::Error),
162
163    #[error("Failed to open stream with error: {1}.")]
164    OpenStream(u32, String),
165}