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
//! Errors produced by FoukoApi.
use std::fmt;
/// Shorthand for `std::result::Result<T, foukoapi::Error>`.
pub type Result<T, E = Error> = std::result::Result<T, E>;
/// Top-level error type used across FoukoApi.
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum Error {
/// An adapter failed to start, connect or send a message.
#[error("platform error ({platform}): {message}")]
Platform {
/// Human-readable platform name, e.g. `"telegram"`.
platform: &'static str,
/// Description of what went wrong.
message: String,
},
/// The bot was asked to run but no platforms were registered.
#[error("no platforms registered - add at least one with Bot::add_platform")]
NoPlatforms,
/// A handler returned an error.
#[error("handler error: {0}")]
Handler(Box<dyn std::error::Error + Send + Sync>),
/// Catch-all for anything that doesn't fit a specific variant yet.
#[error("{0}")]
Other(String),
}
impl Error {
/// Build a [`Error::Platform`] from a platform name and any display-able error.
pub fn platform<E: fmt::Display>(platform: &'static str, err: E) -> Self {
Self::Platform {
platform,
message: err.to_string(),
}
}
}
impl From<std::env::VarError> for Error {
fn from(e: std::env::VarError) -> Self {
Self::Other(format!("missing env var: {e}"))
}
}
impl From<std::io::Error> for Error {
fn from(e: std::io::Error) -> Self {
Self::Other(format!("io error: {e}"))
}
}