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
60
61
62
63
64
65
66
67
use std::fmt;
/// Every error the SDK returns.
#[derive(Debug, Clone)]
pub enum Error {
/// The license server could not be reached.
Network(String),
/// The server answered with a failure of its own (HTTP 5xx) — an outage,
/// not a verdict on the key. Handle it like [`Error::Network`].
Server { status: u16, message: String },
/// The server refused the key: invalid, expired, revoked, blocked...
InvalidLicense { status: u16, message: String },
/// Correctly signed, but for a different product.
ProductMismatch { expected: String, actual: String },
/// A usage-based key doesn't have enough credits left. Nothing was spent.
/// `usage` is what the server reported (unsigned: for messages only).
UsageExceeded { message: String, usage: serde_json::Value },
/// Signature, nonce or timestamp check failed: forged or replayed.
Signature(String),
/// The reply wasn't a usable response body.
Protocol(String),
/// A floating license has no free seat, or a held seat was lost.
SeatUnavailable(String),
/// Bad arguments (e.g. a malformed public key).
Config(String),
/// The sign-in token stopped working: the customer or the seller signed this
/// device out, or the license ended. Forget the token and sign in again.
SignedOut { status: u16, message: String },
/// Signing in could not start or go on (sign-in is off for the product, an
/// unknown product, too many tries).
SignIn { status: u16, message: String },
/// The customer declined the sign-in.
SignInDenied(String),
/// The sign-in code ran out before it was approved.
SignInExpired(String),
}
impl Error {
/// True for problems reaching the server (offline, 5xx) — the cases where
/// an app usually retries or applies a grace period.
pub fn is_network(&self) -> bool {
matches!(self, Error::Network(_) | Error::Server { .. })
}
/// True when the server (or the signature check) says the key may not be used.
pub fn is_refusal(&self) -> bool {
matches!(self, Error::InvalidLicense { .. } | Error::ProductMismatch { .. } | Error::UsageExceeded { .. } | Error::SignedOut { .. })
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Error::Network(m) | Error::Signature(m) | Error::Protocol(m) | Error::SeatUnavailable(m) | Error::Config(m)
| Error::SignInDenied(m) | Error::SignInExpired(m) => f.write_str(m),
Error::Server { message, .. } | Error::InvalidLicense { message, .. } | Error::UsageExceeded { message, .. }
| Error::SignedOut { message, .. } | Error::SignIn { message, .. } => f.write_str(message),
Error::ProductMismatch { expected, actual } => write!(
f, "This license is for a different product. Expected {expected}, got {}.",
if actual.is_empty() { "none" } else { actual }
),
}
}
}
impl std::error::Error for Error {}
pub type Result<T> = std::result::Result<T, Error>;