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
//! What can go wrong in Runlight's own work.
use crate::goals::CodedError;
use crate::store::DbError;
/// An error from Runlight: a database that failed, or something refused with a code the dashboard
/// says in its own words.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Error {
/// The database failed.
Db(DbError),
/// A setting refused (the SDK's SettingsError, a RangeError).
Settings(CodedError),
/// The mail service refused, or is not set up (MailError).
Mail(CodedError),
/// A short link that cannot be made (LinkError).
Link(CodedError),
/// Something asked for that is not there (the SDK's plain RangeError, such as "Unknown link").
Range(String),
/// Anything else, with its message.
Other(String),
}
impl Error {
/// The English message.
pub fn message(&self) -> &str {
match self {
Error::Db(e) => &e.0,
Error::Settings(e) | Error::Mail(e) | Error::Link(e) => &e.message,
Error::Range(m) | Error::Other(m) => m,
}
}
/// The code and params of an error that carries them.
pub fn coded(&self) -> Option<&CodedError> {
match self {
Error::Settings(e) | Error::Mail(e) | Error::Link(e) => Some(e),
_ => None,
}
}
/// A SettingsError.
pub fn settings(message: impl Into<String>, code: &str, params: &[(&str, &str)]) -> Error {
Error::Settings(CodedError::new(message, code, params))
}
}
impl std::fmt::Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(self.message())
}
}
impl std::error::Error for Error {}
impl From<DbError> for Error {
fn from(e: DbError) -> Error {
Error::Db(e)
}
}