1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
//! The errors the client returns.
use popsub_protocol::{CloseCode, ErrorCode};
use thiserror::Error;
/// Why a client call failed.
#[derive(Debug, Clone, Error, PartialEq, Eq)]
pub enum Error {
/// The server refused the request.
#[error("the server refused the request ({code:?}): {message}")]
Server {
/// What went wrong, for programs.
code: ErrorCode,
/// What went wrong, for people.
message: String,
/// Seconds to wait before trying again, for limits.
retry_after: Option<u64>,
},
/// The client has stopped for good: it was closed, the server closed it
/// in a way that rules out reconnecting (such as `SessionTakenOver`), or
/// it ran out of reconnect attempts.
#[error("the client is closed ({0:?})")]
Closed(Option<CloseCode>),
/// The request didn't complete within `ClientConfig::request_timeout`.
#[error("the request timed out")]
Timeout,
/// The server couldn't be reached or the WebSocket handshake failed.
#[error("can't connect: {0}")]
Connect(String),
/// The server sent something this client doesn't expect.
#[error("unexpected reply from the server: {0}")]
Protocol(String),
/// The configuration is invalid, such as a root certificate that doesn't parse.
#[error("invalid configuration: {0}")]
Config(String),
}
impl Error {
/// Whether retrying the same thing later might work.
///
/// Returns:
/// True for timeouts, connection failures and the server's
/// temporary refusals (`rate_limited`, `locked_out`,
/// `storage_unavailable`, `session_unavailable`, `internal`).
pub fn is_temporary(&self) -> bool {
match self {
Error::Timeout | Error::Connect(_) => true,
Error::Server { code, .. } => matches!(
code,
ErrorCode::RateLimited
| ErrorCode::LockedOut
| ErrorCode::StorageUnavailable
| ErrorCode::SessionUnavailable
| ErrorCode::Internal
),
Error::Closed(_) | Error::Protocol(_) | Error::Config(_) => false,
}
}
}