Skip to main content

skipper_core/
error.rs

1use std::fmt;
2
3/// Error type for every fallible Skipper operation.
4#[derive(Debug, Clone, PartialEq, Eq)]
5pub struct Error(pub String);
6
7pub type Result<T> = std::result::Result<T, Error>;
8
9impl Error {
10    pub fn new(msg: impl Into<String>) -> Self {
11        Error(msg.into())
12    }
13}
14
15impl fmt::Display for Error {
16    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
17        f.write_str(&self.0)
18    }
19}
20
21impl std::error::Error for Error {}
22
23macro_rules! from_err {
24    ($($t:ty),*) => {$(
25        impl From<$t> for Error {
26            fn from(e: $t) -> Self { Error(e.to_string()) }
27        }
28    )*};
29}
30
31from_err!(
32    std::io::Error,
33    serde_json::Error,
34    base64::DecodeError,
35    jsonwebtoken::errors::Error
36);
37
38impl From<ureq::Error> for Error {
39    fn from(e: ureq::Error) -> Self {
40        match e {
41            ureq::Error::Status(code, resp) => {
42                let url = resp.get_url().to_string();
43                let body = resp.into_string().unwrap_or_default();
44                Error(format!("HTTP {code} from {url}: {body}"))
45            }
46            other => Error(other.to_string()),
47        }
48    }
49}